【问题标题】:WooCommerce Shipping Plugin - Checkout Shipping Cost Not Updating(AJAX)WooCommerce Shipping 插件 - 结帐运费未更新(AJAX)
【发布时间】:2021-09-24 09:34:53
【问题描述】:

我已经搜索过任何解决方案,但没有任何效果。

这里我想做的事:

  1. 在结帐页面中添加名为 Shipping 的新字段显示为填充有 PreorderNon-preorder 的选择框
  2. 如果preorder 那么将显示位置字段显示为选择框
  3. 如果 non-preorder 则显示 Location 字段与第 2 步相同,但列表选项不同。
  4. ShippingLocation填写正确时,运费将显示为正确的运费。

问题

当我填写了所有必填字段和运费时,位置再次计算运费。它做 AJAX 请求,但运费保持不变。

但是,当我更改 姓名街道地址 时,运费正确更新,这是不好的行为。当用户立即点击Place Order时会发布错误的运费。

这里是我的Youtube Video,以明确我需要帮助的内容。


我做了什么来创建自己的插件:

  1. 使用类似这样的钩子创建自定义字段
    public function woocommerce_checkout_fields( $fields )
    {
        // var_dump(have_rows('location', 'option'));
        // die('test');

        $options = [];

        $options[''] = 'Select Shipping';
        $options['pre-order'] = 'Pre Order';
        if (!$this->hasPreorderItem()) {
            $options['non-pre-order'] = 'Non Pre Order';
        }

        // Add custom billing shipping
        $fields['billing']['billing_shipping'] = array (
            'type' => 'select',
            'label' => 'Shipping',
            'placeholder' => 'Select shipping',
            'class' => array ('address-field', 'update_totals_on_change' ),
            'required' => true,
            'options' => $options
            // 'options' => $this->getLocations()
        );

        $fields['billing']['billing_location_pre_order'] = array (
            'type' => 'select',
            'label' => 'Location(Pre Order)',
            'placeholder' => 'Select Preorder Location',
            'class' => array ('address-field', 'update_totals_on_change' ),
            'required' => false,
            'options' => $this->preorderLocations->getLocations()
            // 'options' => $this->getLocations()
        );

        // Add custom billing location
        $fields['billing']['billing_location'] = array (
            'type' => 'select',
            'label' => 'Location',
            'placeholder' => 'Select location',
            'class' => array ('address-field', 'update_totals_on_change' ),
            'required' => false,
            'options' => $this->locations->getLocations()
            // 'options' => $this->getLocations()
        );

        return $fields;
    }
  1. 创建类以帮助根据ShippingLocation计算成本。

我尝试过的

  1. 使用此答案但没有运气。 Click here 提问。
  2. 尝试将字段设为必填,因为我看到有人说只有在所有必填字段都填满后才会更新。
  3. 试图禁用wp-config.php 中的缓存

【问题讨论】:

    标签: wordpress woocommerce plugins hook-woocommerce shipping-method


    【解决方案1】:

    我从this link找到了解决方案

    我可能会帮助有类似问题的人。您只需要清除会话。因此,WooCommerce 会重新计算并调用您的 calculate_shipping() 函数。

    为此添加woocommerce_checkout_update_order_review 钩子。它看起来像这样

    add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 1);
    function action_woocommerce_checkout_update_order_review( $posted_data )
    {
        global $woocommerce;
        $packages = $woocommerce->cart->get_shipping_packages();
        foreach( $packages as $package_key => $package ) {
            $session_key  = 'shipping_for_package_'.$package_key;
            $stored_rates = WC()->session->__unset( $session_key );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 1970-01-01
      • 2015-09-12
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多