Skip to content

Instantly share code, notes, and snippets.

@Yame-
Last active December 6, 2022 17:14
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save Yame-/265544d033df95e67881 to your computer and use it in GitHub Desktop.
Save Yame-/265544d033df95e67881 to your computer and use it in GitHub Desktop.
Adding a WooCommerce product programmatically.
<?php
$args = array(
'post_author' => 1,
'post_content' => '',
'post_status' => "draft", // (Draft | Pending | Publish)
'post_title' => '',
'post_parent' => '',
'post_type' => "product"
);
// Create a simple WooCommerce product
$post_id = wp_insert_post( $args );
// Setting the product type
wp_set_object_terms( $post_id, 'simple', 'product_type' );
// Setting the product price
update_post_meta( $post_id, '_price', 0 );
update_post_meta( $post_id, '_regular_price', 0 );
@iamumairayub
Copy link

I have a fully working PHP script to insert/update VARIABLE products from JSON into Woocommerce 3.x. If some want to buy it please contact me mani619cash AT gmail DOT com

@peterrobertz
Copy link

:D

@Arrayly
Copy link

Arrayly commented Apr 14, 2020

Thanks for this!

@abdallahoraby
Copy link

Thanks you saved my day 👍

@andrewreal
Copy link

Be careful with this as WooCommerce have added a new table wc_product_meta_lookup which is used for looking up meta data. The code above does not update the new table, so functions like wc_get_product_id_by_sku() will not find products added this way.

A newer solution is to use the CRUD system introduced in WC3 as below.

$post_id = wp_insert_post( array(
    'post_title' => $title,
    'post_type' => 'product',
    'post_status' => 'publish',
    'post_content' => $body,
));
$product = wc_get_product( $post_id );
$product->set_sku( $sku );
$product->set_price( $price );
$product->set_regular_price( $regular_price );
$product->save();

@gbrits
Copy link

gbrits commented May 10, 2021

@andrewreal that's awesome, looks similar to Laravel's eloquent handling of models, how would you check for an SKU already existing prior to creation / update if an SKU already exists?

@gbrits
Copy link

gbrits commented May 10, 2021

@andrewreal sorry, last one - can you create product categories along with this process?

@andrewreal
Copy link

@andrewreal that's awesome, looks similar to Laravel's eloquent handling of models, how would you check for an SKU already existing prior to creation / update if an SKU already exists?

You should be able to use the WooCommerce function wc_get_product_id_by_sku( $sku ) - https://woocommerce.github.io/code-reference/namespaces/default.html#function_wc_get_product_id_by_sku - to check for a product ID.

@andrewreal
Copy link

@andrewreal sorry, last one - can you create product categories along with this process?

I'm not sure there is a WooCommerce function for that, unfortunately the docs at https://woocommerce.github.io/code-reference/ aren't great for discovery but you try trawling through.

You should be able to add a category with thee built in WordPress functions as shown here though https://wordpress.org/support/topic/create-woocommerce-category-in-php/

@hamzaijaz-dev
Copy link

@andrewreal Getting => Trying to get property 'feeds' of non-object in path/to/wp-includes/post.php.
In fact, this is b/c of

global $wpdb, $wp_rewrite;
	$original_slug = $slug;

	$feeds = $wp_rewrite->feeds;
	if ( ! is_array( $feeds ) ) {
		$feeds = array();
	}

Feeds is using in wp_insert_post. can you please check this out? I am getting this same error in every case.

@hamzaijaz-dev
Copy link

image
See attachment

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