gpt4 book ai didi

php - 使用 jQuery 更新 WooCommerce "flat rate"运费

转载 作者:搜寻专家 更新时间:2023-10-31 21:49:07 27 4
gpt4 key购买 nike

我正在尝试在输入地址后更新运费。当客户开始输入地址时,自动完成功能会帮助客户找到街道名称、建筑物、楼层和侧面。

输入地址后,JavaScript 会计算距离和价格。

然后价格通过 AJAX 发送到 shipment.php

我的目标是让 shipment 相应地更新运费。

我试过:

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“成功时”事件中,您将使用以下内容更新结帐:

$('body').trigger('update_checkout');

这将刷新结帐数据......


2) 在 woocommerce_package_rates filer hook 中 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…

关于php - 使用 jQuery 更新 WooCommerce "flat rate"运费,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47548939/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com