英文:
Discount for specific shipping methods in WooCommerce checkout
问题
以下是您要翻译的内容:
"我想对一些本地自取的运输方式提供折扣。使用下面的代码,我会在所有运输方式上获得折扣(包括定价和免费运输)。
但我希望这个折扣仅适用于我在下面的代码中定义的一些特定的本地自取运输方式:"
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 = 5; // <=== 折扣百分比
$chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
$chosen_shipping_method = explode(':', $chosen_shipping_method_id)[0];
// 仅适用于本地自取选择的运输方式
if ( strpos( $chosen_shipping_method_id, 'local_pickup:63', 'local_pickup:64', 'local_pickup:65', 'local_pickup:66', 'local_pickup:67', 'local_pickup:68', 'local_pickup:69' ) !== false ) {
// 计算折扣
$discount = $cart->get_subtotal() * $percentage / 100;
// 添加折扣
$cart->add_fee( __('3% afhaalkorting, levering vanuit ons magazijn') . ' (' . $percentage . '%)', -$discount );
}
}
}
英文:
I like to give a discount on some Local Pickup shipping methods. With the code below I get a discount on all shipping methods (including also Flat Rate and Free Shipping).
But I would like this discount to be applied only on some specific Local Pickup shipping methods I have defined here below in my code:
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 = 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:63', 'local_pickup:64', 'local_pickup:65', 'local_pickup:66', 'local_pickup:67', 'local_pickup:68', 'local_pickup:69' ) !== false ) {
// Calculate the discount
$discount = $cart->get_subtotal() * $percentage / 100;
// Add the discount
$cart->add_fee( __('3% afhaalkorting, levering vanuit ons magazijn') . ' (' . $percentage . '%)', -$discount );
}
}
}
答案1
得分: 1
在WooCommerce中,您可以在拆分购物车上应用多个运输方法,因此是运输方法的数组。
您在尝试从一些自定义的提取运输方法中应用折扣,所以从另一个已定义运输方法的数组。
在这种情况下,最好使用array_intersect()
函数和count()
函数结合在IF
语句中使用,方式如下:
add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_methods', 10, 1 );
function custom_discount_for_pickup_shipping_methods( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// 仅限结账页面
if ( is_checkout() ) {
// 在这里定义允许的运输方法的费率Id
$allowed_shipping_methods = array('local_pickup:63', 'local_pickup:64', 'local_pickup:65', 'local_pickup:66', 'local_pickup:67', 'local_pickup:68', 'local_pickup:69');
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
// 仅适用于选择了当地提取运输方法的情况
if ( count( array_intersect($allowed_shipping_methods, $chosen_shipping_methods) ) > 0 ) {
// 折扣百分比
$percentage = 5;
// 计算折扣
$discount = $cart->get_subtotal() * $percentage / 100;
// 添加折扣
$cart->add_fee( $percentage . __('% afhaalkorting, levering vanuit ons magazijn'), -$discount );
}
}
}
将代码放入您活动的子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。
英文:
First in WooCommerce you can have multiple shipping methods applied on a split cart, so an array of shipping methods.
Here you are trying to apply a discount from some Local Pickup shipping methods, so from an other array of defined shipping methods.
In this case is better to use the functions array_intersect()
with count()
in an IF
statement, this way:
add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_methods', 10, 1 );
function custom_discount_for_pickup_shipping_methods( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only on checkout page
if ( is_checkout() ) {
// Here below define your allowed shipping methods rate Ids in this array
$allowed_shipping_methods = array('local_pickup:63', 'local_pickup:64', 'local_pickup:65', 'local_pickup:66', 'local_pickup:67', 'local_pickup:68', 'local_pickup:69');
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
// Only for Local pickup chosen shipping method
if ( count( array_intersect($allowed_shipping_methods, $chosen_shipping_methods) ) > 0 ) {
// Discount percentage
$percentage = 5;
// Calculate discount
$discount = $cart->get_subtotal() * $percentage / 100;
// Add the discount
$cart->add_fee( $percentage . __('% afhaalkorting, levering vanuit ons magazijn'), -$discount );
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
答案2
得分: 0
[strpos()]函数只支持三个参数,你的使用方式有误。尝试这样:
```php
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;
// 仅在结算页面生效
if ( is_checkout() ) {
$percentage = 5; // <=== 折扣百分比
$chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
$chosen_shipping_method = explode(':', $chosen_shipping_method_id)[0];
// 仅适用于选择的本地自取运输方式
if ( strpos( $chosen_shipping_method_id, 'local_pickup' ) !== false ) {
// 计算折扣
$discount = $cart->get_subtotal() * $percentage / 100;
// 添加折扣
$cart->add_fee( __('3% afhaalkorting, levering vanuit ons magazijn') . ' (' . $percentage . '%)', -$discount );
}
}
}
<details>
<summary>英文:</summary>
[strpos()][1] function supports only three arguments, you're using it wrong. Try this:
```php
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 = 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( __('3% afhaalkorting, levering vanuit ons magazijn') . ' (' . $percentage . '%)', -$discount );
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论