8

Woocommerce coupon - get all identified product identifiers

 2 years ago
source link: https://www.codesd.com/item/woocommerce-coupon-get-all-identified-product-identifiers.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Woocommerce coupon - get all identified product identifiers

advertisements

I working on a website that generate coupon when someone register i have code it -

$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 );

now how can i to get the coupon products ids ?? like from this

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

i want to retrieve these 4454,4452,4451,4449


WooCommerce is storing these values in the postmeta table, so you can use the get_post_meta() to retrieve the product IDs.

get_post_meta( $new_coupon_id, 'product_ids', true );

To get all of the coupon post types first, then loop over them to get the products, then loop over them to get the product post types, you would use the following:

// 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
        }
    }
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK