I am trying to programmatically (using PHP) insert a Woocommerce product into my website. All my products are external / affiliate product types. I have successfully created the product with the code below, but the default product type is Simple Product. My question is how to change the product type to external / affiliate using my PHP posts or metadata?
I tried to execute this command without success:
update_post_meta($post_id, 'product-type' , 'external' );
Here is the code used to create the product with the bare-bones attributes assigned:
require_once("../wp-load.php");
$new_post = array(
'post_title' => "Title of My Product",
'post_content' => 'Full description of My Product',
'post_status' => 'publish',
'post_type' => 'product',
'is_visible' => '1'
);
$post_id = wp_insert_post($new_post);
update_post_meta($post_id, '_sku', '5000' );
update_post_meta($post_id, '_regular_price' , '99.95');
update_post_meta($post_id, '_product_url' , 'http://www.myaffiliatesURL.com');
update_post_meta($post_id, '_button_text' , 'Buy from My Affiliate' );
update_post_meta($post_id, '_aioseop_description' , 'Short description of My Product' );
update_post_meta($post_id, '_visibility' , 'visible' );
source
share