Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 电子商务中的定制动态运费成本计算_Php_Wordpress_Woocommerce_Cart_Shipping - Fatal编程技术网

Php 电子商务中的定制动态运费成本计算

Php 电子商务中的定制动态运费成本计算,php,wordpress,woocommerce,cart,shipping,Php,Wordpress,Woocommerce,Cart,Shipping,我已经设置了一个自定义的运送方法,每次用户到达购物车页面时,我都需要计算运送成本 似乎只有当用户单击某个配送方法时,woocommerce\u package\u ratesaction(我计算定制配送成本)才会运行。这样一来,购物车总数在大多数情况下都是错误的,最糟糕的情况是,已经选择了自定义的配送方式(用户不需要单击它,因此其成本不会更新) 这是woocommerce\u package\u rateshook的正常行为吗 如何在显示购物车总数之前强制执行woocommerce\u pack

我已经设置了一个自定义的运送方法,每次用户到达购物车页面时,我都需要计算运送成本

似乎只有当用户单击某个配送方法时,
woocommerce\u package\u rates
action(我计算定制配送成本)才会运行。这样一来,购物车总数在大多数情况下都是错误的,最糟糕的情况是,已经选择了自定义的配送方式(用户不需要单击它,因此其成本不会更新)

这是
woocommerce\u package\u rates
hook的正常行为吗

如何在显示购物车总数之前强制执行
woocommerce\u package\u rates

编辑

以下是我试图破解的一些代码:

add_action( 'woocommerce_before_cart', 'force_shipping_calc', 1, 0 );
function force_shipping_calc() {
    foreach( WC()->session->get('shipping_for_package_0')['rates'] as $rate_id =>$rate) {
        // looking for the shipping method to recalc
        if($rate->method_id == 'flat_rate') {

            // mk1: set_shipping_total won't work, i'm using woocommerce < 3
            //WC()->cart->set_shipping_total( MY_calculate_shipping() );

            // mk2: doesn't work, "Indirect modification of overloaded property" 
            WC()->cart->totals['shipping_total'] = wc_format_decimal( MY_calculate_shipping(), wc_get_price_decimals() );

            // mk3: cart total nor shipping total affected (?!)
            WC()->cart->shipping_total = MY_calculate_shipping();

            // mk4: ... ?! work in progress...
    }
}

function MY_calculate_shipping() {
    return 99.99;
}


add_filter( 'woocommerce_package_rates', 'fty_shipping_flat_rate_cost_calculation', 10, 2 );
function fty_shipping_flat_rate_cost_calculation($rates, $package) {
    foreach($rates as $rate_key => $rate_values) {

        $method_id = $rate_values->method_id;
        $rate_id = $rate_values->id;

        if ( 'flat_rate' === $method_id ){

            $dist_cost  =   MY_calculate_shipping();

            $price_excl_tax = $rates[$rate_id]->cost + $dist_cost;
            $rates[$rate_id]->cost =  number_format($price_excl_tax, 2);

            $tax_keys   =   array_keys($rates[$rate_id]->taxes);

            $price_excl_tax = $rates[$rate_id]->cost + $dist_cost;
            $rates[$rate_id]->cost =  number_format($price_excl_tax, 2);

            $tax_calculation = $rates[$rate_id]->taxes[$tax_keys[0]] + $dist_cost*(TAX_AMOUNT_IVA-1);
            $rates[$rate_id]->taxes[$tax_keys[0]] = number_format($tax_calculation, 2);

            $rates[$rate_id]->cost +=   $dist_cost;
        }
    }

    return $rates;
}

但它还不是完美的,我认为这个钩子在结帐页面中形成了一个循环…

这只需要在
woocommerce\u package\u rates
中完成。在您的代码中有许多错误…请尝试以下操作:

function custom_calculated_shipping() {
    return 99.99;
}

add_filter( 'woocommerce_package_rates', 'custom_shipping_rate_cost_calculation', 10, 2 );
function custom_shipping_rate_cost_calculation( $rates, $package ) {
    foreach( $rates as $rate_key => $rate ) {
        if ( 'flat_rate' === $rate->method_id ){

            // Get rate cost and Custom cost
            $initial_cost = $rates[$rate_key]->cost;
            $additional_cost = custom_calculated_shipping();

            // Calculation
            $new_cost = $initial_cost + $additional_cost;

            // Set Custom rate cost
            $rates[$rate_key]->cost = round($new_cost, 2);

            // Taxes rate cost (if enabled)
            $new_taxes = array();
            $has_taxes = false;
            foreach ( $rate->taxes as $key => $tax ){
                if( $tax > 0 ){
                    // Calculating the tax rate unit
                    $tax_rate = $tax / $initial_cost;
                    // Calculating the new tax cost
                    $new_tax_cost = $tax_rate * $new_cost;
                    // Save the calculated new tax rate cost in the array
                    $new_taxes[$key] = round( $new_tax_cost, 2 );
                    $has_taxes = true;
                }
            }
            // Set new tax rates cost (if enabled)
            if( $has_taxes )
                $rate->taxes = $new_taxes;
        }
    }

    return $rates;
}
代码进入活动子主题(或主题)的function.php文件。测试和工作。它将在所有woocommerce版本中工作,从2.6

您应该需要刷新发货缓存:
1) 首先,此代码已保存在function.php文件中
2) 空马车
3) 在装运设置中,输入装运区域并禁用装运方法和“保存”。然后重新启用该装运方法和“保存”你完成了。


