在WooCommerce订阅订单中获取结帐自定义字段值
问题描述:
为什么我无法从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'
操作中?
答
由于要保存在订单元数据的'billing_interval'
自定义字段,则无法通过认购对象或ID得到这个自定义字段......你需要得到第一母订单ID。
要从认购对象得到母订单ID您需要使用
WC_Order
methodget_parent_id()
所以这是完全正常的,你只有让你的“其他人” $renewal_date
...
所以你可以在你的功能下面设置它:
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文件或也以任何插件文件。
我没有测试你真正的代码,但它不会引发任何错误,应为你工作这段时间。
谢谢!我几个小时(几天)就没有这个机会了,你的建议解决了它。谢谢!!! – aleks1217
如果我可以给你50票我woulda'。 :-D – aleks1217
@ aleks1217这一个是非常好的......谢谢:) – LoicTheAztec