本文介绍了Woocommerce中的本地取件运输选项自定义百分比折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的WooCommerce结帐页面提供了一些送货选项:
My WooCommerce checkout page gives some shipping options:
- 统一费率(要花钱)
- 本地取件(免费).
如果客户选择本地取件运输方式,我如何给总订单成本提供5%的折扣?
How can I give a 5% discount on total order cost if a customer chooses Local Pickup shipping method?
推荐答案
以下代码将为本地取货选择的运输方式为购物车小计增加5%的折扣:
The following code will add a discount of 5% to cart subtotal for Local Pickup chosen shipping method:
add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_method', 10, 1 );
function custom_discount_for_pickup_shipping_method( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 5; // <=== Discount percentage
$chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
$chosen_shipping_method = explode(':', $chosen_shipping_method_id)[0];
// Only for Local pickup chosen shipping method
if ( strpos( $chosen_shipping_method_id, 'local_pickup' ) !== false ) {
// Calculate the discount
$discount = $cart->get_subtotal() * $percentage / 100;
// Add the discount
$cart->add_fee( __('Pickup discount') . ' (' . $percentage . '%)', -$discount );
}
}
代码进入您的活动子主题(活动主题)的function.php文件中.经过测试,可以正常工作.
Code goes in function.php file of your active child theme (active theme). Tested and works.
这篇关于Woocommerce中的本地取件运输选项自定义百分比折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!