您最好在问题中添加自定义代码……”寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现所需的最短代码@LoicTheAztec我添加了一些胚胎代码我正在尝试了解更多关于正在发生的事情…您能在您的问题中添加您在
woocommerce\u package\u rates
hook中使用的自定义代码吗,请…看起来您使用的是v3之前的WC版本…哪一个?@LoicTheAztec woocommerce v2.6。14@LoicTheAztec添加了
woocommerce\u package\u rates
callbackThanx,我尝试了你的代码,但它没有解决问题,我想这是其他地方的问题。正如我在问题
woocommerce\u package\u rates
中所写,只有当用户单击某个配送方式时才会调用回调-我指的是购物车页面,而不是结账页面。要用mk.4更新我的答案:)@j.c这个钩子在任何地方都有效,是定制运输成本的正确钩子……实际上没有其他方法。因此,问题更多地在于如何设置it中的动态成本。您可能还应该使用WC_会话来获取动态值……WC_会话和WC_传送是我正在查看的内容,但我很确定只有当用户更改传送方法时才会触发
woocommerce_包传送率
hook<代码>WC()->装运->计算装运(WC()->装运->包装)触发它,但我需要在正确的位置运行它。我正在尝试在装运前使用
woocommerce\u cart\u totals\u
hook
function custom_calculated_shipping() {
    return 99.99;
}

add_filter( 'woocommerce_package_rates', 'custom_shipping_rate_cost_calculation', 10, 2 );
function custom_shipping_rate_cost_calculation( $rates, $package ) {
    foreach( $rates as $rate_key => $rate ) {
        if ( 'flat_rate' === $rate->method_id ){

            // Get rate cost and Custom cost
            $initial_cost = $rates[$rate_key]->cost;
            $additional_cost = custom_calculated_shipping();

            // Calculation
            $new_cost = $initial_cost + $additional_cost;

            // Set Custom rate cost
            $rates[$rate_key]->cost = round($new_cost, 2);

            // Taxes rate cost (if enabled)
            $new_taxes = array();
            $has_taxes = false;
            foreach ( $rate->taxes as $key => $tax ){
                if( $tax > 0 ){
                    // Calculating the tax rate unit
                    $tax_rate = $tax / $initial_cost;
                    // Calculating the new tax cost
                    $new_tax_cost = $tax_rate * $new_cost;
                    // Save the calculated new tax rate cost in the array
                    $new_taxes[$key] = round( $new_tax_cost, 2 );
                    $has_taxes = true;
                }
            }
            // Set new tax rates cost (if enabled)
            if( $has_taxes )
                $rate->taxes = $new_taxes;
        }
    }

    return $rates;
}