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

标签 php ajax wordpress woocommerce

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

输入地址后,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/

相关文章:

php - 未选择数据库。预约类(class)录入失败: No database selected

php - PDO "Integrity constraint violation - Duplicate entry",但表中没有重复项

javascript - jQuery 不附加标题

php - 分析 Wordpress Hack : decrypting idca. php

WordPress 在没有插件的情况下向页面添加自定义字段

php - WordPress:提交后表单重定向

javascript - 根据用户位置查询数据库值

php - 在php中反序列化序列化数组

python - Django保存文件不使用表单,仅使用ajax

javascript - 使用基于值的 jQuery 代码更改 SharePoint 列表字段颜色?