如何获得woocommerce优惠券折扣以首先从价格最低的商品进行计算

问题描述

Woocommerce优惠券%折扣(对x个数量的商品有限制),从价格最高的商品开始计算折扣,而不是从价格最低的商品开始。

我正在尝试更改此认设置,以便可以从价格最低的商品中计算优惠券折扣。我发现了这个Finding Lowest Price in Woocommerce Cart Items,但是代码已过时,导致PHP错误

我发现该代码已在class-wc-discounts.PHP中引用

$coupon_amount = $coupon->get_amount();

    foreach ( $items_to_apply as $item ) {
        // Find out how much price is available to discount for the item.
        $discounted_price = $this->get_discounted_price_in_cents( $item );

        // Get the price we actually want to discount,based on settings.
        $price_to_discount = ( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially','no' ) ) ? $discounted_price : round( $item->price );

        // See how many and what price to apply to.
        $apply_quantity    = $limit_usage_qty && ( $limit_usage_qty - $applied_count ) < $item->quantity ? $limit_usage_qty - $applied_count : $item->quantity;
        $apply_quantity    = max( 0,apply_filters( 'woocommerce_coupon_get_apply_quantity',$apply_quantity,$item,$coupon,$this ) );
        $price_to_discount = ( $price_to_discount / $item->quantity ) * $apply_quantity;

任何人都可以提供任何帮助并提供一些指导吗?

解决方法

经过数小时的浏览代码后,答案确实在class-wc-discounts.php中。

这是原始代码:

    /**
 * Sort by price.
 *
 * @since  3.2.0
 * @param  array $a First element.
 * @param  array $b Second element.
 * @return int
 */
protected function sort_by_price( $a,$b ) {
    $price_1 = $a->price * $a->quantity;
    $price_2 = $b->price * $b->quantity;
    if ( $price_1 === $price_2 ) {
        return 0;
    }
    return ( $price_1 < $price_2 ) ? 1 : -1;
}

所以我只是将这行( $price_1 < $price_2 ) ? 1 : -1;更改为( $price_1 < $price_2 ) ? -1 : 1;,然后从最低价格到最高价格进行了排序。

不确定这是否是最好的把戏,但问题解决了。