Wordpress 基于商业上产品数量的运费

Wordpress 基于商业上产品数量的运费,wordpress,woocommerce,shipping,wordpress-ecommerce,Wordpress,Woocommerce,Shipping,Wordpress Ecommerce,我想在我的woocommerce主题上设置运输成本和数量。我想做这个选择: 对于1到5种产品,装运成本将为15%。 超过5种产品的搅打成本为6.99美元 我可以在没有插件的情况下添加此配送选项吗 您需要将一个函数挂接到woocommerce\u calculate\u totals操作,该操作在计算最终购物车总数之前触发。woocommerce\u calculate\u totals操作提供了WC\u Cart实例,您可以根据需要对其执行操作 add_action('woocommerce_c

我想在我的woocommerce主题上设置运输成本和数量。我想做这个选择:

对于1到5种产品,装运成本将为15%。 超过5种产品的搅打成本为6.99美元


我可以在没有插件的情况下添加此配送选项吗

您需要将一个函数挂接到
woocommerce\u calculate\u totals
操作,该操作在计算最终购物车总数之前触发。
woocommerce\u calculate\u totals
操作提供了
WC\u Cart
实例,您可以根据需要对其执行操作

add_action('woocommerce_calculate_totals', 'modify_shipping_totals');

function modify_shipping_totals($cart) {
    if($cart->get_cart_contents_count() < 6) {
        $cart->shipping_total = ( 15/100 ) * $this->cart_contents_total; 
        // shipping cost will be 15% of cart content total

        // you may also want to modify the shipping tax.

        $cart->shipping_tax_total = 0; 
    } else {
        $cart->shipping_total = 6.99;
        $cart->shipping_tax_total = 0;
    }


}
add_操作('woocommerce_计算_总计','modify_shipping_总计');
功能修改装运总额($cart){
如果($cart->get\u cart\u contents\u count()<6){
$cart->shipping\u total=(15/100)*$this->cart\u contents\u total;
//运费将占购物车内容总额的15%
//您可能还需要修改运费税。
$cart->shipping\u tax\u总计=0;
}否则{
$cart->shipping_总计=6.99;
$cart->shipping\u tax\u总计=0;
}
}

有关可更改变量的更多参考,请参阅。

如果您在init hook内调用add_操作,Pranav解决方案将起作用,如下所示:

function init_shop(){
    add_action('woocommerce_calculate_totals', 'modify_shipping_totals', 10);
}
add_action( 'init', 'init_shop');
//**注意**:此代码仅在您设置固定费率设置时有效
//成本价值为1
添加_过滤器('woocmerce_package_rates','custom_package_rates',10,2);
功能自定义包价格($rates,$packages){
if(is_admin()&&!defined('DOING_AJAX'))返回;
$cart\u count=WC()->cart->get\u cart\u contents\u count();
$cart\u total=WC()->cart->cart\u contents\u total;
foreach($rate作为$rate\u键=>$rate\u值){
$method\u id=$rate\u values->method\u id;
$rate\u id=$rate\u values->id;
如果($method\u id=='flat\u rate'){
如果($cart\u count<99){
$flat\u rate\u value=4.95;//“应用小于99个数量的统一费率”
$cart\u 10\u percent=0;//无折扣
}   
如果($cart\u count>99){
$flat\u rate\u value=9.95;//“应用大于99个数量的统一费率”
$cart\u 10\u percent=0;//无折扣
}
$rate\u cost=$flat\u rate\u value>$cart\u 10%?$flat\u rate\u value-$cart\u 10%:0;
//设置新的计算费率成本
$rates[$rate\u id]->cost=number\u格式($rates[$rate\u id]->cost*$rate\u cost,2);
}
}
返回美元汇率;
} 

Hi Pranav,我不是高级开发人员。我可以用表费率运费插件制作这个系统吗?因为客户希望随时更改费用。我面临着这个问题的麻烦。没有人能给我一个正确的答案。你能帮帮我吗?这对我没用。也许这是因为woocommerce的新版本。
// **Note**: This code is working only when you set Flat rate Settings 
// cost value is 1

add_filter( 'woocommerce_package_rates', 'custom_package_rates', 10, 2 );
 function custom_package_rates( $rates, $packages ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $cart_count = WC()->cart->get_cart_contents_count();
    $cart_total =  WC()->cart->cart_contents_total;

    foreach($rates as $rate_key => $rate_values ) {
        $method_id = $rate_values->method_id;
        $rate_id = $rate_values->id;

        if( $method_id == 'flat_rate' ){
            if( $cart_count < 99 ){
                $flat_rate_value = 4.95; //"Applay Flat rate less then 99 quatity"
                $cart_10_percent = 0; // No percent discount
            }   
            if( $cart_count > 99 ){
                $flat_rate_value = 9.95; // "Applay Flat rate greater then 99 quatity"
                $cart_10_percent = 0; // No percent discount
            }
            $rate_cost = $flat_rate_value > $cart_10_percent ? $flat_rate_value - $cart_10_percent : 0;

            // Set the new calculated rate cost
            $rates[$rate_id]->cost = number_format( $rates[$rate_id]->cost * $rate_cost, 2 );

        }
    }
    return $rates;
}