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