且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-23 22:34:09

如果您已经完成操作,则应该直接在模板 loop/add-to-cart.php 中进行一些更改,替换为:

You should need to make some changes in the template loop/add-to-cart.php directly has you have already done, replacing them by:

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 );

这将向您的按钮添加一个附加的已添加,您将可以使用CSS进行定位以进行颜色更改.这已经过测试并且可以正常工作.

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. This is tested and works.

您将不再需要函数 woo_archive_custom_cart_button_text()