Woocommerce:按运费国家/地区更改总价

时间:2017-04-13 14:59:17

标签: wordpress woocommerce shipping

我想在用户更改送货国家时更改总结算价格。例如:如果我选择“意大利”价格它不会发生变化;如果我选择美国价格,我必须打折。

这部分似乎是由ajax运行的

感谢

1 个答案:

答案 0 :(得分:1)

正在尝试调整运费。如果是这样,您可以根据需要调整此代码段。

add_filter('woocommerce_package_rates', 'wf_add_extra_cost_based_on_state_and_shipping_class', 10, 2);
function wf_add_extra_cost_based_on_state_and_shipping_class($available_shipping_methods, $package) {
    global $woocommerce;
    $state_list = array('HI', 'AK');

    $shipping_classes = array(
        'my-shipping-class', ///enter the name of shipping class slug
    );
    $additional_price = 10; // static amount

    $customer_state = (WC()->version < '2.7.0') ? WC()->customer->shipping_state : WC()->customer->get_shipping_state();
    if (in_array($customer_state, $state_list)) {
        $avail_shipping_class = array();
        foreach ($package['contents'] as $key => $cart_item) {
            $avail_shipping_class[] = $cart_item['data']->get_shipping_class(); 

        }
        if (!empty(array_intersect($shipping_classes, $avail_shipping_class))) {
            foreach ($available_shipping_methods as $method_key => &$method) {
                $method->cost += $additional_price;
            }
        }
    }
    return $available_shipping_methods;
}