Hello and happy new year to you all ! I'm trying t...
# support
m
Hello and happy new year to you all ! I'm trying to make the subscription gem work on my shop but I'm stuck at this part
include the following parameters when posting to /orders/populate
I've installed the gem, ran the migration, I can now make a product susbcribable from my admin panel by ticking a box. But nothing happens on the shop side. So I'm obviously missing a step. I don't know where/how to add the following params:
Copy code
{
  // other add to cart params
  subscription_line_item: {
    quantity: 2,             // number of units in each subscription order
    subscribable_id: 1234,   // which variant the subscription is for
    interval_length: 1,      // time between subscription activations
    interval_units: "month", // plural qualifier for length (day/week/month/year)
    end_date: '2011/12/13'   // stop processing after this date (use null to process ad nauseam)
  }
}
Could anyone help me please ?
✅ 1
n
Happy New year 🙂 can you share a bit more about how you are trying to subscribe to it?
m
For now, I'm not doing anything really to subscribe to it 😅 I was actually looking where I can set up the subscriptions intervals, the end date of the subscription and so on
k
Hello Marie! I think you have to customize your add to cart form to include the fields that generate those params.
I don’t know if there’s any example in the extension (it would be nice to have examples on the readme actually)
By the way, looking at the extension, it seems like it’s already injecting the right code in the application. Are you using the starter frontend or the legacy solidus_frontend?
m
Alright, makes sense, thank you. I'm using the starter frontend. I will look in that direction and come back to you if I'm still lost !
k
Ok, that extension works out of the box with the old frontend. In the new frontend, you need to add that part manually
m
Ok so I've added the code to my add to cart on the product show. I can add info in the right fields but when I click on the submit button, it is only adding the product in the cart with the default values of quantity: 1. And it's not creating a new subscription item. I don't understand how to link my
button_tag
to my
fields_for :'subscription_line_item'
Any idea what I'm missing here ?
k
are you adding these fields into the add to cart form?
m
Yes, but actually i'm in
product_submit.html.erb
which is rendered in
cart_line_items/_form.html.erb
k
ok and in your rails server logs, can you see the request coming? if yes, which parameter does it have?
m
In my logs, I can see a POST request with the right params (except for the button params seems like) `"variant_id"=>"12", "subscription_line_item"=>{"quantity"=>"4", "interval_length"=>"2", "interval_units"=>"month", "subscribable_id"=>""}, "button"=>""}`but then it only creates an order with `[["variant_id", 12], ["order_id", 10], ["quantity", 1], ["price", "14.0"]`and I don't know where the params for the subscription line item get lost
k
I think I’ve got what’s happening. The decorator that changes the behavior of the add to cart action at the controller level (which was called populate on the old frontend) is not working anymore because that action is not there anymore in the new frontend Here’s what the decorator does: https://github.com/solidusio/solidus_subscriptions/blob/master/lib/decorators/fron[…]tions/spree/orders_controller/create_subscription_line_items.rb Now, we should try to do the same but for the action of the new frontend, which is here: https://github.com/solidusio/solidus_starter_frontend/blob/v3.2/templates/app/controllers/cart_line_items_controller.rb
So, if you add this code in the
cart_line_items_controller
of your application, it should work:
Copy code
after_action(
  :handle_subscription_line_items,
  only: :create,
  if: ->{ params[:subscription_line_item] }
)


# and in the private section:

def handle_subscription_line_items
  # you can put a binding.pry here to debug and see if this callback gets called
  line_item = @current_order.line_items.find_by(variant_id: params[:variant_id])
  create_subscription_line_item(line_item)
end
m
Alright, I think this makes sense! But I'm now getting this error `undefined method
create_subscription_line_item'
Could this be because, somehow, the cart_line_items doesn't have
.prepend(SolidusSubscriptions::Spree::OrdersController::CreateSubscriptionLineItems)
like in the decorator you shared ? I've tried adding the same line of code but it didn't help
k
Hey Marie, I will take a look tomorrow as soon as I’m at my laptop, but seems a promising idea
m
Alright, thank you so much !
k
Hey @Marie Lamoureux, I tried locally and the missing part is
Copy code
include SolidusSubscriptions::SubscriptionLineItemBuilder
in the cart_line_items_controller.
BTW, thanks to this thread, I realized what is needed to have this extension installed on a new installation and I made a PR that contains an automated script that does everything: https://github.com/solidusio/solidus_subscriptions/pull/276
🙌 2
m
Thank you @kennyadsl! I really appreciate you help. I think it's okay now. Did you already get this error message afterwards
Validation failed: Subscribable must exist, Subscribable can't be blank
?
k
Yes it’s because of a JS error
If you use the partial included in the PR it should be fixed
m
I'm so sorry but it's still not working. Would that help if I share my repo and you have a look? I don't abuse of your help
k
Yes of course I can take a look
Feel free to add me
m
So I sorted this particular issue 🙌 Somehow I must have messed with my spree_products and variants, and the products had a subscribable column when they shouldn’t. Now, subscription line items are created correctly, I can see them in my console, but they are not displayed in the admin panel. I think that after a line item is created, no subscription is actually created. Am I supposed to do anything in particular here ?
k
So your subscriptions tab is empty? The actual subscription is created this job, so make sure that your background job processor (eg. sidekiq?) is running
if you don’t have sidekiq, it should just work, I tried without it and it worked. BTW, please take a look at the rails console after completing the order, maybe there’s an error in that particular job that you can see from the logs.
m
@Naoki Mi just helped me to solve my error. It was an issue with the payment method, and now everything is working. Thank you both for taking the time to help me 🌟
🎉 1