WooCommerce不能优先考虑表费运输方式,所以我正在尝试自己做,但是可怕的是失败。WooCommerce:以编程方式更改默认的运输方式
我试图添加一个动作在我认为它已设置,但它不起作用。以下是我已经尝试(我只是想用第一运输方式提供的运送方法数组):
function reset_default_shipping_method() {
$available_methods = WC()->shipping->packages[0]['rates'];
$chosen_method = key($available_methods);
WC()->session->set('chosen_shipping_methods', $chosen_method);
}
add_action('woocommerce_shipping_method_chosen', 'reset_default_shipping_method');
我已经测试$available_methods
和$chosen_method
无一不是如预期(在页面中运行时)呈现BUT ,一旦我将它添加到行动woocommerce_shipping_method_chosen
它似乎并没有更新。
例如如果我跑这通过不同的挂钩:
function output_to_footer() {
if (current_user_can('administrator')) :
$current_method = WC()->session->get('chosen_shipping_methods');
$available_methods = WC()->shipping->packages[0]['rates'];
$chosen_method = key($available_methods);
WC()->session->set('chosen_shipping_methods', $chosen_method);
print "\n".'-----'."\n";
print_r($current_method);
print "\n".'-----'."\n";
print_r($available_methods);
print "\n".'-----'."\n";
print_r($chosen_method);
print "\n".'-----'."\n";
endif;
}
add_action('print_footer_messages', 'output_to_footer');
看起来一切都在做什么应该,但是当我从操作运行它:woocommerce_shipping_method_chosen
“出现”它做的一切,但航运无线电仍设置到旧的运输方式?
----- Array ( [0] => table_rate-5 : 70 ) ----- Array ( [table_rate-7 : 72] => WC_Shipping_Rate Object ( [id] => table_rate-7 : 72 [label] => Registered Australian Post (2 to 8 Business Days) [cost] => 4.0909 [taxes] => Array ( [1] => 0.40909 ) [method_id] => table_rate ) [table_rate-8 : 90] => WC_Shipping_Rate Object ( [id] => table_rate-8 : 90 [label] => Tracking and Freight Insurance [cost] => 20.055881818182 [taxes] => Array ( [1] => 2.0055881818182 ) [method_id] => table_rate ) [table_rate-5 : 70] => WC_Shipping_Rate Object ( [id] => table_rate-5 : 70 [label] => Nation-Wide Delivery (5 to 12 Business Days) [cost] => 0 [taxes] => Array ( ) [method_id] => table_rate ) [local_pickup] => WC_Shipping_Rate Object ( [id] => local_pickup [label] => Local Pickup [cost] => 0 [taxes] => Array ( ) [method_id] => local_pickup ) ) ----- table_rate-7 : 72 -----
我一直在这一整天工作,感觉没有更接近解决方案。希望这里有人能帮忙。
因此,您希望“表格费率”始终是默认的运输方式?您正在使用哪个版本的WooCommerce?还有一个说明'woocommerce_shipping_chosen_method'是一个过滤器钩子而不是一个动作钩子。因此,请使用'add_filter'而不是'add_action'并返回您的默认送货方式 –
表格速率已经是默认的送货方式了 - 我希望表格速率选项1是默认值,但不是表格速率选项3。 –
PS。当我听到你在说什么的时候,我太了解add_filter和add_action之间的区别了,我真的不明白。 –