Marcus M
Marcus M

Reputation: 65

Using update_post_meta when creating a subscription product in WooCommerce

I am currently creating a plugin that collects data from their license plate. Everything works just fine. The post gets created, I receive the ID of the created post and etc. But at the very end of my function.. I'd like to update post meta doing so:

update_post_meta($product_id, '_regular_price', '0');
update_post_meta($product_id, '_subscription_period_interval', '1');
update_post_meta($product_id, '_subscription_period', 'week');

But nothing happens. I've tried running the function again placing the code above somewhere else and that I've tried multiple places inside my code. Even tried creating a whole new function for those 3 lines itself. Still no luck.

I tried returning the post meta from the post that I just created. Looking inside the returned data I see my post_meta was set, but when visiting the post from the WordPress post type archive, the post meta was reset/ignored.

Here's what I'm facing: enter image description here

As you can see, the post meta is being set but when looking inside my archive... Not set. I even tried with sleep(3) to make sure the post was created before update_post_meta. No luck.

Any ideas or tips for how I can resolve this?

I'd like to add that by taking these 3 lines and adding them to example: functions.php, and then hardcoding id's then it will work. So the short version is that when creating the post, the 3 lines do nothing even if the id is set correctly. So it has to be something to when creating the post etc...

Edit

You asked to see where the product_id is being set:

 $post_data = array(
        'post_author'   => $author,
        'post_name'     => $postname,
        'post_title'    => $data['title'],
        'post_content'  => $data['content'],
        'post_excerpt'  => $data['excerpt'],
        'post_status'   => 'draft',
        'ping_status'   => 'closed',
        'post_type'     => 'product',
    );

    // Creating the product (post data)
    $product_id = wp_insert_post( $post_data );

WordPress #WooCommerce

Upvotes: 4

Views: 1254

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253968

First it seems that you are trying to create a simple subscription product… So you forgot to set the product type which is subscription and also some other things related to product subscription price.:

$post_data = array(
    'post_author'   => $author,
    'post_name'     => $postname,
    'post_title'    => $data['title'],
    'post_content'  => $data['content'],
    'post_excerpt'  => $data['excerpt'],
    'post_status'   => 'draft',
    'ping_status'   => 'closed',
    'post_type'     => 'product',
);


// Creating the product (from post data)
$product_id = wp_insert_post( $post_data );

// Set the product type   <====   <====   <====   Missing
wp_set_object_terms( $product_id, 'subscription', 'product_type' ); //  <==  Missing

$product_price = 0;

update_post_meta($product_id, '_regular_price', $product_price);
update_post_meta($product_id, '_price', $product_price); //  <==   <==   <==  Missing
update_post_meta($product_id, '_subscription_price', $product_price); // <==  Missing 

update_post_meta($product_id, '_subscription_period', 'week');
update_post_meta($product_id, '_subscription_period_interval', '1');

It should better work now.

Upvotes: 2

Related Questions