• Resolved matthias_123

    (@matthias_123)


    Hello,

    I am trying to create a WC_Product_Variable_Subscription and the connected
    WC_Product_Subscription_Variation(s) via PHP

    Creating WC_Product_Variable and WC_Product_Variation is simple (the class from which the classes I create inherit) but I am having troubles setting the required

    interval,
    period and
    length

    properties (e.g make the subscription price 200$ every month for 6 month.

    Reviewing generated WC_Product_Subscription_Variation did not help.
    It’s no property (via set_props) and it seems to be stored in a differend table
    as e.g.

    $products = wc_get_products(['limit' => -1]);
    echo '<pre>' . print_r(
      wc_get_product($products[0]->get_children()[0])
    , true) . '</pre>';

    to display and review a (via the wordpress backend generated) variations did not (as mentioned) get me any closer

    So my question:
    $product = new WC_Product_Variable_Subscription();
    $variation = new WC_Product_Subscription_Variation();

    howto / whereto set the required properties via PHP to define the initially named properties ?

    thanks a lot (!) for anyone being able to point me into the right direction

    btw: had a quick look at https://github.com/wp-premium/woocommerce-subscriptions/blob/master/includes/class-wc-product-subscription-variation.php – the main difference seesm to be an instance of WCS_Array_Property_Post_Meta_Black_Magic

    and…
    does anyone know where the woocommerce subscription unit tests are ?

    • This topic was modified 4 years, 11 months ago by matthias_123.
    • This topic was modified 4 years, 11 months ago by matthias_123.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    btw: had a quick look at https://github.com/wp-premium/woocommerce-subscriptions/blob/master/includes/class-wc-product-subscription-variation.php – the main difference seesm to be an instance of WCS_Array_Property_Post_Meta_Black_Magic

    I can’t say anything about that since this is a third party repo, but I suggest you look at the original code instead.

    WooCommerce Subscriptions also has an extensive developer documentation: https://docs.woocommerce.com/documentation/plugins/woocommerce/woocommerce-extensions/woocommerce-subscriptions/developer-docs/

    I also found this: https://gist.github.com/vishalbasnet23/181f640053d5400530a4411a93cf87f7

    And this: https://wordpress.stackexchange.com/questions/202873/how-to-use-wc-create-order-with-subscription-product

    Happy coding!

    Kind regards,

    Thread Starter matthias_123

    (@matthias_123)

    Hello,

    Thanks a lot for your feedback – there seems to be a slight missunderstanding
    The links you provided are about orders and subscriptions whereas I was talking about a product type
    the name of the product type is a bit confusing –
    WC_Product_Variable_Subscription
    which itself inherits from
    WC_Product_Variable
    and so on…
    ( the doc comment in the top of the class file states as well:
    Variable Subscription Product Class…)

    So I was talking about creating the required product type which in turn will pass the required post meta values to a subscription.

    But no damage done – the links you provided will sure come in handy to build up my knowledge of the internal of the implementations.

    Thanks again and all the best !

    Concerning the actual answer:
    if the WC_Product_Variable_Subscription has been created and the required variations have been created / connected to it
    (as a tip: if you search for a way to do so you can have a look at the createAllProductVariations method of the data_store
    e.g.

    $data_store = $product->get_data_store();    
    $data_store->create_all_product_variations($product, $maxVariations);
    $data_store->sort_all_product_variations($product->get_id());

    the product variations can be updated via the post meta data

    e.g.

    update_post_meta($variation->get_id(), '_subscription_period', 'week');
    update_post_meta($variation->get_id(), '_subscription_period_interval', 1);
    update_post_meta($variation->get_id(), '_subscription_length', 4);
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Creating a product variable subscription and its variations with PHP’ is closed to new replies.