0

I'm trying to prompt retail customers to add an accessory to the cart.

I want to display a message only if the product is not in the cart.

Is there a way to set the "if" condition to false so I don't have to use elseif?


add_action( 'woocommerce_before_cart', 'print_custom_cart_notice', 10 );
function print_custom_cart_notice() {
    
     if ( is_admin() && ! defined( 'DOING_AJAX' ) )
         return;
         
     $user_obj    = wp_get_current_user(); 
 
     if ( in_array('customer', $user_obj->roles ) || ! is_user_logged_in() ) {
         
         $link = 'https://website.com/cart/?add-to-cart=13720&quantity=1';
         $product_id = 13720;
         
         $product_cart_id = WC()->cart->generate_cart_id( $product_id );
         $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
  
         if ( ! $in_cart ) {
         
         wc_print_notice( sprintf( '<span class="cross-sell-cart">' .
        __('Add a Tote Bag to your order. %s', 'woocommerce') . '</span>',
        '<a href='.$link.' class="button alt" style="float:right">'. __('Add to Cart', 'woocommerce') .'</a>'
    ), 'notice' );
         }
     }
}

[UPDATE] I've changed the coding to include the Not logical operator !. I've left the question as this might be helpful to others looking to display cart notices.

0 Answers0