Search code examples
phppython-3.xwordpresswoocommercewoocommerce-subscriptions

How to Create WooCommerce Subscription Product via. REST API?


I am able to find the REST API docs for WooCommerce: https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product but am unable to figure out how to create a subscription product via. this API.

I am able to also edit a user's subscription once it has been created via the REST API: https://prospress.github.io/subscriptions-rest-api-docs/ however I don't see any documentation for how to create subscription products.

I am able to create a simple subscription product with the following code:

data = {
            "name": subscription_product_name,
            "type": "subscription",
            "regular_price": subscription_product_price,
        }
wcapi.post("products", data)

However, the price is set incorrectly, and I am unable to figure out the fields that would be used to set the signup_fee, billing period, and other various fields.

Any help would be greatly appreciated. Thanks!


Solution

  • The API endpoint for creating a WooCommerce product is http://www.example.com//wp-json/wc/v3/products The subscription length, interval... etc are passed as metadata.

    Passing the below JSON can create a simple subscription product

    {
        "name": "simple sub api",
        "type": "subscription",
        "status": "publish",
        "featured": false,
        "catalog_visibility": "visible",
        "description": "sub desc",
        "short_description": "sub short",
        "sku": "",
        "price": "10",
        "regular_price": "10",
        "sale_price": "",
        "date_on_sale_from": null,
        "date_on_sale_from_gmt": null,
        "date_on_sale_to": null,
        "date_on_sale_to_gmt": null,
        "on_sale": false,
        "purchasable": true,
        "total_sales": 3,
        "virtual": false,
        "downloadable": false,
        "downloads": [],
        "download_limit": -1,
        "download_expiry": -1,
        "external_url": "",
        "button_text": "",
        "tax_status": "taxable",
        "tax_class": "",
        "manage_stock": false,
        "stock_quantity": null,
        "backorders": "no",
        "backorders_allowed": false,
        "backordered": false,
        "low_stock_amount": null,
        "sold_individually": false,
    
    
        "meta_data": [
            {
                "key": "_subscription_payment_sync_date",
                "value": "0"
            },
            {
                "key": "_subscription_price",
                "value": "10"
            },
            {
                "key": "_subscription_trial_length",
                "value": "1"
            },
            {
                "key": "_subscription_sign_up_fee",
                "value": ""
            },
            {
                "key": "_subscription_period",
                "value": "month"
            },
            {
                "key": "_subscription_period_interval",
                "value": "1"
            },
            {
                "key": "_subscription_length",
                "value": "0"
            },
            {
                "key": "_subscription_trial_period",
                "value": "month"
            },
            {
    
                "key": "_subscription_limit",
                "value": "no"
            },
            {
                "key": "_subscription_one_time_shipping",
                "value": "no"
            }
        ],
        "stock_status": "instock"
    }