【问题标题】:woocommerce coupon - get all listed products idswoocommerce 优惠券 - 获取所有列出的产品 ID
【发布时间】:2015-06-07 15:00:15
【问题描述】:

我在一个网站上工作,当有人注册时会生成优惠券我有代码 -

$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 ?? 喜欢这个

update_post_meta($new_coupon_id, 'product_ids', '4454,4452,4451,4449');

我想检索这些 4454,4452,4451,4449

【问题讨论】:

  • 我正在努力让这些像这样 $fields = get_post_meta( $post->ID, $id); foreach( $fields as $field ) { return $field; } 将在此处更新,如果它有效
  • 什么是$id?它必须是meta_key,除非它是一个数组(它不是),否则你将无法循环遍历结果。

标签: wordpress woocommerce


【解决方案1】:

WooCommerce 将这些值存储在 postmeta 表中,因此您可以使用 get_post_meta() 检索产品 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 
        }
    }
}

【讨论】:

    【解决方案2】:
    update_post_meta( $new_coupon_id, 'product_ids', get_post_meta( 4461, 'product_ids', true ));
    

    尝试使用该代码

    【讨论】:

    • 是的工作感谢完整的代码它是救命:)
    • 这如何有效? 4461 ID 是从哪里来的?更新不返回 ID,它只设置它们。
    • 其实它的另一个主优惠券已经内置在 woocommerce 中
    • 我猜你和@RamanSingh 一起工作?这不是 SO 的有效答案,它包含特定于您的应用程序且对其他人没有帮助的信息。
    • 是的,实际上它的另一个主优惠券已经在 woo-commerce 中创建。我只想克隆它 - 就像有人注册时一样,为该用户生成自动优惠券,但我不想在优惠券中的产品更改时编辑所有优惠券,所以我创建了一个主优惠券,从中获取所有产品 ID 和其他新用户的信息和克隆。您给了我们正确的想法,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 2020-05-27
    • 2021-01-21
    • 1970-01-01
    • 2021-12-31
    • 2019-12-06
    • 1970-01-01
    相关资源
    最近更新 更多