Skip to content

Instantly share code, notes, and snippets.

@the-nerdery-dot-info
Last active January 13, 2021 21:55
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-nerdery-dot-info/73fb1a93a3150725df65e953a0f6969f to your computer and use it in GitHub Desktop.
Save the-nerdery-dot-info/73fb1a93a3150725df65e953a0f6969f to your computer and use it in GitHub Desktop.
programmatically create woocommerce product in php
//Hi, i created a function for adding variable products based on your code. I share it here in case anyone needs it:
//The first parameter is the title of the product, $cats is the products category id which has to be an array and must be created previously, $variations is an array which contains the variations with prices, descriptions, discount, and $variations_key is the key of the parent attribute which has to be created previously on teh attributes page.
//I´m creating a ticket´s selling store and I´m using 2 globals: $logger and $vgh from my framework, please ignore them.
<?
function create_variable_woo_product($title, $cats = array(), $variations, $variations_key) {
global $wpdb, $logger, $vgh;
$post = array(
'post_title' => $title,
'post_status' => "publish",
'post_name' => sanitize_title($title), //name/slug
'post_type' => "product"
);
//Create product/post:
$new_prod_id = wp_insert_post($post, $wp_error);
$logger->info('Crear producto WooCommerce: ID:' . $new_prod_id);
//make product type be variable:
wp_set_object_terms($new_prod_id, 'variable', 'product_type');
//add category to product:
wp_set_object_terms($new_prod_id, $cats, 'product_cat');
//################### Add size attributes to main product: ####################
//Array for setting attributes
$var_keys = array();
$total_tickets = 0;
foreach ($variations as $variation) {
$total_tickets += (int) $variation["entradas"];
$var_keys[] = sanitize_title($variation['desc']);
wp_insert_term(
$variation['desc'], // the term
$variations_key, // the taxonomy
array(
'slug' => sanitize_title($variation['desc'])
)
);
}
wp_set_object_terms($new_prod_id, $var_keys, $variations_key );
$thedata = Array($variations_key => Array(
'name' => $variations_key,
'value' => implode( ' | ', $var_keys),
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta($new_prod_id, '_product_attributes', $thedata);
//########################## Done adding attributes to product #################
//set product values:
update_post_meta($new_prod_id, '_stock_status', ( (int) $total_tickets > 0) ? 'instock' : 'outofstock');
update_post_meta($new_prod_id, '_sku', mt_rand() );
update_post_meta($new_prod_id, '_stock', $total_tickets);
update_post_meta($new_prod_id, '_visibility', 'visible');
update_post_meta($new_prod_id, '_default_attributes', array());
//###################### Add Variation post types for sizes #############################
$i = 1;
$var_prices = array();
//set IDs for product_variation posts:
foreach ($variations as $variation) {
$my_post = array(
'post_title' => 'Variation #' . $i . ' of ' . count($variations) . ' for product#' . $new_prod_id,
'post_name' => 'product-' . $new_prod_id . '-variation-' . $i,
'post_status' => 'publish',
'post_parent' => $new_prod_id, //post is a child post of product post
'post_type' => 'product_variation', //set post type to product_variation
'guid' => home_url() . '/?product_variation=product-' . $new_prod_id . '-variation-' . $i
);
//Insert ea. post/variation into database:
$attID = wp_insert_post($my_post);
//Create 2xl variation for ea product_variation:
update_post_meta($attID, 'attribute_' . $variations_key, sanitize_title($variation['desc']));
update_post_meta($attID, '_sale_price', pyo_get_price($variation));
update_post_meta($attID, '_regular_price', (int) $variation["cantidad"]);
$var_prices[ $i - 1]['id'] = $attID;
$var_prices[$i - 1 ]['regular_price'] = sanitize_title($variation['cantidad']);
$var_prices[$i - 1 ]['sale_price'] = pyo_get_price($variation);
//add size attributes to this variation:
wp_set_object_terms($attID, $var_keys, 'pa_' . sanitize_title($variation['desc']));
$thedata = Array( $variations_key => Array(
'name' => $variations_key,
'value' => sanitize_title($variation['desc']),
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta($attID, '_product_attributes', $thedata);
update_post_meta($attID, '_sku', mt_rand());
update_post_meta($attID, '_stock_status', ( (int) $variation["entradas"] > 0) ? 'instock' : 'outofstock');
update_post_meta($attID, '_manage_stock', 'yes');
update_post_meta($attID, '_stock', $variation["entradas"]);
$i++;
}
$i = 0;
foreach ($var_prices as $var_price) {
$regular_prices[] = $var_price['regular_price'];
$sale_prices[] = $var_price['sale_price'];
}
update_post_meta($new_prod_id, '_min_variation_price', min($sale_prices));
update_post_meta($new_prod_id, '_max_variation_price', max($sale_prices));
update_post_meta($new_prod_id, '_min_variation_regular_price', min($regular_prices));
update_post_meta($new_prod_id, '_max_variation_regular_price', max($regular_prices));
update_post_meta($new_prod_id, '_min_price_variation_id', $var_prices[array_search(min($sale_prices), $sale_prices)]['id']);
update_post_meta($new_prod_id, '_max_price_variation_id', $var_prices[array_search(max($sale_prices), $sale_prices)]['id']);
update_post_meta($new_prod_id, '_min_regular_price_variation_id', $var_prices[array_search(min($regular_prices), $regular_prices)]['id']);
update_post_meta($new_prod_id, '_max_regular_price_variation_id', $var_prices[array_search(max($regular_prices), $regular_prices)]['id']);
}
@richplane
Copy link

richplane commented Jan 3, 2018

I think there's an issue with this: by using sanitize_title() to guess the slugs from the title, you may not end up with the right slug once the term objects are created. If the slug duplicates another slug once sanitised, it gets a '-2' or similar stuck on the end, which means that you'll associate the variation with the earlier term in the attribute_ metadata, rather than the one you've just created. The solution is to use get_term() to retrieve the data after running wp_insert_term().

From reading the function reference I think that wp_insert_term() will fail if you pass it a pre-existing slug in the third parameter.

One example of where you could come unstuck with this is if you had a size of "1-5m" and another of "1.5m".

@duytanqb
Copy link

Can I have json of this code ? Thanks

@virtualLast
Copy link

Any idea how we could attach product images?

@yunusga
Copy link

yunusga commented Sep 2, 2018

 // $wc_attachments['images'] array of attachments IDs
update_metadata( 'post', $product_id, '_thumbnail_id', $wc_attachments['images'][0] );
update_metadata( 'post', $product_id, '_product_image_gallery', implode(',', $wc_attachments['images']) );

@Sprite105
Copy link

Sprite105 commented Sep 10, 2018

it's example to get attach ID from image URL

function uploadImage($imageUrl) 
{
	$uploadDir  = wp_upload_dir();
	$fileName 	= basename($imageUrl);
	$uploadFile = $uploadDir['path'] . '/' . $fileName;
	$contents 	= file_get_contents($imageUrl);
	$saveFile 	= fopen( $uploadFile, 'w' );

	fwrite( $saveFile, $contents );
	fclose($saveFile);

	$wp_filetype = wp_check_filetype( $fileName, null );
	$attachment  = array(
	    'post_mime_type' => $wp_filetype['type'],
	    'post_title' => $fileName,
	    'post_content' => '',
	    'post_status' => 'inherit'
	);

	$attachID = wp_insert_attachment( $attachment, $uploadFile );
	$imageNew = get_post( $attachID );
	$fullPath = get_attached_file( $imageNew->ID );
	$metaData = wp_generate_attachment_metadata( $attachID, $fullPath );

	wp_update_attachment_metadata( $attachID, $metaData );

	return $attachID;
}

@simplenotezy
Copy link

Happy that you shared this piece of code, although it would be nice of you to explain how all variables look. You reference several variables that are not documented 😊 @the-neardy-dot-info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment