繁体   English   中英

Woocommerce设置运输方式,获取运输成本

[英]Woocommerce set shipping method, get shipping cost

在我的functions.php文件中,我需要能够设置运输方式(此站点只有一个),然后返回运输成本(设置了一些成本)。

我可以看到使用这种方式的flat_rate运送:

foreach (WC()->shipping->get_shipping_methods() as $key => $value){
    echo $key;
}

所以肯定在那里。

基本上,我希望通过API调用返回运输费用。 我已经路由了API,并且一切正常。 它调用了我在某处获取的此函数,目前不记得了:

function pc_get_shipping(){
  $ret = array();
    foreach( WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate ){
        if( WC()->session->get('chosen_shipping_methods')[0] == $method_id ){
            $rate_label = $rate->label; // The shipping method label name
            $rate_cost_excl_tax = floatval($rate->cost); // The cost excluding tax
            // The taxes cost
            $rate_taxes = 0;
            foreach ($rate->taxes as $rate_tax)
                $rate_taxes += floatval($rate_tax);
            // The cost including tax
            $rate_cost_incl_tax = $rate_cost_excl_tax + $rate_taxes;

            $ret[] = array('label' => $rate_label, 'total' => WC()->cart->get_cart_shipping_total());
        } 
    }
  return $ret;
}

但这只给了我一个空数组,可能是因为WC()->session->get('shipping_for_package_0')['rates']计算为一个空数组。

TL:DR

  • 通过WC()->customer->set_shipping_address_1(wc_clean($value));保存来宾客户运输信息WC()->customer->set_shipping_address_1(wc_clean($value)); (对于所有值等)

  • 使用WC()->customer->get_shipping()可以正确返回来宾客户的运送信息,因此我认为它的设置正确。

  • 可以通过WC()->shipping->get_shipping_methods()来使用flat_rate装运方法。

  • 我如何在functions.php中设置当前订单的送货方式,该方式将通过REST API调用。

  • 如何通过REST API调用在functions.php中获得当前订单的计算出的运输成本

首先在API响应上,您需要在WC_Session成本值设置为自定义数据,类似(where $ value is the response cost value from your API)

if( ! WC()->session->__isset( 'shipping_cost' ) && ! empty($value) ){
    WC()->session->set( 'shipping_cost', $value );
}

您可能需要使用jQuery刷新ajax结帐页面: $('body').trigger('update_checkout');

然后,您将使用此挂钩函数:

add_filter('woocommerce_package_rates', 'shipping_cost_based_on_api', 12, 2);
function shipping_cost_based_on_api( $rates, $package ){
    if( WC()->session->__isset( 'shipping_cost' ) ) {

        // Loop through the shipping taxes array
        foreach ( $rates as $rate_key => $rate ){
            $has_taxes = false;

            if( 'flat_rate' === $rate->method_id ){
                // Get the initial cost
                $initial_cost = $new_cost = $rates[$rate_key]->cost;

                // Get the new cost
                $new_cost = WC()->session->get( 'shipping_cost' );

                // Set the new cost
                $rates[$rate_key]->cost = $new_cost;

                // Taxes rate cost (if enabled)
                $taxes = [];
                // Loop through the shipping taxes array (as they can be many)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $rates[$rate_key]->taxes[$key] > 0 ){
                        // Get the initial tax cost
                        $initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];
                        // Get the tax rate conversion
                        $tax_rate    = $initial_tax_cost / $initial_cost;
                        // Set the new tax cost
                        $taxes[$key] = $new_cost * $tax_rate;
                        $has_taxes   = true; // Enabling tax
                    }
                }
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    return $rates;
}

// Enabling, disabling and refreshing session shipping methods data
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods( $post_data ){
    $bool = true;
    if ( WC()->session->__isset('shipping_cost' ) ) $bool = false;

    // Mandatory to make it work with shipping methods
    foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
        WC()->session->set( 'shipping_for_package_' . $package_key, $bool );
    }
    WC()->cart->calculate_shipping();
}

代码进入您的活动子主题(或活动主题)的function.php文件中。 它应该工作。

基于: 如果在WooCommerce Checkout中选中了“自定义”复选框,则删除运输成本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM