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 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题