1

Woocommerceチェックアウトフィールドからデータを取得して、オーダー処理時にサブスクリプションデータを更新できないのはなぜですか?WooCommerceサブスクリプションオーダーでチェックアウトカスタムフィールド値を取得

私は、チェックアウト時の顧客の頻度要求に基づいて次の更新日を更新したいと考えています。

1)チェックアウトフィールドを設定します:

add_action('woocommerce_after_order_notes', 'hgf_custom_checkout_field'); 

function hgf_custom_checkout_field($checkout) { 
global $woocommerce; 

    woocommerce_form_field('frequency', array(
     'type'   => 'select', 
     'required'   => true, 
     'class'   => array('form-row-wide'), 
     'label'   => __('Change this any time, just email.'), 
     'options'  => array(
      'blank'  => __('Select frequency preference', 'woocommerce'),    
      1 => __('Every Week', 'woocommerce'), 
      2 => __('Every Other Week' , 'woocommerce'), 
      3 => __('Once Per Month' , 'woocommerce') 
       ) 
      ), $checkout->get_value('frequency')); 
      echo '</div>'; 
} 

2)オーダーのメタを更新してここに私のコードです。これは、顧客の注文上の2つのカスタムフィールドを作成し、同様にサブスクリプションの課金期間更新:

add_action('woocommerce_checkout_update_order_meta', 'hgf_checkout_field_update_order_meta'); 
function hgf_checkout_field_update_order_meta($order_id) { 

    if (! empty($_POST['frequency'])) { 

     update_post_meta($order_id, 'frequency', $_POST['frequency']); 
     update_post_meta($order_id, 'billing_interval', $_POST['frequency']); 
    } 

} 

3)更新日
は最後の部分については、私はダミーで更新日を更新することができます更新します例えば、私が選んだの日付(:。$renewal date = "2018-12-15"、私はそれが'billing_interval'フィールドを読むために取得しようとすると、それはelseデフォルトに戻り

add_action('woocommerce_checkout_create_subscription', 'hgf_next_renewal'); 
    function hgf_next_renewal($subscription, $timezone='site') { 

     $billing_interval = get_post_meta(get_the_ID(), 'billing_interval', true); 

    if($billing_interval == 2) { 
     $renewal_date = date('Y-m-d H:i:s', strtotime("2017-10-01")) /* date('Y-m-d H:i:s', strtotime("+2 weeks")) */ ; 
    } if($billing_interval == 4) { 
     $renewal_date = date('Y-m-d H:i:s', strtotime("2017-12-01")) /* date('Y-m-d H:i:s', strtotime("+4 weeks")) */ ; 
    } else { 
     $renewal_date = date('Y-m-d H:i:s')) /* date('Y-m-d H:i:s', strtotime($renewal_date)) */ ; 
    } 

     $subscription->update_dates(array(
      'next_payment' => $renewal_date, 
      )); 

     return $subscription; 
    } 

は、これは私が試したすべての異なった方法である:

1) $billing_interval = $subscription->get_billing_interval(); 
2) $billing_interval = get_post_meta($subscription->get_ID(), 'billing_interval', true); 
3) $billing_interval = get_post_meta($order_id, 'billing_interval', true); 
4) $subscriptions = wcs_get_users_subscriptions($user_id); 
    foreach ($subscriptions as $sub) { 
     $billing_interval = get_post_meta($sub->get_order_number(), '_billing_interval', true); 
     } 
5) $billing_interval = $checkout->get_value('frequency'); 

'woocommerce_checkout_create_subscription'アクション中に私のチェックアウトフィールドから値を取得できない理由を知っている人はいますか?

答えて

1

あなたが注文メタデータにあなたの'billing_interval'カスタムフィールドを保存すると、サブスクリプションオブジェクトまたはIDでこのカスタムフィールドを取得することはできません...あなたが最初親注文IDを取得する必要があります。

あなたはWC_Order method get_parent_id()

だから、あなたがどこだけ$renewal_dateあなたの「他」を取得することを完全に正常だったを使用する必要がサブスクリプションオブジェクトから親注文IDを取得するには...

このようにして関数内で設定します:

add_action('woocommerce_checkout_create_subscription', 'hgf_next_renewal'); 
function hgf_next_renewal($subscription, $timezone='site') { 

    // MISSING!: The parent order ID to set below 
    $order_parent_id = $subscription->get_parent_id(); 

    $billing_interval = get_post_meta($order_parent_id, 'billing_interval', true); 

    if($billing_interval == 2) { 
     $renewal_date = date('Y-m-d H:i:s', strtotime("2017-10-01")) /* date('Y-m-d H:i:s', strtotime("+2 weeks")) */ ; 
    } elseif($billing_interval == 4) { 
     $renewal_date = date('Y-m-d H:i:s', strtotime("2017-12-01")) /* date('Y-m-d H:i:s', strtotime("+4 weeks")) */ ; 
    } else { 
     $renewal_date = date('Y-m-d H:i:s', strtotime("2017-09-01")) /* date('Y-m-d H:i:s') */ ; 
    } 

    $subscription->update_dates(array(
     'next_payment' => $renewal_date, 
    )); 

    return $subscription; 
} 

コードはあなたのアクティブな子供のテーマ(またはテーマ)のfunction.phpファイル、またはすべてのプラグインファイルに入ります。

私はあなたのコードを実際にテストしていませんが、何もエラーを投げず、今度はあなたのために働くはずです。

+0

感謝します!私は何時間も何もしていなかったので、あなたの提案はそれを解決しました。ありがとうございました!!! – aleks1217

+0

もし私があなたに50票を与えることができたら、私は願っています。 :-D – aleks1217

+0

@ aleks1217これはちょうど非常に良いです...ありがとう:) – LoicTheAztec