dongruan6001 2018-08-28 11:11
浏览 33
已采纳

WooCommerce - 重复订单的不同送货方式

I am developing a site with the following plugins:

  • WooCommerce
  • WooCommerce Subscriptions
  • Pakkelabels.dk for WooCommerce

"Pakkelabels.dk" is a packaging label plugin for carriers in Denmark. This plugin is using the standard WooCommerce filters and hooks to add additional shipping methods.

I am using a mixed checkout. The cart totals currently looks like this:

enter image description here

This is what I wan't to do

For recurring orders I wan't to limit the shipping methods to just "DAO Pakkeshop" and "Local pick up" (sorry for the Danish language in the image).

I have added this to functions.php, which unsets the shipping methods I don't wan't to have, when a specific product ID (the subscription product) is in the cart:

add_filter( 'woocommerce_package_rates', 'hide_shipping_methods_woo_sg', 10, 2 );
function hide_shipping_methods_woo_sg( $rates, $package ) {

    $product_id = get_field('product_auto_cart', 'option');

    if($product_id){
        $product_cart_id = WC()->cart->generate_cart_id( $product_id );
        $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

        if($in_cart) {
            unset( $rates['pakkelabels_shipping_dao_direct'] );
            unset( $rates['pakkelabels_shipping_gls_private'] );
            unset( $rates['pakkelabels_shipping_gls_business'] );
            unset( $rates['pakkelabels_shipping_gls'] );
            unset( $rates['pakkelabels_shipping_pdk'] );
            unset( $rates['pakkelabels_shipping_postnord_private'] );
            unset( $rates['pakkelabels_shipping_postnord_business'] );
            // unset( $rates['local_pickup:19'] );
        }
        return $rates;
    }
}

My problem is, that this removes the shipping methods for both the order and recurring order, as you can see on the image.

I need some sort of conditional, so that I can target only the recurring order shipping methods and unset those.

How can I achieve this?

  • 写回答

1条回答 默认 最新

  • donkey199024 2018-08-28 12:30
    关注

    Okay - that was a simple fix. WC()->cart->recurring_carts was the conditional I needed. My code now looks like this:

    add_filter( 'woocommerce_package_rates', 'hide_shipping_methods_woo_sg', 10, 2 );
    function hide_shipping_methods_woo_sg( $rates, $package ) {
    
        $product_id = get_field('product_auto_cart', 'option');
    
        if($product_id){
            $product_cart_id = WC()->cart->generate_cart_id( $product_id );
            $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
    
            if($in_cart && WC()->cart->recurring_carts) {
                unset( $rates['pakkelabels_shipping_dao_direct'] );
                unset( $rates['pakkelabels_shipping_gls_private'] );
                unset( $rates['pakkelabels_shipping_gls_business'] );
                unset( $rates['pakkelabels_shipping_gls'] );
                unset( $rates['pakkelabels_shipping_pdk'] );
                unset( $rates['pakkelabels_shipping_postnord_private'] );
                unset( $rates['pakkelabels_shipping_postnord_business'] );
                // unset( $rates['local_pickup:19'] );
            }
            return $rates;
        }
    }
    

    The above shipping methods are now removed for recurring carts.

    My cart totals now looks like this:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)