Answers.Body: <p>Ok, I think your thinking on this stuff is a little wrong at the moment.  </p>
  
  <p>The way Commerce works is that when you add to a cart, it creates a <code>lineItem</code>.  That data model holds basic stuff like <code>qty</code> and a <code>description</code> 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 <code>purchasable</code> 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).</p>
  
  <p>From this purchasable, you can get back to the full product and/or variant model.</p>
  
  <p>So, you don't actually need <em>any</em> 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:</p>
  
  <p><code>lineItem.purchasable.product.url</code> - for the product url
  <code>lineItem.purchasable.url</code> - for a specific variant url</p>
  
  <p>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:</p>
  
  <p><code>lineItem.purchasable.product.fieldHandle</code> - for product fields
  <code>lineItem.purchasable.fieldHandle</code> - for variant fields.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-10T09:24:57.463Z
Answers.Text: 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.
Answers.UserDisplayName: Stephen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-10T22:16:17.503Z
Answers.Text: 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! :)
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-10T00:26:23.027Z
Answers.LastActivityDate: 2017-02-10T00:26:23.027Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>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 }}</p>
  
  <p>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.</p>
  
  <p>However, I'd rather use a custom field or a dynamic field to do this instead. Is this possible? If so, how?</p>
  
  <p>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: </p>
  
  <blockquote>
    <p>Craft\Commerce_LineItemModel and its behaviors do not have a method or
    closure named "testPageUrl".</p>
  </blockquote>
  
  <p>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: </p>
  
  <blockquote>
    <p>Craft\Commerce_LineItemModel and its behaviors do not have a method or
    closure named "productPageUrl".</p>
  </blockquote>
  
  <p>Thanks for any help in advance.</p>
  
Tags: <plugin-craftcommerce><field>
Title: How to add custom fields for products in Craft Commerce and output them in the cart
ThreadText: # How to add custom fields for products in Craft Commerce and output them in the cart
  
  (Asked by: username_0 on 2017-02-09)
  
  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.
  
  **Tags:** plugin-craftcommerce, field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-02-10. Score: 4
  
  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.
  
  > **Comments:**
  > **username_0**: Hi username_1, 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.  
  > **username_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! :)  
  
  ---
  
  