Woocommerce Add product with external / affiliate type

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' );
+4
source share
2 answers

Perhaps try this.

wp_set_object_terms ($post_id, 'external', 'product_type');

You also need to set 'parent_id'

+4

! , . , /:

wp_set_object_terms( $post_id, 8, 'product_type', false );

8 / .

0

Source: https://habr.com/ru/post/1525947/


All Articles