简体   繁体   English

当产品在 Woocommerce 的购物车中时,更改添加到购物车按钮样式

[英]Change add to cart button style when product is in cart in Woocommerce

I have an icon (instead of text) in my add to cart button.我的添加到购物车按钮中有一个图标(而不是文本)。 I added in the add-to-cart.php file an icon class to the add-to-cart anchor, as you can see here:我在 add-to-cart.php 文件中向 add-to-cart 锚点添加了一个图标类,如下所示:

echo apply_filters( 'woocommerce_loop_add_to_cart_link',
    sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s fa fa-cart-plus">%s</a>' 

and I would like to change his color in case the item is already in cart.如果商品已经在购物车中,我想更改他的颜色。
I have a function to get an add-to-cart button without any text, and as well I get the items id that are in cart:我有一个功能可以在没有任何文本的情况下获取添加到购物车按钮,而且我还获取了购物车中的商品 ID:

add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' );    // 2.1 +
function woo_archive_custom_cart_button_text() {

    global $woocommerce;

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

        if( get_the_ID() == $_product->id ) {
            return __('', 'woocommerce');   
         }
    }
    return __( '', 'woocommerce' );

which is great, but for some reason I can't think of a way to use this new information with changing the icon style.这很好,但出于某种原因,我想不出一种方法来使用这些新信息来更改图标样式。 I've tried to 'echo' a new style, but I don't know how to relate to the product id in css (or in jquery).我试图“回应”一种新样式,但我不知道如何与 css(或 jquery)中的产品 id 相关联。 Any ideas?有任何想法吗?


*There might be a different way to do this. *可能有不同的方法来做到这一点。 When an item is added to cart, the add-to-cart anchor has a new class- 'added', so I can easily customize it with css, and it works, but the 'added' class is there only when the item is being added.当一个项目被添加到购物车时,添加到购物车的锚点有一个新的类——“添加”,所以我可以很容易地用 css 自定义它,并且它可以工作,但是“添加”类只在项目被添加时才存在正在添加。 after refreshing the page it doesn't exist, therefore the customize I've done doesn't affect anymore.刷新页面后它不存在,因此我所做的自定义不再影响。

You should need to make some changes in the template loop/add-to-cart.php directly has you have already done, replacing them by:您应该需要直接在模板loop/add-to-cart.php进行一些更改,将它们替换为:

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

global $product;

// Your icon class is now here
$add_class = ' fa fa-cart-plus';

// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item )
    // If the product is in cart
    if( $product->get_id() == $cart_item['product_id'] ) {
        $add_class .= ' is-added'; // We add an additional class
        break;
    }

$add_to_cart_text = '';

echo apply_filters( 'woocommerce_loop_add_to_cart_link',
    sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        esc_attr( $product->get_id() ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $class ) ? $class : 'button' ) . $add_class,
        $add_to_cart_text
    ),
$product );

This will add an additional is-added to your button, that you will be able to target with your CSS to make a color change.这将为您的按钮添加一个附加的is-added add ,您将能够使用 CSS 定位以进行颜色更改。 This is tested and works.这是经过测试并有效的。

You will not need anymore your function woo_archive_custom_cart_button_text()你将不再需要你的函数woo_archive_custom_cart_button_text() ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果产品在购物车中,则更改“添加到购物车”按钮文本,并在 WooCommerce 中满足条件时重定向到购物车页面 - Change "add to cart" button text if product is in cart and redirect to cart page when condition is met in WooCommerce WooCommerce:当产品已在购物车中时更改添加到购物车的文本 - WooCommerce: change the add to cart text when the product is already in cart Woocommerce:购物车中已有物品时,更改“添加到购物车”按钮的颜色 - Woocommerce: Change “add to cart” button color when something is already in the cart Woocommerce将购物车风格添加为产品页面 - Woocommerce Add to cart style as product page 根据Woocommerce中的父产品类别更改“添加到购物车”按钮文本 - Change Add To Cart button text based on parent product categories in Woocommerce 根据 WooCommerce 产品类型更改添加到购物车按钮和文本 - Change add to cart button and text based on WooCommerce product type Woocommerce-当用户是产品作者时,删除添加到购物车按钮 - Woocommerce - Remove add to cart button when user is the product author 在WooCommerce中隐藏产品可见性时,隐藏“添加到购物车”按钮 - Hide add to cart button when product visibility is hidden in WooCommerce Woocommerce - 当产品处于缺货状态时禁用 add_to_cart 按钮 - Woocommerce - disable add_to_cart button when product is on backorder 如果产品在购物车中,则在woocommerce中添加类以添加到购物车按钮 - if product is in cart add class to add to cart button in woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM