1

You can add a note field for a product on the 'Add to Basket' form in Craft Commerce, and output this on the basket page using {{ item.note }}

So I'm using the note field field to record the page URL for the product, so I can link back to the product page for each line item in the basket.

However, I'd rather use a custom field or a dynamic field to do this instead. Is this possible? If so, how?

I've tried creating a dynamic field named 'test-page-url', but that didn't work when trying to output {{ item.testPageUrl }} on the basket page. I got this error:

Craft\Commerce_LineItemModel and its behaviors do not have a method or closure named "testPageUrl".

I also tried creating a new product field (productPageUrl) and using this name, but that didn't work either. I got this error when trying to use {{ item.productPageUrl }} on the basket page:

Craft\Commerce_LineItemModel and its behaviors do not have a method or closure named "productPageUrl".

Thanks for any help in advance.

3

Ok, I think your thinking on this stuff is a little wrong at the moment.

The way Commerce works is that when you add to a cart, it creates a lineItem. That data model holds basic stuff like qty and a description and so on, enough to just manage and print out the basics of the cart. However, it also (for each lineItem) maintains a reference to the purchasable itself - with this purchasable being a purchasable variant (either a products defaultVariant if it is a non-variant product, or the actual variant chosen if it is a variant product).

From this purchasable, you can get back to the full product and/or variant model.

So, you don't actually need any custom fields to get the product's url. You can print it simply by working back from the lineitem to the product or variant as you need, using:

lineItem.purchasable.product.url - for the product url lineItem.purchasable.url - for a specific variant url

In more general terms, in Commerce settings, you can add as many fields to products as you like, and indeed you can also add custom fields to variants. It's hugely powerful. And from then on you can get to those fields using the same approach:

lineItem.purchasable.product.fieldHandle - for product fields lineItem.purchasable.fieldHandle - for variant fields.

The Commerce example templates are very handy for figuring this stuff out, and it's well worth spending a couple hours with the default Commerce install and those templates to see how it all hangs together.

| improve this answer | |
  • Hi Jeremy, thanks for this. I'm still getting my head around how all this works after coming from being comfortable with EE and CartThrob. I tried using {{ item.purchasable.product.url }} in the basket but that outputted nothing, and {{ item.purchasable.url }} outputted "?variant=119". The reason {{ item.purchasable.product.url }} is outputting nothing (I think) is because my products themselves don't have a URL. Instead, they are being pulled into a separate entry via the Commerce Products relationship field, which is why I was coming back to this 'hack' with using the Notes field instead. – Stephen Feb 10 '17 at 9:24
  • 1
    Well, yes, if your products don't have urls...then you won't be able to get urls for them! How to refer to products via a Commerce products field is really another question but the answer is {% for product in entry.productsFieldHandle %} {{ product.url }} {% endfor %} in a nutshell. At no point should you need to hack any other field to hold a url, assuming you do want urls for your products. Basically, in Craft, if you feel yourself labelling anything a 'hack' - it will be wrong. It's the complete opposite of Cartthrob in that respect! :) – Jeremy Daalder Feb 10 '17 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.