2016-03-30 24 views
3

WooCommerceはテーブルレート配送方法の優先順位を決めることができないため、私はそれを自分で実行しようとしていますが、ひどく失敗しています。WooCommerce:デフォルトの配送方法をプログラムで変更する

私はそれが設定されていると思うが動作していないと思うような時点でアクションを追加しようとしました。私は$available_methods$chosen_methodテストしてきたとの両方が(ページで実行した場合)期待通りに提示されている

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'); 

しかし:ここに私がしようとしているものです(私はちょうど利用できる配送方法の配列の最初の配送方法を使用します) 、一度私はアクションにそれを追加する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_action'の代わりに' add_filter'を使用してデフォルトの配送方法を返します。 –

+0

テーブルレートはデフォルトの配送方法です。テーブルレートオプション3ではなくテーブルレートオプション1をデフォルトにします。 –

+0

PS。 add_filterとadd_actionの違いを理解するのは初心者の方がはるかに多いので、あなたが言っていることを聞いている間は、私はそれを本当に理解していません。 –

答えて

1

woocommerce_shipping_chosen_methodは、フィルタフックではなく、アクションフックです。私はあなたに要点を与え、所望1にそれを変更するデフォルトの方法としてtable_rate-5ハードコードされている次のコード

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を試してみてください。理想的には、コードを記述して、デフォルトとして設定したいメソッドが$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); 

は(注:これは働いていた私が欲しかった出荷率は、実際にリスト内の最初だったが、ただ、デフォルトで選択されていなかったので、 )

関連する問題