Create a Woocommerce product when post is created - wordpress

I am using Woocommerce on my WordPress site. and I am Selling Various items on my site.
What I want is that every time I create a small post about a particular item.it also creates a Woocommerce product page with the one item available to be sold.
For example: I create a post about custom-made jewelry and I write a small post about it, and the customer can look at the post and buy it from the Woocommerce product section.
Once the product is out of stock the post disappears"Hidden" until I have them in stock.
How this can be done? Any ideas?

Add this to your function file and replace empty strings with variable where necessary. This should solve your problem.
add_action( 'save_post', 'auto_create_product_from_post', 100, 2 );
function auto_create_product_from_post($id, $post){
$post_id = wp_insert_post( array(
//'post_title' => 'Adams Product',
'post_title' => $post.post_title,
'post_content' => $post.post_title,
'post_status' => 'publish',
'post_type' => "product",
) );
wp_set_object_terms( $post_id, 'simple', 'product_type' );
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, 'total_sales', '0' );
update_post_meta( $post_id, '_downloadable', 'no' );
update_post_meta( $post_id, '_virtual', 'yes' );
update_post_meta( $post_id, '_regular_price', '' );
update_post_meta( $post_id, '_sale_price', '' );
update_post_meta( $post_id, '_purchase_note', '' );
update_post_meta( $post_id, '_featured', 'no' );
update_post_meta( $post_id, '_weight', '' );
update_post_meta( $post_id, '_length', '' );
update_post_meta( $post_id, '_width', '' );
update_post_meta( $post_id, '_height', '' );
update_post_meta( $post_id, '_sku', '' );
update_post_meta( $post_id, '_product_attributes', array() );
update_post_meta( $post_id, '_sale_price_dates_from', '' );
update_post_meta( $post_id, '_sale_price_dates_to', '' );
update_post_meta( $post_id, '_price', '' );
update_post_meta( $post_id, '_sold_individually', '' );
update_post_meta( $post_id, '_manage_stock', 'no' );
update_post_meta( $post_id, '_backorders', 'no' );
update_post_meta( $post_id, '_stock', '' );
}

Related

Generate WC coupon programatically when order is processing - coupon code is wrong

