【问题标题】:Relabel "add to cart" button after add to cart in WooCommerce在 WooCommerce 中添加到购物车后重新标记“添加到购物车”按钮
【发布时间】:2018-05-08 08:26:42
【问题描述】:

我想在点击后重新标记我的添加到购物车按钮并将一件商品添加到购物车到再添加一件到购物车

这可能吗?

我有 Child-Theme function.php 和第二个 go to 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' );
}

【问题讨论】:

  • Is this possible?是的。 But i do not know how to solve the relabel thing 向我们展示你目前的尝试?
  • 购物车是用ajax还是刷新页面?
  • 现在正在使用刷新
  • 大声笑,真的吗?你试过只提神吗?没有代码?
  • 抱歉购物车正在使用 ajax。但我正在尝试在不在购物车中的单个产品页面上重新标记我的按钮。

标签: php jquery ajax wordpress woocommerce


【解决方案1】:

将 add-to-cart.php 添加到您的主题/woocommerce/loop 文件夹。然后在 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 购物车,您可能需要对此进行改进

【讨论】:

  • 我尝试了一些不同的方法,但它不起作用。我将它发布到我原来的问题中。我会试试你的代码。谢谢
  • @Christian 没问题。它可能需要一些改进以适合您的模板,但它应该可以工作。您应该能够将相同的逻辑应用于您的过滤器。
【解决方案2】:

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

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 文件或任何插件文件中。

经过测试并且有效。

【讨论】:

  • 我刚刚对其进行了测试,但它不适用于我的情况。也许是因为它是一个可变产品? (不同尺码的衬衫)
  • @Christian 我已经更新了我的代码……它现在适用于可变产品……请尝试并告诉我。
  • 这里发布了所有答案,这适用于可变产品,并且截至今天仍然有效。
  • 我想知道是否可以向重新标记的按钮添加一个 CSS 类,使其样式不同于默认的“添加到购物车”按钮?
【解决方案3】:

这应该可以工作

 // 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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    相关资源
    最近更新 更多