在 Woocommerce 3.2+ 中以编程方式向订单添加折扣
Add a discount programmatically to an Order in Woocommerce 3.2+
在 woocommerce 中,我们可以使用优惠券功能(固定金额、百分比金额……)为任何订单添加折扣。
是否可以在折扣金额可以是任意金额的情况下以编程方式将折扣金额添加到任何订单?
如有任何帮助,我们将不胜感激。
The only available feature to make a discount programmatically for an Order, is tricking the Fee API. For info, this trick is not recommended by woocommerce, but used by many people as there is not a discount feature in Woocommerce outside Coupons.
以下功能可让您进行任意金额的固定折扣或百分比折扣。订单需要存在(之前保存).
函数代码(对于Woocommerce 3.2+版本):
/**
* Add a discount to an Orders programmatically
* (Using the FEE API - A negative fee)
*
* @since 3.2.0
* @param int $order_id The order ID. Required.
* @param string $title The label name for the discount. Required.
* @param mixed $amount Fixed amount (float) or percentage based on the subtotal. Required.
* @param string $tax_class The tax Class. '' by default. Optional.
*/
function wc_order_add_discount( $order_id, $title, $amount, $tax_class = '' ) {
$order = wc_get_order($order_id);
$subtotal = $order->get_subtotal();
$item = new WC_Order_Item_Fee();
if ( strpos($amount, '%') !== false ) {
$percentage = (float) str_replace( array('%', ' '), array('', ''), $amount );
$percentage = $percentage > 100 ? -100 : -$percentage;
$discount = $percentage * $subtotal / 100;
} else {
$discount = (float) str_replace( ' ', '', $amount );
$discount = $discount > $subtotal ? -$subtotal : -$discount;
}
$item->set_tax_class( $tax_class );
$item->set_name( $title );
$item->set_amount( $discount );
$item->set_total( $discount );
if ( '0' !== $item->get_tax_class() && 'taxable' === $item->get_tax_status() && wc_tax_enabled() ) {
$tax_for = array(
'country' => $order->get_shipping_country(),
'state' => $order->get_shipping_state(),
'postcode' => $order->get_shipping_postcode(),
'city' => $order->get_shipping_city(),
'tax_class' => $item->get_tax_class(),
);
$tax_rates = WC_Tax::find_rates( $tax_for );
$taxes = WC_Tax::calc_tax( $item->get_total(), $tax_rates, false );
print_pr($taxes);
if ( method_exists( $item, 'get_subtotal' ) ) {
$subtotal_taxes = WC_Tax::calc_tax( $item->get_subtotal(), $tax_rates, false );
$item->set_taxes( array( 'total' => $taxes, 'subtotal' => $subtotal_taxes ) );
$item->set_total_tax( array_sum($taxes) );
} else {
$item->set_taxes( array( 'total' => $taxes ) );
$item->set_total_tax( array_sum($taxes) );
}
$has_taxes = true;
} else {
$item->set_taxes( false );
$has_taxes = false;
}
$item->save();
$order->add_item( $item );
$order->calculate_totals( $has_taxes );
$order->save();
}
代码进入您的活动子主题(活动主题)的 function.php 文件。已测试并有效。
用法示例:
1) </code></strong>的固定折扣</strong>(动态<code>$order_id
):
wc_order_add_discount( $order_id, __("Fixed discount"), 12 );
2) 5%
的折扣百分比(动态 $order_id
):
wc_order_add_discount( $order_id, __("Discount (5%)"), '5%' );
The amount (or the percentage) can be also a dynamic variable…
实际上你可以在计算税收等之前做个钩子,得到小计,应用折扣,完成:)
它自动申请订单消息等,它也以适当的方式在后端可见。去掉挂钩后,订单信息中仍保留折扣信息
// Hook before calculate fees
add_action('woocommerce_cart_calculate_fees' , 'add_user_discounts');
/**
* Add custom fee if more than three article
* @param WC_Cart $cart
*/
function add_user_discounts( WC_Cart $cart ){
//any of your rules
// Calculate the amount to reduce
$discount = $cart->get_subtotal() * 0.5;
$cart->add_fee( 'Test discount 50%', -$discount);
}
在 woocommerce 中,我们可以使用优惠券功能(固定金额、百分比金额……)为任何订单添加折扣。
是否可以在折扣金额可以是任意金额的情况下以编程方式将折扣金额添加到任何订单?
如有任何帮助,我们将不胜感激。
The only available feature to make a discount programmatically for an Order, is tricking the Fee API. For info, this trick is not recommended by woocommerce, but used by many people as there is not a discount feature in Woocommerce outside Coupons.
以下功能可让您进行任意金额的固定折扣或百分比折扣。订单需要存在(之前保存).
函数代码(对于Woocommerce 3.2+版本):
/**
* Add a discount to an Orders programmatically
* (Using the FEE API - A negative fee)
*
* @since 3.2.0
* @param int $order_id The order ID. Required.
* @param string $title The label name for the discount. Required.
* @param mixed $amount Fixed amount (float) or percentage based on the subtotal. Required.
* @param string $tax_class The tax Class. '' by default. Optional.
*/
function wc_order_add_discount( $order_id, $title, $amount, $tax_class = '' ) {
$order = wc_get_order($order_id);
$subtotal = $order->get_subtotal();
$item = new WC_Order_Item_Fee();
if ( strpos($amount, '%') !== false ) {
$percentage = (float) str_replace( array('%', ' '), array('', ''), $amount );
$percentage = $percentage > 100 ? -100 : -$percentage;
$discount = $percentage * $subtotal / 100;
} else {
$discount = (float) str_replace( ' ', '', $amount );
$discount = $discount > $subtotal ? -$subtotal : -$discount;
}
$item->set_tax_class( $tax_class );
$item->set_name( $title );
$item->set_amount( $discount );
$item->set_total( $discount );
if ( '0' !== $item->get_tax_class() && 'taxable' === $item->get_tax_status() && wc_tax_enabled() ) {
$tax_for = array(
'country' => $order->get_shipping_country(),
'state' => $order->get_shipping_state(),
'postcode' => $order->get_shipping_postcode(),
'city' => $order->get_shipping_city(),
'tax_class' => $item->get_tax_class(),
);
$tax_rates = WC_Tax::find_rates( $tax_for );
$taxes = WC_Tax::calc_tax( $item->get_total(), $tax_rates, false );
print_pr($taxes);
if ( method_exists( $item, 'get_subtotal' ) ) {
$subtotal_taxes = WC_Tax::calc_tax( $item->get_subtotal(), $tax_rates, false );
$item->set_taxes( array( 'total' => $taxes, 'subtotal' => $subtotal_taxes ) );
$item->set_total_tax( array_sum($taxes) );
} else {
$item->set_taxes( array( 'total' => $taxes ) );
$item->set_total_tax( array_sum($taxes) );
}
$has_taxes = true;
} else {
$item->set_taxes( false );
$has_taxes = false;
}
$item->save();
$order->add_item( $item );
$order->calculate_totals( $has_taxes );
$order->save();
}
代码进入您的活动子主题(活动主题)的 function.php 文件。已测试并有效。
用法示例:
1) </code></strong>的固定折扣</strong>(动态<code>$order_id
):
wc_order_add_discount( $order_id, __("Fixed discount"), 12 );
2) 5%
的折扣百分比(动态 $order_id
):
wc_order_add_discount( $order_id, __("Discount (5%)"), '5%' );
The amount (or the percentage) can be also a dynamic variable…
实际上你可以在计算税收等之前做个钩子,得到小计,应用折扣,完成:) 它自动申请订单消息等,它也以适当的方式在后端可见。去掉挂钩后,订单信息中仍保留折扣信息
// Hook before calculate fees
add_action('woocommerce_cart_calculate_fees' , 'add_user_discounts');
/**
* Add custom fee if more than three article
* @param WC_Cart $cart
*/
function add_user_discounts( WC_Cart $cart ){
//any of your rules
// Calculate the amount to reduce
$discount = $cart->get_subtotal() * 0.5;
$cart->add_fee( 'Test discount 50%', -$discount);
}