php点击按钮加入购物车,php – 在WooCommerce中添加到购物车后,Relabel“添加到购物车”按钮...

我想在点击它之后重新标记我的添加到购物车按钮并将一个项目添加到购物车再添加一个到购物车.

这可能吗?

我有Child-Theme function.php,第二个转到购物车按钮,这是有效的.

但是我不知道如何在将一件物品添加到购物车后解决这个重新标签(商店只销售一件不同尺寸的商品).我希望我很清楚.

这是我的代码:

add_filter( 'woocommerce_product_add_to_cart_text',

'customizing_add_to_cart_button_text', 10, 2 );

add_filter( 'woocommerce_product_single_add_to_cart_text',

'customizing_add_to_cart_button_text', 10, 2 );

function customizing_add_to_cart_button_text( $button_text, $product )

{

if ( WC()->cart->get_cart_contents_count() )

return __( 'Add one more to cart', 'woocommerce' );

} else {

return __( 'Add to cart ', 'woocommerce' );

}

解决方法:

更新:您将在下面找到使此重新标记工作的正确条件:

add_filter( 'woocommerce_product_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );

add_filter( 'woocommerce_product_single_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );

function customizing_add_to_cart_button_text( $button_text, $product )

{

$is_in_cart = false;

foreach ( WC()->cart->get_cart() as $cart_item )

if ( $cart_item['product_id'] == $product->get_id() ) {

$is_in_cart = true;

break;

}

if( $is_in_cart )

$button_text = __( 'Add one more to cart', 'woocommerce' );

return $button_text;

}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.

经过测试和工作.

如果你已经启用了Ajax,对于档案页面上的非变量产品(如商店页面或产品类别页面)而你想要获得这个实况事件,你也应该添加它:

add_action('wp_footer','custom_jquery_add_to_cart_script');

function custom_jquery_add_to_cart_script(){

if ( is_shop() || is_product_category() || is_product_tag() ): // Only for archives pages

$new_text = __( 'Add one more to cart', 'woocommerce' );

?>

// Ready state

(function($){

$('a.add_to_cart_button').click( function(){

$this = $(this);

$( document.body ).on( 'added_to_cart', function(){

$($this).text('<?php echo $new_text; ?>');

console.log('EVENT: added_to_cart');

});

});

})(jQuery); // "jQuery" Working with WP (added the $alias as argument)

endif;

}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.

经过测试和工作.

标签:php,jquery,ajax,wordpress,woocommerce

来源: https://codeday.me/bug/20191002/1843518.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值