questions

Hide “remove item” and “quantity input” from cart in WooCommerce

Rate this post

On the WooCommerce cart page I want to hide:

  • “remove item” button
  • “quantity input” field

For items that will go in the comment of // HIDE REMOVE BUTTON & QUNATITY OF THESE ITEMS in the code below

if (is_cart()){
    foreach ( $cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];

        $original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
        
        if ($original_name == "Build Your Own"){
            $meta = wc_get_formatted_cart_item_data( $cart_item, true );
            $sMeta = substr($meta, -7);
            $new_name = $original_name . ' - ' . $sMeta;
            
            if( method_exists( $product, 'set_name' ) )
                $product->set_name( $new_name );
            else
                $product->post->post_title = $new_name;
        }else{
            $meta = wc_get_formatted_cart_item_data( $cart_item, true );
            
            if (!empty($meta)){
              
              // HIDE REMOVE BUTTON & QUANTITY OF THESE ITEMS.
              
            }
        }
    }
}

Can someone walk me through how to do that?

 

✔️Solution:

A first option is to hide these fields via jQuery and CSS. You could use various hooks on the cart page, like the woocommerce_before_calculate_totals action hook.

Note: because all table rows of shop_table contain the same classes, the correct button/field is hidden based on the product ID.

  • Explanation via comment tags added in the code
function action_woocommerce_before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

   if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Only cart
    if( ! is_cart() )
        return;

    // If cart is NOT empty
    if ( ! $cart->is_empty() ) {
    
        // Loop
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            // Get an instance of the WC_Product object
            $product = $cart_item['data'];
            
            // Get product id
            $product_id = $cart_item['product_id'];

            // Get name
            $original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
            
            // Compare
            if ( $original_name == 'Build Your Own') {
                $meta = wc_get_formatted_cart_item_data( $cart_item, true );
                $sMeta = substr( $meta, -7 );
                $new_name = $original_name . ' - ' . $sMeta;
                
                // Set name
                if( method_exists( $product, 'set_name' ) ) {
                    $product->set_name( $new_name );
                } else {
                    $product->post->post_title = $new_name;
                }
            } else {
                $meta = wc_get_formatted_cart_item_data( $cart_item, true );
            
                // NOT empty
                if ( ! empty( $meta ) ) {
                    // Hide remove button & quantity fields
                    ?>
                    <script>
                        jQuery( document ).ready( function($) {
                            // Selector (product remove)
                            var product_selector = '[data-product_id="<?php echo $product_id; ?>"]';
                            
                            // Hide 'remove item'
                            $( product_selector ).css( 'display', 'none' );
                            
                            // Hide 'quantity input' (starting from the product remove selector)
                            $( product_selector ).parent().siblings( '.product-quantity' ).find( '.quantity' ).css( 'display', 'none' );
                        });
                    </script>
                    <?php
                }
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

Another option is to hide the remove button via the woocommerce_cart_item_remove_link filter hook. So it depends on your preference and ultimate goal, you could then apply something similar for the quantity field.

function filter_woocommerce_cart_item_remove_link( $link, $cart_item_key ) {
    // Returns true on the cart page.
    if ( is_cart() ) {
        // Get cart
        $cart = WC()->cart;
        
        // If cart is NOT empty
        if ( ! $cart->is_empty() ) {
        
            // Loop
            foreach ( $cart->get_cart() as $cart_item ) {
                // Get an instance of the WC_Product object
                $product = $cart_item['data'];

                // Get name
                $original_name = method_exists( $product, 'get_name' ) ? $product->get_name() : $product->post->post_title;
                
                // Compare
                if ( $original_name == 'Build Your Own') {
                    $meta = wc_get_formatted_cart_item_data( $cart_item, true );
                    $sMeta = substr( $meta, -7 );
                    $new_name = $original_name . ' - ' . $sMeta;
                    
                    // Set name
                    if( method_exists( $product, 'set_name' ) ) {
                        $product->set_name( $new_name );
                    } else {
                        $product->post->post_title = $new_name;
                    }
                } else {
                    $meta = wc_get_formatted_cart_item_data( $cart_item, true );
                    
                    // NOT empty
                    if ( ! empty( $meta ) ) {
                        // Hide remove button
                        $link = '';
                    }
                }
            }
        }
    }

    return $link;
}
add_filter( 'woocommerce_cart_item_remove_link', 'filter_woocommerce_cart_item_remove_link', 10, 2 );

Leave a Reply

Your email address will not be published.

Back to top button