我已经设置了一种自定义运输方式,每次用户进入购物车页面时都需要计算运费。
似乎woocommerce_package_rates
操作(我计算自定义运输成本(仅在用户单击运输方式时才运行。这样,购物车总数在大多数情况下都是错误的,最糟糕的是已经选择了自定义运输方式(用户不需要单击它,因此其成本不会更新(。
这是woocommerce_package_rates
钩子的正常行为吗?
如何在显示购物车总计之前强制始终执行woocommerce_package_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;
}
再次编辑
这(mk. ~17786(似乎方向正确。我从WC_Shipping更改了钩子并强制calculate_shipping()
add_action( 'woocommerce_cart_totals_before_shipping', 'fty_force_calculate_shipping', 1, 2550 );
function fty_force_calculate_shipping() {
WC()->shipping->calculate_shipping(WC()->shipping->packages);
WC()->cart->calculate_totals();
}
但它还不完美,我认为这个钩子在结帐页面中做了一个循环......
只需要在woocommerce_package_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;
}
代码进入函数.php活动子主题(或主题(的文件。经过测试并工作。它将在 2.6 以来的所有 wooCommerce 版本中运行......
您应该需要刷新运输缓存:
1(首先,此代码已经保存在您的函数.php文件中。
2(空车。
3( 在配送设置中,输入配送区域并禁用配送方式并"保存"。然后重新启用该运输方式并"保存"。大功告成。