在我的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->get_shipping()
正确返回了客户的客户运送信息,因此我认为它的设置正确。
可以通过flat_rate
获得运输方式WC()->shipping->get_shipping_methods()
。
我如何在functions.php中设置当前订单的运送方式,该方式将通过REST API调用。
我如何在functions.php中获取当前订单的计算出的运输成本,该方法将通过REST API进行调用。
答案 0 :(得分:1)
首先在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文件中。应该可以。
基于:Remove shipping cost if custom checkbox is checked in WooCommerce Checkout