Checked already dozens of articles and questions posted here - used tips from each of them already :)
My goal is to generate coupon with pre-defined details for specific product id, once payment is confirmed. And I want it to be generated manually. Here is my code:
This is quite long but working function for creating coupon for specific email address.
function bp_generate_coupon_for_training_packs( string $email ) {
// check id of user
$user = get_user_by( 'email', $email );
if ( $user ) {
$userid = $user->ID;
}
else {
$userid = 'unknown';
}
// generate random coupon code
$characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$char_length = "15";
$random_string = substr( str_shuffle( $characters ), 0, $char_length );
// today date
$date = date_create();
$today = date_format( $date, "Ymd" );
// coupon name
$coupon_code = $userid . '_treningx4_' . $today . '_' . $random_string;
// set expiry date
$date->add( new DateInterval( 'P30D' ) ); // add 30 days to expiry date
$date_expires = date_timestamp_get( $date );
$expiry_date = date_format( $date, "d.m.Y" ); // correct the format
$schedule_end = date_format( $date, "d.m.Y H:i" ); // here too
// coupon details
$new_coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
// restrict coupon
$roles = [ "customer", "wpamelia-customer" ];
$restrictions = array_map( 'sanitize_text_field', $roles );
// new coupon creation
$new_coupon_id = wp_insert_post( $new_coupon );
// post meta
update_post_meta( $new_coupon_id, 'discount_type', 'percent' );
update_post_meta( $new_coupon_id, 'coupon_amount', '100' );
update_post_meta( $new_coupon_id, 'individual_use', 'yes' );
update_post_meta( $new_coupon_id, 'product_ids', '2762' );
update_post_meta( $new_coupon_id, 'usage_limit', '4' );
update_post_meta( $new_coupon_id, 'usage_limit_per_user', '0' );
update_post_meta( $new_coupon_id, 'limit_usage_to_x_items', '1' );
update_post_meta( $new_coupon_id, 'usage_count', '0' );
update_post_meta( $new_coupon_id, 'date_expires', $date_expires );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
update_post_meta( $new_coupon_id, 'exclude_sale_items', 'no' );
update_post_meta( $new_coupon_id, 'customer_email', $email );
update_post_meta( $new_coupon_id, '_acfw_disable_url_coupon', 'yes' );
update_post_meta( $new_coupon_id, '_acfw_force_apply_url_coupon', 'yes' );
update_post_meta( $new_coupon_id, '_acfw_code_url_override', '' );
update_post_meta( $new_coupon_id, '_acfw_enable_role_restriction', 'yes' );
update_post_meta( $new_coupon_id, '_acfw_role_restrictions_type', 'allowed' );
update_post_meta( $new_coupon_id, '_acfw_role_restrictions_error_msg', 'Nie mozesz skorzystac z tej promocji.' );
update_post_meta( $new_coupon_id, '_acfw_cart_conditions', '' );
update_post_meta( $new_coupon_id, '_acfw_cart_condition_notice', '' );
update_post_meta( $new_coupon_id, '_acfw_role_restrictions', $restrictions );
update_post_meta( $new_coupon_id, '_acfw_bogo_deals', '' );
update_post_meta( $new_coupon_id, '_acfw_success_message', '' );
update_post_meta( $new_coupon_id, '_acfw_after_redirect_url', '' );
update_post_meta( $new_coupon_id, '_acfw_bogo_auto_add_products', '' );
update_post_meta( $new_coupon_id, '_acfw_add_before_conditions', '' );
update_post_meta( $new_coupon_id, '_acfw_add_products_data', '' );
update_post_meta( $new_coupon_id, '_acfw_excluded_coupons', '' );
update_post_meta( $new_coupon_id, '_acfw_shipping_overrides', '' );
update_post_meta( $new_coupon_id, '_acfw_schedule_start', '' );
update_post_meta( $new_coupon_id, '_acfw_schedule_end', $schedule_end );
update_post_meta( $new_coupon_id, '_acfw_schedule_start_error_msg', '' );
update_post_meta( $new_coupon_id, '_acfw_schedule_expire_error_msg', 'Ten pakiet juz wygasl.' );
update_post_meta( $new_coupon_id, '_acfw_apply_notification_message', '' );
update_post_meta( $new_coupon_id, '_acfw_apply_notification_btn_text', '' );
update_post_meta( $new_coupon_id, '_acfw_apply_notification_type', 'info' );
update_post_meta( $new_coupon_id, '_acfw_reset_usage_limit_period', 'none' );
update_post_meta( $new_coupon_id, '_acfw_loyalty_program_user', '' );
update_post_meta( $new_coupon_id, '_acfw_loyalty_program_points', '' );
update_post_meta( $new_coupon_id, '_acfw_cart_condition_display_notice_auto_apply', '' );
update_post_meta( $new_coupon_id, 'expiry_date', $expiry_date );
update_post_meta( $new_coupon_id, '_acfw_usage_limit_reset_time', '' );
// id of coupon category
$cat_id = array( 164 );
$term_id = array_map( 'intval', $cat_id );
$term_id = array_unique( $term_id );
wp_set_post_terms( $new_coupon_id, $term_id, 'shop_coupon_cat' );
}
This one creates a coupon once status of order is changed to processing (after payment):
add_action( 'woocommerce_order_status_completed', 'bp_generate_coupon_after_payment' );
function bp_generate_coupon_after_payment( $order_id ) {
$order = wc_get_order( $order_id );
$email = $order->get_billing_email();
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item[ 'product_id' ];
if ( $product_id == '2804' ) {
bp_generate_coupon_for_training_packs( $email );
}
}
}
When creating coupon using a function bp_generate_for_training_packs( 'test#email.pl' ), it works just fine. But when function is called in bp_generate_coupon_after_payment(), it creates a coupon with code:
order – 4th December, 2020 # 09:19 pm and user's email address is not put into customer_email field of coupon.
Any tips are welcome :) Thanks in advance!

