使用 Jquery ajax 更新 WooCommerce "flat rate" 运费
Update WooCommerce "flat rate" shipping cost using Jquery ajax
我正在尝试在输入地址后更新运费。当客户开始输入地址时,自动完成功能会帮助客户找到街道名称、建筑物、楼层和侧面。
输入地址后javascript计算距离和价格。
然后价格通过ajax发送到shipment.php
我的目标是让发货相应地更新运费。
我试过了:
include '../../../../wp-load.php';
add_action( 'woocommerce_flat_rate_shipping_add_rate','add_another_custom_flat_rate', 10, 2 );
function add_another_custom_flat_rate( $method, $rate ) {
$new_rate = $rate;
$new_rate['cost'] = 50; // Update the cost to 50
$method->add_rate( $new_rate );
}
但运气不好。
我也遵循了本指南并添加了另一种运输方式:Tuts
https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098
但再次强调,输入地址后我无法更新价格。
有人可以帮我吗?
此致
I think that You don't need Query ajax to update the price directly for this calculated shipping cost and you don't need a custom shipping method as in your code or linked tutorial.
有三个步骤:
1) 在您现有的 Ajax 驱动的 PHP 函数中,您将在自定义 WC_Session 属性中设置计算价格,其中 $shipping_cost
变量将是收集的值通过 JS 传递的计算运费的百分比 Ajax:
WC()->session->set( 'shipping_calculated_cost', $shipping_cost );
然后在 jQuery Ajax "on success" 活动中,您将使用以下内容更新结帐:
$('body').trigger('update_checkout');
这将刷新结账数据…
2) 在woocommerce_package_rates
filer hook的自定义函数中,会得到计算运费的session值...
IMPORTANT: In WooCommerce settings > shipping, you will set the "flat rate" cost to 1
…
在函数代码中,您将为此固定费率设置默认价格(当运费计算尚不存在时将使用):
add_filter('woocommerce_package_rates', 'update_shipping_costs_based_on_cart_session_custom_data', 10, 2);
function update_shipping_costs_based_on_cart_session_custom_data( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// SET HERE the default cost (when "calculated cost" is not yet defined)
$cost = '50';
// Get Shipping rate calculated cost (if it exists)
$calculated_cost = WC()->session->get( 'shipping_calculated_cost');
// Iterating though Shipping Methods
foreach ( $rates as $rate_key => $rate_values ) {
$method_id = $rate_values->method_id;
$rate_id = $rate_values->id;
// For "Flat rate" Shipping" Method only
if ( 'flat_rate' === $method_id ) {
if( ! empty( $calculated_cost ) ) {
$cost = $calculated_cost;
}
// Set the rate cost
$rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cost, 2);
// Taxes rate cost (if enabled)
foreach ($rates[$rate_id]->taxes as $key => $tax){
if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
$taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cost, 2 );
$has_taxes = true;
} else {
$has_taxes = false;
}
}
if( $has_taxes )
$rates[$rate_id]->taxes = $taxes;
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件或任何插件文件。
3) 刷新运输缓存(有时需要):
- First empty your cart.
- This code is already saved on your function.php file.
- Go in a shipping zone settings and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done and you can test it.
这应该适合你,
If you have many different "flat rates" by shipping zones, you will need to make some changes in my code, if the default cost is different for each…
我正在尝试在输入地址后更新运费。当客户开始输入地址时,自动完成功能会帮助客户找到街道名称、建筑物、楼层和侧面。
输入地址后javascript计算距离和价格。
然后价格通过ajax发送到shipment.php
我的目标是让发货相应地更新运费。
我试过了:
include '../../../../wp-load.php';
add_action( 'woocommerce_flat_rate_shipping_add_rate','add_another_custom_flat_rate', 10, 2 );
function add_another_custom_flat_rate( $method, $rate ) {
$new_rate = $rate;
$new_rate['cost'] = 50; // Update the cost to 50
$method->add_rate( $new_rate );
}
但运气不好。
我也遵循了本指南并添加了另一种运输方式:Tuts https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098
但再次强调,输入地址后我无法更新价格。
有人可以帮我吗?
此致
I think that You don't need Query ajax to update the price directly for this calculated shipping cost and you don't need a custom shipping method as in your code or linked tutorial.
有三个步骤:
1) 在您现有的 Ajax 驱动的 PHP 函数中,您将在自定义 WC_Session 属性中设置计算价格,其中 $shipping_cost
变量将是收集的值通过 JS 传递的计算运费的百分比 Ajax:
WC()->session->set( 'shipping_calculated_cost', $shipping_cost );
然后在 jQuery Ajax "on success" 活动中,您将使用以下内容更新结帐:
$('body').trigger('update_checkout');
这将刷新结账数据…
2) 在woocommerce_package_rates
filer hook的自定义函数中,会得到计算运费的session值...
IMPORTANT: In WooCommerce settings > shipping, you will set the "flat rate" cost to
1
…
在函数代码中,您将为此固定费率设置默认价格(当运费计算尚不存在时将使用):
add_filter('woocommerce_package_rates', 'update_shipping_costs_based_on_cart_session_custom_data', 10, 2);
function update_shipping_costs_based_on_cart_session_custom_data( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// SET HERE the default cost (when "calculated cost" is not yet defined)
$cost = '50';
// Get Shipping rate calculated cost (if it exists)
$calculated_cost = WC()->session->get( 'shipping_calculated_cost');
// Iterating though Shipping Methods
foreach ( $rates as $rate_key => $rate_values ) {
$method_id = $rate_values->method_id;
$rate_id = $rate_values->id;
// For "Flat rate" Shipping" Method only
if ( 'flat_rate' === $method_id ) {
if( ! empty( $calculated_cost ) ) {
$cost = $calculated_cost;
}
// Set the rate cost
$rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cost, 2);
// Taxes rate cost (if enabled)
foreach ($rates[$rate_id]->taxes as $key => $tax){
if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
$taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cost, 2 );
$has_taxes = true;
} else {
$has_taxes = false;
}
}
if( $has_taxes )
$rates[$rate_id]->taxes = $taxes;
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件或任何插件文件。
3) 刷新运输缓存(有时需要):
- First empty your cart.
- This code is already saved on your function.php file.
- Go in a shipping zone settings and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done and you can test it.
这应该适合你,
If you have many different "flat rates" by shipping zones, you will need to make some changes in my code, if the default cost is different for each…