Skip to content

Instantly share code, notes, and snippets.

@alancpazetto
Forked from Yame-/add-woocommerce-product.php
Created November 23, 2017 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alancpazetto/58895d9aef0d4e52959c6515d46a6442 to your computer and use it in GitHub Desktop.
Save alancpazetto/58895d9aef0d4e52959c6515d46a6442 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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment