Php 本地皮卡运输选项在Woocommerce中的自定义百分比折扣
我的WooCommerce结账页面提供了一些配送选项:Php 本地皮卡运输选项在Woocommerce中的自定义百分比折扣,php,wordpress,woocommerce,discount,shipping-method,Php,Wordpress,Woocommerce,Discount,Shipping Method,我的WooCommerce结账页面提供了一些配送选项: 统一费率(费用) 本地接送(免费) 如果客户选择本地提货运输方式,我如何对订单总成本给予5%的折扣?以下代码将为本地提货运输方式的购物车小计增加5%的折扣: add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_method', 10, 1 ); function custom_discount_for_pickup_shi
- 统一费率(费用)
- 本地接送(免费)
如果客户选择本地提货运输方式,我如何对订单总成本给予5%的折扣?以下代码将为本地提货运输方式的购物车小计增加5%的折扣:
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 );
}
}
add_action('woocommerce_cart_计算_费用','custom_折扣_取货_运输_方法',10,1);
功能自定义\取货\发货\折扣\方法($cart){
if(定义了('DOING'uajax'))
返回;
$percentage=5;//会话->获取('selected_shipping_methods')[0];
$selected_shipping_method=explode(“:”,$selected_shipping_method_id)[0];
//仅适用于本地提货选择的运输方式
if(strpos($selected\u shipping\u method\u id,'local\u pickup')!==false){
//计算折扣
$discount=$cart->get_subtotal()*$percentage/100;
//加上折扣
$cart->add_费用(uuu('皮卡折扣')。('.$percentage.%'),-$折扣);
}
}
代码进入活动子主题(活动主题)的function.php文件。测试和工作
要刷新付款方式更改的结帐“订单审核”部分,请参阅:@moneeb我已经更新了我的答案,并添加了一个类似代码的链接,该代码刷新了关于付款方式变更的订单审查部分。所以你现在可以删除这个评论了。