Last active
December 6, 2022 17:14
-
-
Save Yame-/265544d033df95e67881 to your computer and use it in GitHub Desktop.
Adding a WooCommerce product programmatically.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
@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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/