WooCommerce: Check the number of different cart items / SKUs in cart

441 Views Asked by At

I want to show a checkout button above the cart if there are more than one items (different SKUs) in it.

I found a way to hide the button of there is only one item in the cart. The problem is, that multiple versions of the same product (SKU) counts as an own item.

If I'm using the following code, it shows the button even if there is single product two times in the cart:

<?php if ( WC()->cart->get_cart_contents_count() > 1 ) : ?>
    <?php woocommerce_button_proceed_to_checkout(); ?>
<?php endif; ?>

Is there a way to check it based on the items / SKUs?

1

There are 1 best solutions below

0
On BEST ANSWER

You can use the following, that will get the count of different product(s) sku(s) in cart:

<?php 
$skus = array();
foreach( WC()->cart->get_cart() as $cart_item ) :
    $sku = $cart_item['data']->get_sku();
    $skus[$sku] = $cart_item['data']->get_id();
endforeach;
 
if( count($skus) > 1 ) : 
    woocommerce_button_proceed_to_checkout();
endif; ?>

It should work.