0
Follow
2
View

Free shipping on the first order on WooCommerce [closed]

lingyun820 注册会员
2023-01-26 02:58

Maybe something like this ? All credits goes to - 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;
}

About the Author

Question Info

Publish Time
2023-01-26 02:58
Update Time
2023-01-26 02:58

Related Question

为什么“hello”(isMaster)命令与topologyVersion超时?

尝试添加自定义字体到Sphinx-RTD-theme

python:PDF转excel

Python深度学习的时候屏幕显示线插在独立显卡还是集成显卡好?

【Opencv-Python】读取文件imread()出现的问题

颤动改变颜色的utils const颜色,在应用程序屏幕使用按钮

PhoneAuth可以在模拟器上工作,但不能在真正的IOS设备上工作

如何存储字典python列表中循环的所有值

Google_fonts包参数不匹配

python解释的过程是怎么样的呢?