how to get one order for each item in woocommerce

I wants to receive one order for each item(of an order).
For example:
If we get a order of three items then admin->WooCommerce->Orders will show three different orders for each item.
Any idea?
Thanks....
Please use this code in your child theme function.php. I hope this code will you to achieve your goal.
Note : I would suggest please use this code in your local setup first.
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
global $wp,$wpdb;
if(!empty($wp->query_vars['order-received'])){
$order = new WC_Order($wp->query_vars['order-received']);
$arrayCount = array();
$productIDArray = array();
$productQtyArray = array();
foreach($order->get_items() as $originalOrderItem){
createNewOrderAndRecordPayment($wp->query_vars['order-
received'],$originalOrderItem['item_meta']['_product_id']
[0],$originalOrderItem['item_meta']['_qty'][0]);
}
wp_redirect( '/thank-you/' );
exit;
}
}
function createNewOrderAndRecordPayment($orderID,$productID,$qty) {
global $wpdb;
global $woocommerce;
$original_order_id = $orderID;
$original_order = new WC_Order($original_order_id);
$currentUser = wp_get_current_user();
$order_data = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'ping_status' => 'closed',
'post_author' => $currentUser->ID,
'post_excerpt' => $original_order->customer_message,
'post_password' => uniqid( 'order_' ) // Protects the post just in case
);
$order_id = wp_insert_post( $order_data, true );
if ( is_wp_error( $order_id ) ){
$msg = "Unable to create order:" . $order_id->get_error_message();;
throw new Exception( $msg );
} else {
$order = new WC_Order($order_id);
update_post_meta( $order_id, '_order_shipping',
get_post_meta($original_order_id, '_order_shipping', true) );
update_post_meta( $order_id, '_order_ip_value', get_client_ip());
update_post_meta( $order_id, '_order_discount',
get_post_meta($original_order_id, '_order_discount', true) );
update_post_meta( $order_id, '_cart_discount',
get_post_meta($original_order_id, '_cart_discount', true) );
update_post_meta( $order_id, '_order_tax',
get_post_meta($original_order_id, '_order_tax', true) );
update_post_meta( $order_id, '_order_shipping_tax',
get_post_meta($original_order_id, '_order_shipping_tax', true) );
if($orderLoop == 1){
update_post_meta( $order_id, '_order_total',
get_post_meta($original_order_id, '_order_total', true) );
}
update_post_meta( $order_id, '_order_key', 'wc_' .
apply_filters('woocommerce_generate_order_key', uniqid('order_') ) );
update_post_meta( $order_id, '_customer_user',
get_post_meta($original_order_id, '_customer_user', true) );
update_post_meta( $order_id, '_order_currency',
get_post_meta($original_order_id, '_order_currency', true) );
if($orderLoop == 1){
update_post_meta( $order_id,
'_prices_include_tax',get_post_meta($original_order_id,
'_prices_include_tax', true) );
}
update_post_meta( $order_id,
'_customer_ip_address',get_post_meta($original_order_id,
'_customer_ip_address', true) );
update_post_meta( $order_id,
'_customer_user_agent',get_post_meta($original_order_id,
'_customer_user_agent', true) );
update_post_meta( $order_id, '_billing_city',
get_post_meta($original_order_id, '_billing_city', true));
update_post_meta( $order_id, '_billing_state',
get_post_meta($original_order_id, '_billing_state', true));
update_post_meta( $order_id, '_billing_postcode',
get_post_meta($original_order_id, '_billing_postcode', true));
update_post_meta( $order_id, '_billing_email',
get_post_meta($original_order_id, '_billing_email', true));
update_post_meta( $order_id,
'_billing_phone',get_post_meta($original_order_id, '_billing_phone', true));
update_post_meta( $order_id,
'_billing_address_1',get_post_meta($original_order_id, '_billing_address_1',
true));
update_post_meta( $order_id,
'_billing_address_2',get_post_meta($original_order_id, '_billing_address_2',
true));
update_post_meta( $order_id,
'_billing_country',get_post_meta($original_order_id, '_billing_country',
true));
update_post_meta( $order_id,
'_billing_first_name',get_post_meta($original_order_id,
'_billing_first_name', true));
update_post_meta( $order_id,
'_billing_last_name',get_post_meta($original_order_id, '_billing_last_name', true));
update_post_meta( $order_id,
'_billing_company',get_post_meta($original_order_id, '_billing_company',
true));
update_post_meta( $order_id,
'_shipping_country',get_post_meta($original_order_id, '_shipping_country',
true));
update_post_meta( $order_id,
'_shipping_first_name',get_post_meta($original_order_id,
'_shipping_first_name', true));
update_post_meta( $order_id,
'_shipping_last_name',get_post_meta($original_order_id,
'_shipping_last_name', true));
update_post_meta( $order_id,
'_shipping_company',get_post_meta($original_order_id, '_shipping_company',
true));
update_post_meta( $order_id,
'_shipping_address_1',get_post_meta($original_order_id,
'_shipping_address_1', true));
update_post_meta( $order_id,
'_shipping_address_2',get_post_meta($original_order_id,
'_shipping_address_2', true));
update_post_meta( $order_id, '_shipping_city',
get_post_meta($original_order_id, '_shipping_city', true));
update_post_meta( $order_id,
'_shipping_state',get_post_meta($original_order_id, '_shipping_state',
true));
update_post_meta( $order_id,
'_shipping_postcode',get_post_meta($original_order_id, '_shipping_postcode',
true));
update_post_meta( $order_id, '_order_delivery_date',$deliveryDate);
$itemName = get_the_title($productID);
$quantity = $qty;
$lineTotal = $originalOrderItem['line_total'];
$lineTax = $originalOrderItem['line_tax'];
$productID = $productID;
$item_id = wc_add_order_item( $order_id, array(
'order_item_name' => $itemName,
'order_item_type' => 'line_item'
) );
wc_add_order_item_meta( $item_id, '_qty', $quantity );
wc_add_order_item_meta( $item_id, '_product_id', $productID );
wc_add_order_item_meta( $item_id, '_line_subtotal',
wc_format_decimal( $lineTotal ) );
wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal(
$lineTotal ) );
wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( '0' ) );
wc_add_order_item_meta( $item_id, '_line_subtotal_tax',
wc_format_decimal( '0' ) );
$original_order_shipping_items =
$original_order->get_items('shipping');
foreach ( $original_order_shipping_items as
$original_order_shipping_item ) {
$ShippingItemID = wc_add_order_item( $order_id, array(
'order_item_name' =>
$original_order_shipping_item['name'],
'order_item_type' => 'shipping'
) );
if ( $ShippingItemID ) {
wc_add_order_item_meta( $ShippingItemID, 'method_id',
$original_order_shipping_item['method_id'] );
wc_add_order_item_meta( $ShippingItemID, 'cost',
wc_format_decimal( $original_order_shipping_item['cost'] ) );
}
}
update_post_meta( $order_id, '_payment_method',
get_post_meta($original_order_id, '_payment_method', true) );
update_post_meta( $order_id, '_payment_method_title',
get_post_meta($original_order_id, '_payment_method_title', true) );
update_post_meta( $order->id, 'Transaction ID',
get_post_meta($original_order_id, 'Transaction ID', true) );
$order->payment_complete();
$order->update_status('processing');
}
return $order_id;
}

