Title: Php create subscriptions
Last modified: June 26, 2023

---

# Php create subscriptions

 *  Resolved [kopka](https://wordpress.org/support/users/kopka/)
 * (@kopka)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/php-create-subscriptions/)
 * Hi, I need advice, I’m creating a php code to create a subscription product, 
   I have this code:
 *     ```wp-block-code
           $product = new WC_Product_Subscription();
   
           $product->set_name('test');
           $product->set_status('publish');
           $product->set_sku('tipster_'. $user_id);
           $product->set_virtual(true);
   
           $subscription_period = 'month';
           $subscription_length = 1;
           $subscription_price = 60;
   
           $product_id = $product->save();
   
   
           update_post_meta($product_id, 'user_id_tipster', $user_id);
           update_post_meta($product_id, '_subscription_period', $subscription_period);
           update_post_meta($product_id, '_subscription_length', $subscription_length);
           update_post_meta($product_id, '_subscription_price', $subscription_price);
           update_post_meta($product_id, '_subscription_trial_length', 0);
           update_post_meta($product_id, '_subscription_trial_period', 'day');
       ```
   
 * The product is created for me, but when I go to the product page, I can’t see
   the price and I can’t add it to the cart. But as soon as I update the product,
   I see the price on the product page and can add it to the cart.
 * So I assume that I’m missing some code. Can anyone advise me, **thank you.**

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [kopka](https://wordpress.org/support/users/kopka/)
 * (@kopka)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/php-create-subscriptions/#post-16886304)
 *     ```wp-block-code
       $product = new WC_Product_Subscription();
   
       $subscription_price = 60;
   
       $product->set_name('test');
   
       $product->set_status('publish');
   
       $product->set_regular_price($subscription_price);
   
       $product->set_virtual( false );
   
       $product_id = $product->save();
   
   
       update_post_meta($product_id, '_subscription_period', 'month');
   
       update_post_meta($product_id, '_subscription_price', $subscription_price);
   
       update_post_meta($product_id, '_subscription_length', 1);
   
       update_post_meta($product_id, '_subscription_trial_length', 0);
   
       update_post_meta($product_id, '_subscription_trial_period', 'day');
   
       update_post_meta($product_id, '_subscription_sign_up_fee', '');
   
       update_post_meta($product_id, '_subscription_period_interval', 1);
   
       update_post_meta($product_id, '_subscription_limit', 'no');
   
       update_post_meta($product_id, '_subscription_one_time_shipping', 'no');
   
       update_post_meta($product_id, '_subscription_payment_sync_date', 0);
   
       $category = get_term_by( 'slug', 'subscription', 'product_type' );
   
       if ($category) {
   
       wp_set_object_terms( $product_id, $category->term_id, 'product_type', true );
   
       }
       ```
   
 * Done
    -  This reply was modified 2 years, 11 months ago by [kopka](https://wordpress.org/support/users/kopka/).
 *  [HelgaTheViking](https://wordpress.org/support/users/helgatheviking/)
 * (@helgatheviking)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/php-create-subscriptions/#post-16886489)
 * I believe Subs has some additional [custom meta fields](https://github.com/Automattic/woocommerce-subscriptions-core/blob/trunk/includes/class-wc-subscriptions-product.php#L18-L25)
   that are required. I would start with something like:
 *     ```wp-block-code
       $product = new WC_Product();
   
       $product->set_type('subscription');
   
       $user_id = get_current_user_id();
   
       $product->set_name('test');
   
       $product->set_status('publish');
   
       $product->set_sku('tipster_'. $user_id);
   
       $product->set_virtual(true);
   
       $subscription_period = 'month';
   
       $subscription_length = 1;
   
       $subscription_price = 60;
   
       $product->set_meta( '_subscription_price', $subscription_price );
   
       $product->set_meta( '_subscription_period', $subscription_period );
   
       $product->set_meta( '_subscription_length', $subscription_length );
   
       $product_id = $product->save();
       ```
   
 *  Thread Starter [kopka](https://wordpress.org/support/users/kopka/)
 * (@kopka)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/php-create-subscriptions/#post-16886589)
 * F**atal error**: Uncaught Error: Call to undefined method WC_Product::set_type()
 * set type does not exist 🙂 but I already solved it, but thanks for the help.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Php create subscriptions’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/xa-woocommerce-subscriptions_07c0d4.
   svg)
 * [Subscriptions for WooCommerce](https://wordpress.org/plugins/xa-woocommerce-subscriptions/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/xa-woocommerce-subscriptions/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/xa-woocommerce-subscriptions/)
 * [Active Topics](https://wordpress.org/support/plugin/xa-woocommerce-subscriptions/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/xa-woocommerce-subscriptions/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/xa-woocommerce-subscriptions/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [kopka](https://wordpress.org/support/users/kopka/)
 * Last activity: [2 years, 11 months ago](https://wordpress.org/support/topic/php-create-subscriptions/#post-16886589)
 * Status: resolved