skip to Main Content

I want to hide the product quantity in the WooCommerce Checkout and replace the “x 1” next to the title with an extra line below the product title.

But it should show the quantity of the product only if there are more than 1 of it in the order.

I found the following code which adds the extra line for the quantity. But I couldn’t figure out how to display the quantity only if there is > 1 product in the order.

add_filter( 'woocommerce_checkout_cart_item_quantity', 'customizing_checkout_item_quantity', 10, 3);
function customizing_checkout_item_quantity( $quantity_html, $cart_item, $cart_item_key ) {
    $quantity_html = ' <br>
            <span class="product-quantity">' . __('Quantity', 'woocommerce') . ': <strong>' . $cart_item['quantity'] . '</strong></span>';

    return $quantity_html;
}

Code is from this answer: https://stackoverflow.com/a/48233426/1788961

Is there any way to check the quantity and display the line only if there a more than 1 of the product in the order?

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out. There is already the quantity in the functions:

    This is my solution:

    add_filter( 'woocommerce_checkout_cart_item_quantity', 'customizing_checkout_item_quantity', 10, 3);
    function customizing_checkout_item_quantity( $quantity_html, $cart_item, $cart_item_key ) {
    
        $cart_item_quantity_count = $cart_item['quantity'];
        if ( $cart_item_quantity_count > 1 ) :
            $quantity_html = '<br> <span class="product-quantity">' . __('Quantity', 'woocommerce') . ': <strong>' . $cart_item['quantity'] . '</strong></span>';
        else:
            $quantity_html = '';
        endif;
    
        return $quantity_html;
    
    }
    

  2. To check if there is more than one product per order, I’d go for the following condition:

    if ( WC()->cart->get_cart_contents_count() > 1 ) {
         // more than 1 product per order
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search