【问题标题】:Add To Cart button Text Change Based on Discount添加到购物车按钮文本更改基于折扣
【发布时间】:2019-03-06 18:13:21
【问题描述】:

这会将文本更改为“再次添加?”当客户单击“添加到购物车”时,我想知道如何将其更改为每次单击按钮时应用折扣。

代码:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_add_to_cart_again' );
function woo_add_to_cart_again() {

    global $woocommerce;
    foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        if( get_the_ID() == $_product->id ) {
            return __('Add Again', 'woocommerce');
        }
    }
    return __('Add to cart', 'woocommerce');
}

第一次点击时,像往常一样加入购物车,但将按钮文字更改为“再次购买并获得 5% 折扣”,第二次点击后,文字变为“购买 3 次并获得 10% 折扣”,然后折扣应适用于产品,而不是购物车。

我希望在创建新产品时,在 WooCommerce Admin 中的“可下载”旁边有一个复选框选项。非常感谢任何帮助。

【问题讨论】:

  • 基本上你想检查购物车中的产品是否等于 1 然后按摩将再次购买并获得 5% 折扣如果购物车中的 2“购买 3 并获得 10% 折扣”正确或你想要它在单击时添加事件侦听器?然后相应地添加折扣
  • 只要按钮文本根据购物车中的数量发生变化并应用折扣,任何一种方式对我来说都可以。

标签: woocommerce


【解决方案1】:

首先根据数量更改文本,我刚刚向您的函数添加了一个 if 条件,以检查购物车中特定产品的数量并相应地更改文本,如下所示:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_add_to_cart_again' );
function woo_add_to_cart_again() {

    global $woocommerce;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        if ( $values['quantity'] == 1 && get_the_ID() == $_product->id ) {
            return __( 'Buy again & Get 5% OFF', 'woocommerce' );
        }

        if ( $values['quantity'] == 2 && get_the_ID() == $_product->id ) {
            return __( 'Buy 3 and get 10% OFF', 'woocommerce' );
        }
        if ( $values['quantity'] > 2 && get_the_ID() == $_product->id ) {
            return __( 'Add Again', 'woocommerce' );
        }
    }
    return __( 'Add to cart', 'woocommerce' );
}

注意:如果你也想改变存档页面中的文字,你可以使用woocommerce_product_add_to_cart_text钩子并将其添加到上面的代码中,如下所示:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_add_to_cart_again' );
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_add_to_cart_again' );

然后我们需要计算那些符合折扣条件的产品的价格,并根据产品价格添加折扣。

我们将使用woocommerce_cart_calculate_fees 将此折扣插入我们的购物车,如下所示:

add_action( 'woocommerce_cart_calculate_fees', 'add_discount' );


function add_discount( WC_Cart $cart ) {

    $discounts = []; //Store the disocunts in array 
    foreach ( $cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        if ( $values['quantity'] == 2 ) {
            // Calculate the discount amount  
            $product_price = ( $_product->get_sale_price() ) ? $_product->get_sale_price() : $_product->get_regular_price();
            $price         = $product_price * 2;
            array_push( $discounts, $price * 0.05 ); //Push the discount 

        }

        if ( $values['quantity'] > 2 ) {
            // Calculate the discount amount  
            $product_price = ( $_product->get_sale_price() ) ? $_product->get_sale_price() : $_product->get_regular_price();
            $price         = $product_price * $values['quantity'];
            $discount      = $price * 0.1;
            array_push( $discounts, $price * 0.1 );//Push the discount 
        }
    }

    $cart->add_fee( 'Your Disocunt', -array_sum( $discounts ) );

}

更新:

要在使用 Ajax 时更改 add_to_cart 文本,实际上它已经在这里由@LoicTheAztec 在此link 回答,只需稍作修改,您就可以实现如下目标:

add_action( 'wp_footer', 'change_add_to_cart_jquery' );
function change_add_to_cart_jquery() {
    if ( is_shop() || is_product_category() || is_product_tag() ) : ?>
            <script type="text/javascript">
            (function ($) {
                $('a.add_to_cart_button').click(function () {
                    $this = $(this);
                    if (typeof $($this).attr('data-qty') == 'undefined') {
                        $($this).text('Buy again & Get 5% OFF');
                        $($this).attr('data-qty', 1);
                        return;
                    }
                    if ($($this).attr('data-qty') == 1) {
                        $($this).text('Buy 3 and get 10% OFF');
                        $($this).attr('data-qty', 2);
                        return
                    }
                    $($this).text('Add Again');
                });

            })(jQuery);
            </script>
        <?php
        endif;
}

当然,上面的代码已经过测试。

【讨论】:

  • 谢谢!效果很好。如果在存档上更改按钮而无需更新(刷新)页面,那就更好了。但那是小事。再次谢谢你。如何使它成为一个复选框按钮,以便可以根据产品“激活”该功能?正如我在最初的问题中提到的那样:“在创建新产品时,我希望它成为 WooCommerce Admin 中“可下载”旁边的复选框选项。”
  • @bjornen 不客气,我会考虑简单的解决方案,给我 10 分钟
  • 确定。我会等 :) 感谢您抽出时间来帮助我。
  • @bjornen 我已经更新了我的答案,抱歉耽搁了:)
  • 酷!我没有刷新 :) 谢谢你,非常感谢 LoicTheAztec。它工作正常:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-21
  • 2021-01-30
  • 1970-01-01
  • 2015-01-25
  • 1970-01-01
  • 2018-03-03
  • 1970-01-01
相关资源
最近更新 更多