user3287416
user3287416

Reputation: 41

Woocommerce Add Product with External/Affiliate Type

I am trying to programmatically (using PHP) insert a Woocommerce product into my site. All of my products are External/Affiliate product type. I have successfully created a product with the code below, but it is defaulting to a "Simple Product" product type. My question is how do I change the product type to External/Affiliate using my PHP post creation or metadata updates?

I've attempted this command with no success:

update_post_meta($post_id, 'product-type' , 'external' );

Here is the code used to create the product with 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' );

Upvotes: 4

Views: 2922

Answers (2)

user3287416
user3287416

Reputation: 41

I figured it out! thanks for commenting to help. Here is the code to set it to external/affiliate:

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

The key here is to use 8 for for external/affiliate

Upvotes: 0

user1032317
user1032317

Reputation:

May be try this instead.

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

You also need to set the 'parent_id'

Upvotes: 4

Related Questions