【问题标题】:Do not apply coupon discount on backorder products in WooCommerce不要在 WooCommerce 中对缺货产品应用优惠券折扣
【发布时间】:2020-08-15 08:42:12
【问题描述】:

我正在尝试仅对有货的产品应用优惠券。

因此,如果我的购物车中有 5 件商品和 4 件有库存,则只有 4 件可以享受折扣,延期交货的产品将不会。

实际上我正在尝试更改“class-wc-discounts.php”文件中的默认“get_items_to_apply_coupon”函数。

我尝试在 foreach cicle 中计算当前产品的库存数量,然后我将 $item_to_apply->quantity 更改为库存产品和购物车产品之间的差异。

但如果我在 $->quantity 中输入“200”,折扣也不会改变

protected function get_items_to_apply_coupon( $coupon ) {
    $items_to_apply = array();

    foreach ( $this->get_items_to_validate() as $item ) {
        $item_to_apply = clone $item; // Clone the item so changes to this item do not affect the originals.

        if ( 0 === $this->get_discounted_price_in_cents( $item_to_apply ) || 0 >= $item_to_apply->quantity ) {
            continue;
        }

        if ( ! $coupon->is_valid_for_product( $item_to_apply->product, $item_to_apply->object ) && ! $coupon->is_valid_for_cart() ) {
            continue;
        }
        
            $items_to_apply[] = $item_to_apply;
        

    }



    return $items_to_apply;
}

【问题讨论】:

    标签: php wordpress woocommerce product coupon


    【解决方案1】:

    class-wc-discounts.php 包含

    在这个函数中,我们找到了woocommerce_coupon_get_discount_amount 过滤钩子。

    因此,当产品延期交货时,您可以返回 0 作为折扣

    function filter_woocommerce_coupon_get_discount_amount( $discount, $price_to_discount , $cart_item, $single, $coupon ) {    
        // On backorder
        if ( $cart_item['data']->is_on_backorder() ) {
            $discount = 0;
        }
    
        return $discount;
    }
    add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
    

    【讨论】:

      猜你喜欢
      • 2022-01-13
      • 2019-09-04
      • 2015-09-08
      • 2021-12-31
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 2020-08-12
      相关资源
      最近更新 更多