WC_Discounts::apply_coupon_custom()
Apply custom coupon discount to items.
Метод класса: WC_Discounts{}
Возвращает
int
. Total discounted.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->apply_coupon_custom( $coupon, $items_to_apply );
- $coupon(WC_Coupon) (обязательный)
- Coupon object. Passed through filters.
- $items_to_apply(массив) (обязательный)
- Array of items to apply the coupon to.
Список изменений
С версии 3.3 | Введена. |
Код WC_Discounts::apply_coupon_custom() WC Discounts::apply coupon custom WC 9.5.1
protected function apply_coupon_custom( $coupon, $items_to_apply ) { $limit_usage_qty = 0; $applied_count = 0; if ( null !== $coupon->get_limit_usage_to_x_items() ) { $limit_usage_qty = $coupon->get_limit_usage_to_x_items(); } // Apply the coupon to each item. 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 = wc_remove_number_precision( ( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ) ? $discounted_price : $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 ) ); // Run coupon calculations. $discount = wc_add_number_precision( $coupon->get_discount_amount( $price_to_discount / $item->quantity, $item->object, true ) ) * $apply_quantity; $discount = wc_round_discount( min( $discounted_price, $discount ), 0 ); $applied_count = $applied_count + $apply_quantity; // Store code and discount amount per item. $this->discounts[ $coupon->get_code() ][ $item->key ] += $discount; } // Allow post-processing for custom coupon types (e.g. calculating discrepancy, etc). $this->discounts[ $coupon->get_code() ] = apply_filters( 'woocommerce_coupon_custom_discounts_array', $this->discounts[ $coupon->get_code() ], $coupon ); return array_sum( $this->discounts[ $coupon->get_code() ] ); }