当商品价格低于折扣金额时,Woocommerce 优惠券折扣

时间:2021-05-27 00:35:25

标签: php wordpress woocommerce

根据我对 woocommerce_coupon_get_discount_amount 工作原理的理解。折扣是根据购物车中每件商品的折扣来计算的。例如,如果您的购物车中有 4 件商品,价格为 10 美元,并且您要打折一件商品才能免费。然后,您将向购物车中的每个产品返还 2.50 美元。从而将其中一种产品打折 100%。

但是,假设您在购物车中的产品价格为 1 美元,那么应用的折扣不是 10 美元,而是 8.50 美元。使折扣不正确。

对于上面的例子,优惠券的计算方式如下:

add_filter('woocommerce_coupon_get_discount_amount', 'mb_wcchpi_cpn_disc', 10, 5);
function mb_wcchpi_cpn_disc($discount, $discounting_amount, $cart_item, $single, $coupon) 
{
    //IF CUSTOM COUPON TYPE
    if ($coupon->type == 'most_expensive_coupon') {       
        //Gets the most expensive item in the cart
        $mostExpensiveItem = FindMaxPricedItem($multiple_products);
        
        //Get the price of the most expensive item in the cart
        $maxPrice = $mostExpensiveItem['price'];

        //GET THE COUPON DISCOUNT AMOUNT
        $amt = floatval($coupon->amount);

        //GET THE DISCOUNT AMOUNT
        $discount = ($amt / 100) * $maxPrice;

        //GET THE ITEMS IN THE CART
        $itemCount = WC()->cart->cart_contents_count;

        //DISTRIBUTE EQUALLY ACROSS ALL ITEMS IN CART
        $discount = $discount / $itemCount;
    }

    return $discount;

}

如何处理购物车中的商品低于折扣金额的极端情况?是否有与 woocommerce_coupon_get_discount_amount 不同的操作用于将优惠券应用于购物车总数而不是每件商品?我应该使用不同的公式来计算折扣吗?

0 个答案:

没有答案