How to convert posts as products with there categories?

I am working on woocommerce product Convert.I am converting post into woocommerce products on click of import button.Convert is working fine but i have created same categories as in posts and i need to import products in the same categories as posts. for example i have categories in posts.
Style - main cat
Men -- Sub Cat
Clothing -- Men Sub Cat
Shirt
Belts
Women -- Sub cat
Clothing -- Women Sub Cat
Top
Purse
And i have created same categories in woocommerce. Now when i am importing posts then if a post is in Women Clothing category then the imported product should go to Women Clothing category.Products should import according to categories and sub categories.This is my code to convert post as products.
foreach ($items as $post) {
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
$getpostid = $wpdb->get_results( "
SELECT post_id
FROM " . $wpdb->prefix ."postmeta AS pm
INNER JOIN ".$wpdb->prefix."posts AS p
ON p.ID=pm.post_id
WHERE pm.`meta_key` = 'admitted_goods_id'
AND pm.`meta_value` ='".$post->ID."'
AND p.post_status='publish'
");
foreach($getpostid as $k=>$postdata) {
$postId = $postdata->post_id;
$admited_goodsId[$k] = get_post_meta($postId, "admitted_goods_id", true);
}
if($post->ID==$admited_goodsId[0]) {
echo "Updated";
} else {
//echo "Insert";
$post_id = wp_insert_post( array(
'post_title' => $post->post_title,
'post_content' => $post->post_content,
'post_status' => 'publish',
'post_type' => "product",
) );
wp_set_object_terms( $post_id, 'external', 'product_type' );
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_downloadable', 'no' );
update_post_meta( $post_id, '_thumbnail_id',$post_thumbnail_id);
update_post_meta( $post_id, 'admit_ds_id',$post->ID);
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, '_button_text','Buy Now');
update_post_meta( $post_id, '_brand', $pdata->vendor );
update_post_meta( $post_id, '_model', $pdata->model );
$term_list = wp_get_post_terms($post->ID, 'adm_category', array("fields" => "all"));
foreach($term_list as $term) {
wp_set_post_terms( $post_id, $term->name, 'product_cat', false );
}
$getOtherdata = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix ."admitad_product_data where post_id=".$post->ID);
foreach($getOtherdata as $pdata) {
//echo $pdata->price;
update_post_meta( $post_id, '_regular_price', $pdata->old_price );
update_post_meta( $post_id, '_sale_price', $pdata->price );
update_post_meta( $post_id, '_product_url',$pdata->url);
update_post_meta( $post_id, '_price', $pdata->price );
update_post_meta( $post_id, '_currencyId', $pdata->currencyId );
}
}
}
There were two errors in your code that I fixed please check below:
<?php
foreach ($items as $post) {
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
$getpostid = $wpdb->get_results( "SELECT post_id FROM " . $wpdb->prefix ."postmeta as pm inner join ".$wpdb->prefix."posts as p on p.ID=pm.post_id WHERE pm.`meta_key` = 'admitted_goods_id' AND pm.`meta_value` ='".$post->ID."' and p.post_status='publish' ");
foreach($getpostid as $k=>$postdata)
{
$postId = $postdata->post_id;
$admited_goodsId[$k] = get_post_meta($postId, "admitted_goods_id", true);
}
if($post->ID==$admited_goodsId[0])
{
echo "Updated";
}else
{
//echo "Insert";
$post_id = wp_insert_post( array(
'post_title' => $post->post_title,
'post_content' => $post->post_content,
'post_status' => 'publish',
'post_type' => "product",
) );
wp_set_object_terms( $post_id, 'external', 'product_type' );
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_downloadable', 'no' );
update_post_meta($post_id,'_thumbnail_id',$post_thumbnail_id);
update_post_meta($post_id,'admit_ds_id',$post->ID);
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta($post_id,'_button_text','Buy Now');
update_post_meta( $post_id, '_brand', $pdata->vendor );
update_post_meta( $post_id, '_model', $pdata->model );
//$term_list = wp_get_post_terms($post->ID, 'adm_category', array("fields" => "all"));
$term_list = wp_get_post_categories( $post->ID );
// $term_list variable is an array containing IDs of all the post categories
// foreach($term_list as $term)
// {
// wp_set_post_terms( $post_id, $term->name, 'product_cat', false );
// }
// wp_set_post_term can't be used to update Product Category/Terms since this function can only be used to update Terms of native post type not custom post type. In order to update Terms of custom post type which is Products in our case we need to use wp_set_object_terms function.
wp_set_object_terms( $post_id, $term_list, 'product_cat' );
$getOtherdata = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix ."admitad_product_data where post_id=".$post->ID);
foreach($getOtherdata as $pdata)
{
//echo $pdata->price;
update_post_meta( $post_id, '_regular_price', $pdata->old_price );
update_post_meta( $post_id, '_sale_price', $pdata->price );
update_post_meta($post_id,'_product_url',$pdata->url);
update_post_meta( $post_id, '_price', $pdata->price );
update_post_meta( $post_id, '_currencyId', $pdata->currencyId );
}
}
}
?>

