简体   繁体   English

Woocommerce:购物车中已有物品时,更改“添加到购物车”按钮的颜色

[英]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.

相关问题 WooCommerce:当产品已在购物车中时更改添加到购物车的文本 - WooCommerce: change the add to cart text when the product is already in cart 当产品在 Woocommerce 的购物车中时,更改添加到购物车按钮样式 - Change add to cart button style when product is in cart in Woocommerce 如果产品在购物车中,则更改“添加到购物车”按钮文本,并在 WooCommerce 中满足条件时重定向到购物车页面 - Change "add to cart" button text if product is in cart and redirect to cart page when condition is met in WooCommerce 如果产品已经在 WooCommerce 购物车中,则禁用添加到购物车按钮 - Disable add to cart button if product is already in WooCommerce cart 如果产品已经在购物车中,则禁用Woocommerce添加到购物车按钮 - Disable Woocommerce add to cart button if the product is already in cart WooCommerce-产品已经在购物车中,更改“添加到购物车”文本为ajax - WooCommerce - Product already in cart, change “add to cart” text ajax 在Woocommerce中单击后,将“添加到购物车”按钮更改为“查看购物车”按钮 - Change Add to Cart Button to View Cart Button after click in Woocommerce 删除Woocommerce中的“添加到购物车”通知并更改“添加到购物车”按钮 - Remove add to cart notice and change “add to cart” button in Woocommerce Woocommerce:更改“添加到购物车”按钮的行为 - Woocommerce: change behaviour of “Add to Cart” button 当产品已经在购物车中并且产品标签为Buynow时,将“添加到购物车”按钮文本更改为“添加” - Change Add to cart button text to Added when product already in cart and product tag is buynow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM