【问题标题】:Progressive fixed Coupon Discount based on specific product quantity in Woocommerce基于 Woocommerce 中特定产品数量的渐进式固定优惠券折扣
【发布时间】:2018-12-12 07:04:28
【问题描述】:

我有一个不知道如何解决自己的小问题。我想将此逻辑用于我的 Woocommerce 商店,仅针对一种产品。

我已经使用这样的链接自动应用优惠券代码并添加到购物车:

https://testsite.com/checkout/?add-to-cart=Product_ID&quantity=1&coupon=Coupon_Code

当数量为 1 时,这似乎有效。但我想要在点击之前的直接链接时自动放置的折扣 (30%),以使其动态化,例如:

  • 购物车页面数量增加到2个,优惠券自动计算2 x 30$ = 60$折扣,
  • 同一产品买3件,计算3 X 30$ = 90$优惠券折扣 等等..

我搜索,找到了这个usefull 线程,但情况与我的略有不同。

如何才能获得特定优惠券?一些建议或起点。谢谢

【问题讨论】:

    标签: php wordpress woocommerce coupon product-quantity


    【解决方案1】:

    这两个步骤是可能的:

    1) 添加独特的优惠券:

    • 在常规设置中 > 类型 = 固定产品折扣
    • 在常规设置中 > 金额 = 30
    • 在使用限制 > 产品中 ==> 设置您想要的产品(s)

    2) 添加此代码(您将在函数中设置优惠券代码(小写)):

    add_filter( 'woocommerce_coupon_get_discount_amount', 'custom_coupon_get_discount_amount', 10, 5 );
    function custom_coupon_get_discount_amount( $rounded_discount, $discounting_amount, $cart_item, $single, $coupon ){
    
        ## ---- Your settings ---- ##
    
        // Related coupons codes to be defined in this array (you can set many)
        $coupon_codes = array('30perqty');
    
        ## ------ The code ------- ##
    
        if ( $coupon->is_type('fixed_product') && in_array( $coupon->get_code(), $coupon_codes ) && $cart_item['quantity'] > 1 ) {
            if( in_array( $cart_item['product_id'], $coupon->get_product_ids() ) ){
                $discount = (float) $coupon->get_amount() * (int) $cart_item['quantity'];
                $round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
            }
        }
        return $rounded_discount;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 此代码完美运行,但是当使用直接链接时,例如:https://siteurl.com/checkout1/?add-to-cart=319&coupon=gss30 优惠券未显示在结帐页面中。为什么 ?见图片。 i.imgur.com/2q3HanP.png?1
    • @DavidCopper ...在您的问题中,您只询问了我的实际代码...但是您没有要求可以从您的自定义添加到购物车 URL 中将优惠券添加到购物车中的东西,因此您可以问一个新的相关问题。
    猜你喜欢
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2021-12-31
    • 2015-09-08
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    相关资源
    最近更新 更多