• Resolved kopka

    (@kopka)


    Hi, I need advice, I’m creating a php code to create a subscription product, I have this 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

    (@kopka)

    $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 1 year, 7 months ago by kopka.

    I believe Subs has some additional custom meta fields that are required. I would start with something like:

    $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

    (@kopka)

    Fatal 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.