• Resolved yanickc

    (@yanickco)


    Hi there,

    How I can add a subscription product to the Cart using WC_Cart::add_to_cart() php function?

    I’m trying to add a subscription product to my cart via ajax but can’t find any information on how to achieve this.

    Can anyone help?

    Thanks!
    Yan

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello @yanickco, You can do the add to cart as the normal product and the will be manged by the woocommerce subscription plugin you do not take any tentions about that.
    WC()->cart->add_to_cart( $product_id );

    Thread Starter yanickc

    (@yanickco)

    Thanks for the reply @mohitmishra

    I actually found the solution.

    Here it is in case someone else might need it:

    You can create an add to cart link for a specific subscription plan of a product by adding the convert_to_sub_ + product_id parameters on your product link.

    For example, for a product with the ID 256 that has a subscription plan for 1_month, the URL would be: https://domain.com/product_name?add-to-cart=256&convert_to_sub_256=1_month. You can replace the id and the subscription plan in this link to create similar links for any other products.

    Note that you can also find the 2nd parameter using your browser’s inspector in the radio buttons of the subscription plans.

    Alternatively, if you’d like to use PHP code to achieve what you’d like, then you can use the WCS_ATT_Cart::apply_subscription_schemes function.

    Hopefully this can be helpful to anyone with the same question.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add a subscription product to the Cart using WC_Cart::add_to_cart() ph’ is closed to new replies.