Mostafa AHmadi
Mostafa AHmadi

Reputation: 335

Woocommerce: Change “add to cart” button color when something is already in the cart

I want to change the color of the "add to cart" button when something is already in the cart.
I think that it is possible when a product is already in the cart to get a new class "added" or something else.

Could anyone help me with that and knows how to achieve this?

Upvotes: 1

Views: 450

Answers (1)

Darsh khakhkhar
Darsh khakhkhar

Reputation: 676

Try this

function woo_in_cart($product_id) {
    global $woocommerce;

    foreach($woocommerce->cart->get_cart() as $key => $val ) {
        $_product = $val['data'];

        if($product_id == $_product->id ) {
            return true;
        }
    }

    return false;
}

To use it just pass the product id in a function it will return true if the item has been already in cart and will return false if item is not in the cart..

suppose the product id is "123"..

if(woo_in_cart(123)) {
  // Product is already in cart
  // add a custom class to cart button.
}

Upvotes: 1

Related Questions