Woocommerce display graphics based on number of items in cart

Hi,

This is for woo commerce or any shopping cart that uses PHP…

Would anyone know a way of having a different graphic show up which is based on the number items in the cart?

E.g.: A picture of a box… and if they add an item it will change to show a box with a number 1… and a number 2… up to say 10?

Each could be a different graphic, or a table with a graphic and room for the text (number) in the centre?

If anyone has done anything similar in the past I’d appreciate the help. I can post some cart code also.

Dan

Yes, this is possible. You could use PHP to create a function that checks the number of items in the cart and then set up a switch statement that will display the appropriate image based on the number of items in the cart. You can also use css to style the images and text accordingly.


function display_images_by_cart_items(){
    //Get number of items in cart
    $items_in_cart = WC()->cart->get_cart_contents_count();
    
    //Check how many items are in the cart
    if($items_in_cart == 0){
        //Display image 1
        echo '<img src="image1.jpg" />';
    } elseif($items_in_cart == 1) {
        //Display image 2
        echo '<img src="image2.jpg" />';
    } elseif($items_in_cart == 2){
        //Display image 3
        echo '<img src="image3.jpg" />';
    } elseif ($items_in_cart == 3){
        //Display image 4
        echo '<img src="image4.jpg" />';
    }
    
}

//Call the function
display_images_by_cart_items();
Sponsor our Newsletter | Privacy Policy | Terms of Service