How to import product into wordpress woocommerce from url and get command

I want to import products into word-press from URL.
I want to know the product table fully, I mean that I want to map my URL tables with woo commerce table. What are the products and attributes and prices and variables table in woo commerce?
For example
INSERT INTO `catalog_product_website` (`product_id`, `website_id`) VALUES
The Wordpress Woocommerce stores products informations in wp_post (post_type = product) and in wp_postmeta (meta_key and meta_value for a post_id).
So, to store new products that way, you will have to do something like this :
To add new product in the wp_post table :
$post = array(
'post_author' => $user_id,
'post_content' => '',
'post_status' => "publish",
'post_title' => $product->part_num,
'post_parent' => '',
'post_type' => "product",
);
//Create post
$post_id = wp_insert_post( $post, $wp_error );
if($post_id){
$attach_id = get_post_meta($product->parent_id, "_thumbnail_id", true);
add_post_meta($post_id, '_thumbnail_id', $attach_id);
}
To set the category and the type of the new product :
wp_set_object_terms( $post_id, 'Races', 'product_cat' );
wp_set_object_terms($post_id, 'simple', 'product_type');
And then set its values into wp_postmeta table :
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, 'total_sales', '0');
update_post_meta( $post_id, '_downloadable', 'yes');
update_post_meta( $post_id, '_virtual', 'yes');
update_post_meta( $post_id, '_regular_price', "1" );
update_post_meta( $post_id, '_sale_price', "1" );
update_post_meta( $post_id, '_purchase_note', "" );
update_post_meta( $post_id, '_featured', "no" );
update_post_meta( $post_id, '_weight', "" );
update_post_meta( $post_id, '_length', "" );
update_post_meta( $post_id, '_width', "" );
update_post_meta( $post_id, '_height', "" );
update_post_meta( $post_id, '_sku', "");
update_post_meta( $post_id, '_product_attributes', array());
update_post_meta( $post_id, '_sale_price_dates_from', "" );
update_post_meta( $post_id, '_sale_price_dates_to', "" );
update_post_meta( $post_id, '_price', "1" );
update_post_meta( $post_id, '_sold_individually', "" );
update_post_meta( $post_id, '_manage_stock', "no" );
update_post_meta( $post_id, '_backorders', "no" );
update_post_meta( $post_id, '_stock', "" );
Hope it helps.
Source : How to add product in woocommerce with php code

woocommerce coupon - get all listed products ids

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
}
}
}
update_post_meta( $new_coupon_id, 'product_ids', get_post_meta( 4461, 'product_ids', true ));
try to use that code

Resources