使用使用的优惠券代码WooCommerce填写自定义字段 [英] Fill out custom field with used coupon code WooCommerce
本文介绍了使用使用的优惠券代码WooCommerce填写自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在,我正在使用一个mailchimp插件,该插件需要一个用于验证/分段的自定义字段.
Right now I'm working with a mailchimp plugin that needs a custom field for validating/segmenting.
对于此细分,我想检查使用哪种优惠券.因此,我清除了以下代码,这些代码应将使用的优惠券填充到我的自定义字段中:
For this segment I want to check what kind of coupon is used. So I scavenged the following code that should fill my custom field with the used coupons:
add_action( 'woocommerce_checkout_update_order_meta',
'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( empty( $_POST['my_field_name'] ) ) {
$coupons = $order->get_used_coupons();
update_post_meta( $order_id, 'my_field_name', $coupons);
}
}
可悲的是,这只会使我的网站崩溃.
Sadly this will only crash my site.
任何帮助将不胜感激.
推荐答案
您的代码有几个问题:
- 您不验证该字段是否包含任何信息,因此您需要检查
$ _ POST
是否具有my_field_name密钥 - 您需要加载
$ order
变量以获取已使用的优惠券. - 当存在my_field_value时会发生什么?你存储吗?
- You're not validating if the field has any information, so you need
to check if
$_POST
has the my_field_name key - You need to load the
$order
variable in order to get the used coupons. - What happens when there's a my_field_value? Do you store it?
这是修改后的代码:
add_action( 'woocommerce_checkout_update_order_meta',
'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id, $posted ) {
if ( isset($_POST['my_field_name']) && empty( $_POST['my_field_name'])) {
$order = new WC_Order( $order_id );
$coupons = $order->get_used_coupons();
update_post_meta( $order_id, 'my_field_name', $coupons);
}
}
这篇关于使用使用的优惠券代码WooCommerce填写自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文