【问题标题】:Free shipping on the first order on WooCommerce [closed]WooCommerce 上的第一个订单免运费 [关闭]
【发布时间】:2021-11-30 10:43:14
【问题描述】:

我希望客户的第一笔订单免运费 如何将此功能添加到 WooCommerce? 我不想使用折扣代码,我希望自动应用此功能。

【问题讨论】:

    标签: php wordpress woocommerce orders shipping-method


    【解决方案1】:

    也许是这样的?所有学分都归-Set custom shipping rates programmatically in Woocommerce 3

    add_filter( 'woocommerce_package_rates', 'free_first_order_shipping', 20, 2 );
    function free_first_order_shipping( $rates, $package ) {
        // New shipping cost (can be calculated)
        $new_cost = 0;
        $tax_rate = 0;
        //If user is logged in
        if(is_user_logged_in()) {
            $user_id = get_current_user_id();
            //We want to know how many completed orders customer have or remove status if you dont care.
            $args = array(
                'customer_id' => 1,
                'status' => array('wc-completed'),
            );
            $orders = wc_get_orders($args);
            //If there are no orders returned apply free shipping
            if(!$orders) {
                foreach( $rates as $rate_key => $rate ){
                    error_log(print_r($rate,true));
                    // Excluding free shipping methods
                    if( $rate->method_id != 'free_shipping'){
    
                        // Set rate cost
                        $rates[$rate_key]->cost = $new_cost;
                        //Set shipping label
                        $rates[$rate_key]->label = 'Free Shipping';
    
                        // Set taxes rate cost (if enabled)
                        $taxes = array();
                        foreach ($rates[$rate_key]->taxes as $key => $tax){
                            if( $rates[$rate_key]->taxes[$key] > 0 )
                                $taxes[$key] = $new_cost * $tax_rate;
                        }
                        $rates[$rate_key]->taxes = $taxes;
    
                    }
                }            
            }
        }
    
        return $rates;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 2014-02-01
      • 1970-01-01
      • 2018-10-27
      • 2021-05-28
      相关资源
      最近更新 更多