• Resolved sadieboxchilli

    (@sadieboxchilli)


    Hi,

    I want to replicate the remove product functionality you get in the cart within the success.php message that appears whenever you add a product to your cart on the single product page. I want it to sit as a button next to the view cart button.

    I have managed to get achieve this by using the following:

    <?php foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
         $_product   = apply_filters( 'woocommerce_cart_item_product', 
         $cart_item['data'], $cart_item, $cart_item_key );
         $product_id = apply_filters( 'woocommerce_cart_item_product_id', 
         $cart_item['product_id'], $cart_item, $cart_item_key );
    
         if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && 
             apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, 
             $cart_item_key ) ) 
         {
         $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', 
         $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', 
         $cart_item, $cart_item_key );
    ?>
    
    <div class="woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
    
    <?php echo apply_filters( 
    							 
       'woocommerce_cart_item_remove_link',
       sprintf( '<a href="%s" class="remove blueB removeProd" aria-label="%s" data- 
       product_id="%s" data-product_sku="%s">Remove product</a>',
       esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
       esc_html__( 'Remove this item', 'woocommerce' ),
       esc_attr( $product_id ),
       esc_attr( $_product->get_sku() )
    ),
    $cart_item_key);
    ?>
    
    </div>
    <?php }}?>

    I know you need to pull the product ID and key for this to work, however it appears to work at first, but then it starts to include multiple buttons for different items within the cart.

    Is it possible to just have the button to appear for that one product you add to cart on the single product page? Not display previous products added to the cart?

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support AW a11n

    (@slash1andy)

    Automattic Happiness Engineer

    Hey there!

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the following places for more development-oriented questions:

    1. WooCommerce Slack Community: https://woocommerce.com/community-slack/
    2. WooCommerce FB group: https://www.facebook.com/groups/advanced.woocommerce/

    Hello,

    Below is code which you can use for same.

    <?php 
    		$current_pid = get_the_ID();
    
    		foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    
    			$_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
    			$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
    
    			if($product_id == $current_pid) {				
    
    				if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
    
    					echo '<a href="'.esc_url( wc_get_cart_remove_url( $cart_item_key ) ).'" class="button wc-forward" >Remove</a>';
    					
    
    				}
    			}
    
    		}
    		?>
    Thread Starter sadieboxchilli

    (@sadieboxchilli)

    Hi,

    That seems to have done the job from excluding other products on other pages which is great, however if I have a variable product and add two different instances of that same product it will still bring up multiple buttons. Is there a way to target only the last product added to the basket?

    As if they’ve added two of the same product but different variables they should only get the chance to remove it immediately after they’ve added the first variable product and reset if they’ve added another?

    Thanks in advance, appreciate your help!

    Hello,

    That will be tricky to do. Cause on product page we cannot only get product id and not product variation. Yet let me check and get back to you.

    Thread Starter sadieboxchilli

    (@sadieboxchilli)

    Of course, thank you!

    Thread Starter sadieboxchilli

    (@sadieboxchilli)

    Unless putting the results into an array and only loading the first value?

    <?php 
    		$current_pid = get_the_ID();
    		$remove_html = '';
    		foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    
    			$_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
    			$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
    
    			if($product_id == $current_pid) {				
    
    					if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
    
    						$remove_html = '<a href="'.esc_url( wc_get_cart_remove_url( $cart_item_key ) ).'" class="button wc-forward" >Remove</a>';
    						
    					}
    
    			}
    
    		}
    		echo $remove_html;
    		?>

    This will add last variable product to remove. But after you remove it, it will again display the last one added before that. So you need to find minor logic around it.

    Regards,
    Rishi Mehta

    Thread Starter sadieboxchilli

    (@sadieboxchilli)

    Thank you so much, you’ve been a massive help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Add a delete product button to product page notice message.’ is closed to new replies.