[英]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? 谁能帮助我并且知道如何实现这一目标?
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.. 要使用它,只需在函数中传递产品ID,如果该商品已经在购物车中,它将返回true;如果该商品不在购物车中,则将返回false。
suppose the product id is "123".. 假设产品ID为“ 123”。
if(woo_in_cart(123)) {
// Product is already in cart
// add a custom class to cart button.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.