在WooCommerce结账中为特定运输方式提供折扣。

huangapple go评论53阅读模式
英文:

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( &#39;woocommerce_cart_calculate_fees&#39;, &#39;custom_discount_for_pickup_shipping_method&#39;, 10, 1 );
function custom_discount_for_pickup_shipping_method( $cart ) {
    if ( is_admin() &amp;&amp; ! defined( &#39;DOING_AJAX&#39; ) )
        return;

    // Only on checkout page
    if ( is_checkout() ) {
    
        $percentage = 5; // &lt;=== Discount percentage
    
        $chosen_shipping_method_id = WC()-&gt;session-&gt;get( &#39;chosen_shipping_methods&#39; )[0];
        $chosen_shipping_method    = explode(&#39;:&#39;, $chosen_shipping_method_id)[0];
    
        // Only for Local pickup chosen shipping method
        if ( strpos( $chosen_shipping_method_id, &#39;local_pickup:63&#39;, &#39;local_pickup:64&#39;, &#39;local_pickup:65&#39;, &#39;local_pickup:66&#39;, &#39;local_pickup:67&#39;, &#39;local_pickup:68&#39;, &#39;local_pickup:69&#39; ) !== false ) {
            // Calculate the discount
            $discount = $cart-&gt;get_subtotal() * $percentage / 100;
            // Add the discount
            $cart-&gt;add_fee( __(&#39;3% afhaalkorting, levering vanuit ons magazijn&#39;) . &#39; (&#39; . $percentage . &#39;%)&#39;, -$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( &#39;woocommerce_cart_calculate_fees&#39;, &#39;custom_discount_for_pickup_shipping_methods&#39;, 10, 1 );
function custom_discount_for_pickup_shipping_methods( $cart ) {
    if ( is_admin() &amp;&amp; ! defined( &#39;DOING_AJAX&#39; ) )
        return;

    // Only on checkout page
    if ( is_checkout() ) {
        
        // Here below define your allowed shipping methods rate Ids in this array
        $allowed_shipping_methods = array(&#39;local_pickup:63&#39;, &#39;local_pickup:64&#39;, &#39;local_pickup:65&#39;, &#39;local_pickup:66&#39;, &#39;local_pickup:67&#39;, &#39;local_pickup:68&#39;, &#39;local_pickup:69&#39;);
        
        $chosen_shipping_methods = WC()-&gt;session-&gt;get( &#39;chosen_shipping_methods&#39; );
    
        // Only for Local pickup chosen shipping method
        if ( count( array_intersect($allowed_shipping_methods, $chosen_shipping_methods) ) &gt; 0 ) {
            // Discount percentage
            $percentage = 5;
            // Calculate discount
            $discount = $cart-&gt;get_subtotal() * $percentage / 100;
            // Add the discount
            $cart-&gt;add_fee( $percentage . __(&#39;% afhaalkorting, levering vanuit ons magazijn&#39;), -$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&#39;re using it wrong. Try this:

```php
add_action( &#39;woocommerce_cart_calculate_fees&#39;, &#39;custom_discount_for_pickup_shipping_method&#39;, 10, 1 );
function custom_discount_for_pickup_shipping_method( $cart ) {
    if ( is_admin() &amp;&amp; ! defined( &#39;DOING_AJAX&#39; ) )
        return;

    // Only on checkout page
    if ( is_checkout() ) {
    
        $percentage = 5; // &lt;=== Discount percentage
    
        $chosen_shipping_method_id = WC()-&gt;session-&gt;get( &#39;chosen_shipping_methods&#39; )[0];
        $chosen_shipping_method    = explode(&#39;:&#39;, $chosen_shipping_method_id)[0];
    
        // Only for Local pickup chosen shipping method
        if ( strpos( $chosen_shipping_method_id, &#39;local_pickup&#39; ) !== false ) {
            // Calculate the discount
            $discount = $cart-&gt;get_subtotal() * $percentage / 100;
            // Add the discount
            $cart-&gt;add_fee( __(&#39;3% afhaalkorting, levering vanuit ons magazijn&#39;) . &#39; (&#39; . $percentage . &#39;%)&#39;, -$discount );
        }
    }
}

huangapple
  • 本文由 发表于 2023年5月30日 02:26:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定