Php 获取订单中的签出自定义字段值
为什么我不能从Woocommerce签出字段中检索数据,以便在订单处理时更新订阅数据 我想根据客户结帐时的频率请求更新下一个续订日期。这是我的密码: 1) 设置签出字段:Php 获取订单中的签出自定义字段值,php,wordpress,woocommerce,checkout,woocommerce-subscriptions,Php,Wordpress,Woocommerce,Checkout,Woocommerce Subscriptions,为什么我不能从Woocommerce签出字段中检索数据,以便在订单处理时更新订阅数据 我想根据客户结帐时的频率请求更新下一个续订日期。这是我的密码: 1) 设置签出字段: add_action( 'woocommerce_after_order_notes', 'hgf_custom_checkout_field' ); function hgf_custom_checkout_field( $checkout ) { global $woocommerce; woocommerce
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>';
}
3) 更新更新日期对于最后一部分,我可以使用我选择的虚拟日期更新续订日期(例如:
$renewal date=“2018-12-15”
,但当我试图让它读取'billing\u interval'
字段时,它会恢复为其他默认值
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\u checkout\u create\u subscription'
操作中无法从我的签出字段中检索值吗?当您在订单元数据中保存'billing\u interval'
自定义字段时,您无法通过订阅对象或ID获取此自定义字段。您需要首先获取父订单ID
要从订阅对象获取父订单ID,需要使用
代码位于活动子主题(或主题)的function.php文件或任何插件文件中
我还没有真正测试过你的代码,但它不会抛出任何错误,这次应该对你有用。谢谢你!!我花了好几个小时(几天?)都没有收到任何效果,你的建议解决了它。谢谢!!!@aleks1217这个很好…谢谢:)
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' );
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;
}