Wordpress woocommerce优惠券-获取所有列出的产品ID
我在一个网站上工作,当有人注册时生成优惠券,我有代码-Wordpress woocommerce优惠券-获取所有列出的产品ID,wordpress,woocommerce,Wordpress,Woocommerce,我在一个网站上工作,当有人注册时生成优惠券,我有代码- $coupon_code = "ex".$resulted_coupon_code ;// Code $amount = '20'; // Amount $discount_type = 'percent_product'; // Type: fixed_cart, percent, fixed_product, percent_product $coupon = array( 'post_title' => $coup
$coupon_code = "ex".$resulted_coupon_code ;// Code
$amount = '20'; // Amount
$discount_type = 'percent_product'; // Type: fixed_cart, percent, fixed_product, percent_product
$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$new_coupon_id = wp_insert_post( $coupon );
// Add meta
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_po st_meta( $new_coupon_id, 'individual_use', 'yes' );
update_post_meta( $new_coupon_id, 'product_ids', '4454,4452,4451,4449' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '1' );
update_post_meta( $new_coupon_id, 'usage_limit_per_user', '1' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
update_post_meta( $new_coupon_id, 'customer_email', $user_email );
现在如何获取优惠券产品ID??
像这样
更新后元($new_优惠券id,'product_id','44544524451449')
我想检索这些445445244514449WooCommerce正在将这些值存储在
postETA
表中,因此您可以使用来检索产品ID
get_post_meta( $new_coupon_id, 'product_ids', true );
要首先获取所有优惠券帖子类型,然后循环获取产品,然后循环获取产品帖子类型,您可以使用以下方法:
// get all coupons that are published
$coupons = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'shop_coupon',
'post_status' => 'publish',
) );
// loop through the coupons
foreach ( $coupons as $coupon ){
// get the product ids meta value
$product_ids = get_post_meta( $coupon->ID, 'product_ids', true );
// make sure something has been saved
if ( !empty( $product_ids ){
// convert from comma separated string to array
$id_list = explode( ',', $product_ids );
// loop over each ID
foreach( $id_list as $product_id ){
// get the product for each ID
$product = get_post( $product_id );
// each product associated
}
}
}
试着使用我正在处理的代码来获得这些$fields=get\u post\u meta($post->ID,$ID);如果foreach($fields as$field){return$field;}有效,它将在此处更新。
$id
?它必须是meta_键
,除非它是一个数组(它不是)。是的,工作正常,感谢完整的代码,这是救命的:)这怎么有效?4461
ID来自哪里?更新不会返回ID,它只会设置ID。实际上,它的另一个主优惠券已经内置在WooCommerce中。我猜你是在使用@RamanSingh吗?这不是一个有效的答案,因此,它包括特定于您的应用程序的信息,对其他人没有帮助。是的,事实上,它的另一个主优惠券已经在woo commerce中创建。我只想克隆它-就像当有人注册时,自动为该用户生成优惠券,但我不想在优惠券中的产品发生变化时编辑所有优惠券,因此我创建了一个主优惠券,它从中获取所有产品ID和其他信息,并为新用户克隆。你给了我们正确的想法,非常感谢!
update_post_meta( $new_coupon_id, 'product_ids', get_post_meta( 4461, 'product_ids', true ));