我想重新命名我的,添加到购物车按钮后,点击它,并添加一个项目到购物车到,再添加一个到购物车。
这个是可能的吗?
我有儿童主题function.php和第二个到cart按钮,这是工作的。
但我不知道如何解决这个重新标签后,一个项目已添加到购物车(商店只出售一个不同大小的项目)。我希望我能说清楚。
这是我的代码:
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' );
}发布于 2017-11-24 13:28:49
更新:在下面的中,您将找到正确的条件来使此重新标记工作:
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' );
?>
<script type="text/javascript">
// 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)
</script>
<?php
endif;
}代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。
测试和工作。
发布于 2017-11-24 12:07:16
在主题/woocommerce/loop文件夹中添加一个add-to-cart.php。然后在sprintf按钮函数之前添加下面的代码。
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$inCart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if($values['product_id'] == $product->id && $values['quantity'] > 1){
$inCart = true;
break;
}
}
$buttonText = $inCart?'add one more':'add to cart';将sprintf函数中的最后一行更改为
esc_html( ( !empty( $product_addons ) )?'Select options':$buttonText )如果您打开Ajax购物车,您可能需要改进这一点。
发布于 2021-08-01 22:11:37
这应该能行
// Part 1
// Single Product Page Add to Cart
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_cart_button_single_product', 9999 );
function custom_add_cart_button_single_product( $label ) {
if ( WC()->cart && ! WC()->cart->is_empty() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
if ( get_the_ID() == $product->get_id() ) {
$label = 'Added to Cart';
break;
}
}
}
return $label;
}
// Part 2
// Loop Pages Add to Cart
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_cart_button_loop', 9999, 2 );
function custom_add_cart_button_loop( $label, $product ) {
if ( $product->get_type() == 'simple' && $product->is_purchasable() && $product->is_in_stock() ) {
if ( WC()->cart && ! WC()->cart->is_empty() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( get_the_ID() == $_product->get_id() ) {
$label = 'Added to Cart';
break;
}
}
}
}
return $label;
}https://stackoverflow.com/questions/47471293
复制相似问题