1 回答
TA贡献1863条经验 获得超2个赞
要使其仅在结帐页面上激活,请使用以下命令:
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;
// Only on checkout page
if ( is_checkout() ) {
$percentage = 2; // <=== 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 文件中。经过测试并有效。
- 1 回答
- 0 关注
- 76 浏览
添加回答
举报