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

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

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错