2

我有一个不知道如何解决自己的小问题。我想将此逻辑用于我的 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$优惠券折扣等..

我搜索了,发现了这个有用的线程,但情况和我的有点不同。

我怎样才能拥有特定的优惠券?一些建议或起点。谢谢

4

1 回答 1

1

这可以通过 2 个步骤实现:

1)添加一个独特的优惠券:

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

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 文件中。测试和工作。

于 2018-07-04T00:30:19.133 回答