Skip to content

Instantly share code, notes, and snippets.

@akshuvo
Last active May 28, 2022 14:37
Show Gist options
  • Save akshuvo/989cf7dad18851bd14dcdce97fd12131 to your computer and use it in GitHub Desktop.
Save akshuvo/989cf7dad18851bd14dcdce97fd12131 to your computer and use it in GitHub Desktop.
Add WooCommerce product programmatically
<?php
// Product Title
$post_title = 'Test Product';
// Add Product
$new_post = array(
'post_title' => $post_title,
'post_type' => 'product',
'post_status' = > 'draft',
'post_content' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
'post_excerpt' => 'Lorem Ipsum is simply dummy text'
);
// Catch post ID
$post_id = post_exists( $post_title ) or wp_insert_post( $new_post );
// Add/Update Product meta
// set product is simple/variable/grouped
wp_set_object_terms( $post_id, 'term name here', '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', '11' );
update_post_meta( $post_id, '_length', '11' );
update_post_meta( $post_id, '_width', '11' );
update_post_meta( $post_id, '_height', '11' );
update_post_meta( $post_id, '_sku', 'SKU11' );
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', '11' );
update_post_meta( $post_id, '_sold_individually', '' );
update_post_meta( $post_id, '_manage_stock', 'yes' );
wc_update_product_stock($post_id, $single['qty'], 'set');
update_post_meta( $post_id, '_backorders', 'no' );
// update_post_meta( $post_id, '_stock', $single['qty'] );
/**
* Attach images to product (feature/ gallery)
*/
function attach_product_thumbnail($post_id, $url, $flag){
/*
* If allow_url_fopen is enable in php.ini then use this
*/
$image_url = $url;
$url_array = explode('/',$url);
$image_name = $url_array[count($url_array)-1];
$image_data = file_get_contents($image_url); // Get image data
/*
* If allow_url_fopen is not enable in php.ini then use this
*/
// $image_url = $url;
// $url_array = explode('/',$url);
// $image_name = $url_array[count($url_array)-1];
// $ch = curl_init();
// curl_setopt ($ch, CURLOPT_URL, $image_url);
// // Getting binary data
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
// $image_data = curl_exec($ch);
// curl_close($ch);
$upload_dir = wp_upload_dir(); // Set upload folder
$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
$filename = basename( $unique_file_name ); // Create image file name
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// asign to feature image
if( $flag == 0){
// And finally assign featured image to post
set_post_thumbnail( $post_id, $attach_id );
}
// assign to the product gallery
if( $flag == 1 ){
// Add gallery image to product
$attach_id_array = get_post_meta($post_id,'_product_image_gallery', true);
$attach_id_array .= ','.$attach_id;
update_post_meta($post_id,'_product_image_gallery',$attach_id_array);
}
}
//set product feature image
attach_product_thumbnail($post_id, $single['coverImage'], 0);
foreach($single['screenshots'] as $screenshots){
//set gallery image
attach_product_thumbnail($post_id, $screenshots['url_original'], 1);
}
// Add Product Category
foreach($single['genres'] as $prod_cat){
if(!term_exists($prod_cat, 'product_cat')){
$term = wp_insert_term($prod_cat, 'product_cat');
array_push($tag, $term['term_id']);
} else {
$term_s = get_term_by( 'name', $prod_cat, 'product_cat' );
array_push($tag , $term_s->term_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment