2016-03-30 106 views
3

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 
-----

我一直在这一整天工作,感觉没有更接近解决方案。希望这里有人能帮忙。

+0

因此,您希望“表格费率”始终是默认的运输方式?您正在使用哪个版本的WooCommerce?还有一个说明'woocommerce_shipping_chosen_method'是一个过滤器钩子而不是一个动作钩子。因此,请使用'add_filter'而不是'add_action'并返回您的默认送货方式 –

+0

表格速率已经是默认的送货方式了 - 我希望表格速率选项1是默认值,但不是表格速率选项3。 –

+0

PS。当我听到你在说什么的时候,我太了解add_filter和add_action之间的区别了,我真的不明白。 –

回答

1

woocommerce_shipping_chosen_method是一个过滤器钩子而不是一个动作钩子。试试下面的代码

function reset_default_shipping_method($method, $available_methods) { 

    // If the shipping method has been chosen don't do anything 
    if (! empty($method)) { 
     return $method; 
    }   

    // add code to set 'Table Rate' as the default shipping method 

    $method = 'table_rate-5'; 

    return $method;  
} 

add_filter('woocommerce_shipping_chosen_method', 'reset_default_shipping_method', 10, 2); 

P.S:我已经硬编码table_rate-5作为默认的方法给你的要点,所以将其更改为所需的一个。理想情况下,您需要编写代码以查看您想要设置为默认的方法是否在$available_methods中可用,然后进行相应的工作。

+0

我硬编码以及尝试强制它,它仍然不会更新。 –

+0

这看起来是重复的http://stackoverflow.com/questions/36297571/woocommerce-how-to-set-a-default-table-rate-shipping-method – Lutrov

+0

我做到了这一点,该方法并没有改变了 –

1

这是我最终使用的要求,其工作:

/*=Use the shipping method filter to set the "selected" shipping method to 
* the first (default) method in the list 
**************************************************************************/ 
function oley_reset_default_shipping_method($method, $available_methods) { 

    $method = key($available_methods); 
    return $method; 

} 
add_filter('woocommerce_shipping_chosen_method', 'oley_reset_default_shipping_method', 10, 2); 

(注:这工作,因为我想要的运费竟是在列表中的第一个,但只是没有被默认选中)

相关问题