爲什麼我無法從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)更新的順序元。這將創建客戶訂單上的兩個自定義字段,以及更新的訂閱的計費週期:
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'
操作中?
謝謝!我幾個小時(幾天)就沒有這個機會了,你的建議解決了它。謝謝!!! – aleks1217
如果我可以給你50票我woulda'。 :-D – aleks1217
@ aleks1217這一個是非常好的......謝謝:) – LoicTheAztec