Answers.Body: <p>Just quickly as I'm on my phone...The two main ways to debug this sort of thing in Commerce are:</p>
  
  <ol>
  <li><p>Check what you are actually POSTing matches what you think you are posting (to check this I just use the browser dev tools)</p></li>
  <li><p>Check the flash data (notice and error) for messages when the page reloads (or check the JSON response if ajaxing). </p></li>
  </ol>
  
  <p>Usually you'll get a sensible message back, e.g. out of stock or similar.  </p>
  
  <p>I use this code to dump the flash data (I think only error and notice are used these days, the <code>commerce</code> flash dates back from before commerce was released but it can't hurt!):</p>
  
  <pre><code>{% if craft.config.devMode and (craft.session.hasFlash('commerce') or craft.session.hasFlash('notice') or craft.session.hasFlash('error')) %}
  
          {% set isErrors = true %}
          {% set commerceFlash = craft.session.getFlash('commerce')|join('|') %}
          {% set noticeFlash = craft.session.getFlash('notice')|join('|') %}
          {% set errorFlash = craft.session.getFlash('error')|join('|') %}
  
          Commerce: [{{  commerceFlash }}]
          Notice: [{{  noticeFlash }}]
          Error: [{{  errorFlash }}]
  
      {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-14T10:47:48.017Z
Answers.Text: Sorry if I'm a little obtuse about this one. I've never done this before. Can you point me to an article or something similar to get me on my way to do this. Are you referring to something like this: (http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/) I also turned on DevMode which didn't turn up anything.
Answers.UserDisplayName: Wonko
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-14T12:16:42.060Z
Answers.Text: See updated answer above with code for dumping the flash data (I put this in my main twig layout template).
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-14T13:32:20.750Z
Answers.Text: That's great! A clue here. The error: `'Could not add to cart: Not enabled for sale.'`. But the product is enabled and there is no expiry date set. Stock is also unlimited.
Answers.UserDisplayName: Wonko
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-14T14:25:46.503Z
Answers.Text: Hmm, beginning to sound like a bug.  Make sure you're checking the *variant* and the product though.
  
  The actual line in Commerce that's coming from seems to be:
      if ($lineItem->purchasable->getStatus() != BaseElementModel::ENABLED)
    {
     $lineItem->addError('purchasableId',Craft::t('Not enabled for sale.'));
    }
  
  
  At this point I'd probably send your database in to P & T with a support ticket from your dashboard.
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-14T14:50:01.507Z
Answers.Text: Thanks for this. I still have the original product template that comes with CraftCommerce (commerce/products). Loading that page and adding the item from there doesn't do anything either. "Main" products do get added from that page, though. So there's something different between the two? The accessories didn't have a template so I added one. That didn't help. A ticket it is, I guess.
Answers.UserDisplayName: Wonko
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-14T17:43:45.100Z
Answers.Text: Fooled around a bit more with this. The SKU for the accessories was acc_01, acc_02, acc_03, etc. Getting rid of the underscore (e.g. acc01, acc02, acc03 etc.) has fixed the problem. No idea why because the "main" product has an sku of bk_001, bk_002, bk_003 etc. and those were being added to the cart without a problem.
Answers.UserDisplayName: Wonko
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-14T21:20:11.113Z
Answers.Text: Hmm that makes no sense. We use skus with underscores all the time (for related products too). If you add them back is it reproducible?  I suspect inadvertent other changes made the difference somehow.
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-15T19:11:10.267Z
Answers.Text: Yes, definitely didn't make sense. It seems that even though the products were listed as "enabled" in the dashboard the weren't in the DB. Saving the products again fixed the issue. Underscores are irrelevant in this case. A re-save was what was needed to correct the DB. Thanks for looking into this.
Answers.UserDisplayName: Wonko
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-13T12:41:16.253Z
Answers.LastActivityDate: 2016-03-14T12:15:52.670Z
Answers.LastEditDate: 2016-03-14T12:15:52.670Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>Folks:</p>
  
  <p>I have a list of accessories related to a main product. After adding the main product to the cart, the user can then add accessories related to that product.</p>
  
  <p>The code seems to check out. The price and purchasableId for the related product are coming through just fine. However, when you click the "Add to Cart" button for the related product, the page refreshes but nothing is added to the cart. Relevant code:</p>
  
  <pre><code>{% set cart = craft.commerce.cart %}
  {% for relatedProduct in product.relatedAccessories %}
  &lt;img src="{{ relatedProduct.accessoryImage.first().url(accessoryThumb) }}" width="{{ asset.getWidth(accessoryThumb) }}" height="{{ asset.getHeight(accessoryThumb) }}" class="img-responsive" /&gt; 
     &lt;p&gt;{{ relatedProduct.title }} - {{ relatedProduct.accessoryDescription }} - {% for variant in relatedProduct.variants %}{{ variant.salePrice|currency('CAD', true) }} - {{ variant.purchasableId }}{% endfor %}&lt;/p&gt; 
   &lt;form method="POST"&gt;
      &lt;input type="hidden" name="action" value="commerce/cart/updateCart"&gt;
      &lt;input type="hidden" name="redirect" value="commerce/cart"&gt;
      &lt;input type="hidden" name="qty" value="1"&gt;
      {% for variant in relatedProduct.variants %}&lt;input type="hidden" name="purchasableId" value="{{ variant.purchasableId }}"&gt;{% endfor %}
      &lt;input class="btn  btn-d  btn-lg" type="submit" value="Add to cart"&gt;
  &lt;/form&gt;
  {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>The source code for the rendered page looks like this:</p>
  
  <pre><code>&lt;img src="http://example.com/images_product/_125x100_crop_top-center_75/photo.jpg" width="125" height="100" class="img-responsive" /&gt; 
     &lt;p&gt;Accessory description - $50&lt;/p&gt; 
  &lt;form method="POST"&gt;
      &lt;input type="hidden" name="action" value="commerce/cart/updateCart"&gt;
      &lt;input type="hidden" name="redirect" value="commerce/cart"&gt;
      &lt;input type="hidden" name="qty" value="1"&gt;
      &lt;input type="hidden" name="purchasableId" value="65"&gt;
      &lt;input class="btn  btn-d  btn-lg" type="submit" value="Add to cart"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>As far as I can see, this should be working. But again, nothing is added to the cart.</p>
  
  <p>Does anyone have any ideas?</p>
  
Tags: <plugin-craftcommerce>
Title: Adding items to cart from related entry not working
ThreadText: # Adding items to cart from related entry not working
  
  (Asked by: username_0 on 2016-03-13)
  
  Folks:
  
  I have a list of accessories related to a main product. After adding the main product to the cart, the user can then add accessories related to that product.
  
  The code seems to check out. The price and purchasableId for the related product are coming through just fine. However, when you click the "Add to Cart" button for the related product, the page refreshes but nothing is added to the cart. Relevant code:
  
  ```
  {% set cart = craft.commerce.cart %}
  {% for relatedProduct in product.relatedAccessories %}
  <img src="{{ relatedProduct.accessoryImage.first().url(accessoryThumb) }}" width="{{ asset.getWidth(accessoryThumb) }}" height="{{ asset.getHeight(accessoryThumb) }}" class="img-responsive" /> 
     <p>{{ relatedProduct.title }} - {{ relatedProduct.accessoryDescription }} - {% for variant in relatedProduct.variants %}{{ variant.salePrice|currency('CAD', true) }} - {{ variant.purchasableId }}{% endfor %}</p> 
   <form method="POST">
      <input type="hidden" name="action" value="commerce/cart/updateCart">
      <input type="hidden" name="redirect" value="commerce/cart">
      <input type="hidden" name="qty" value="1">
      {% for variant in relatedProduct.variants %}<input type="hidden" name="purchasableId" value="{{ variant.purchasableId }}">{% endfor %}
      <input class="btn  btn-d  btn-lg" type="submit" value="Add to cart">
  </form>
  {% endfor %}
  {% endfor %}
  
  ```
  
  The source code for the rendered page looks like this:
  
  ```
  <img src="http://example.com/images_product/_125x100_crop_top-center_75/photo.jpg" width="125" height="100" class="img-responsive" /> 
     <p>Accessory description - $50</p> 
  <form method="POST">
      <input type="hidden" name="action" value="commerce/cart/updateCart">
      <input type="hidden" name="redirect" value="commerce/cart">
      <input type="hidden" name="qty" value="1">
      <input type="hidden" name="purchasableId" value="65">
      <input class="btn  btn-d  btn-lg" type="submit" value="Add to cart">
  </form>
  
  ```
  
  As far as I can see, this should be working. But again, nothing is added to the cart.
  
  Does anyone have any ideas?
  
  **Tags:** plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-03-13. Score: 2 | Accepted answer
  
  Just quickly as I'm on my phone...The two main ways to debug this sort of thing in Commerce are:
  
  1. Check what you are actually POSTing matches what you think you are posting (to check this I just use the browser dev tools)
  2. Check the flash data (notice and error) for messages when the page reloads (or check the JSON response if ajaxing).
  
  Usually you'll get a sensible message back, e.g. out of stock or similar.
  
  I use this code to dump the flash data (I think only error and notice are used these days, the `commerce` flash dates back from before commerce was released but it can't hurt!):
  
  ```
  {% if craft.config.devMode and (craft.session.hasFlash('commerce') or craft.session.hasFlash('notice') or craft.session.hasFlash('error')) %}
  
          {% set isErrors = true %}
          {% set commerceFlash = craft.session.getFlash('commerce')|join('|') %}
          {% set noticeFlash = craft.session.getFlash('notice')|join('|') %}
          {% set errorFlash = craft.session.getFlash('error')|join('|') %}
  
          Commerce: [{{  commerceFlash }}]
          Notice: [{{  noticeFlash }}]
          Error: [{{  errorFlash }}]
  
      {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: Sorry if I'm a little obtuse about this one. I've never done this before. Can you point me to an article or something similar to get me on my way to do this. Are you referring to something like this: (http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/) I also turned on DevMode which didn't turn up anything.  
  > **username_1**: See updated answer above with code for dumping the flash data (I put this in my main twig layout template).  
  > **username_0**: That's great! A clue here. The error: `'Could not add to cart: Not enabled for sale.'`. But the product is enabled and there is no expiry date set. Stock is also unlimited.  
  > **username_1**: Hmm, beginning to sound like a bug.  Make sure you're checking the *variant* and the product though.  The actual line in Commerce that's coming from seems to be:     if ($lineItem->purchasable->getStatus() != BaseElementModel::ENABLED)   {    $lineItem->addError('purchasableId',Craft::t('Not enabled for sale.'));   }   At this point I'd probably send your database in to P & T with a support ticket from your dashboard.  
  > **username_0**: Thanks for this. I still have the original product template that comes with CraftCommerce (commerce/products). Loading that page and adding the item from there doesn't do anything either. "Main" products do get added from that page, though. So there's something different between the two? The accessories didn't have a template so I added one. That didn't help. A ticket it is, I guess.  
  > **username_0**: Fooled around a bit more with this. The SKU for the accessories was acc_01, acc_02, acc_03, etc. Getting rid of the underscore (e.g. acc01, acc02, acc03 etc.) has fixed the problem. No idea why because the "main" product has an sku of bk_001, bk_002, bk_003 etc. and those were being added to the cart without a problem.  
  > **username_1**: Hmm that makes no sense. We use skus with underscores all the time (for related products too). If you add them back is it reproducible?  I suspect inadvertent other changes made the difference somehow.  
  > **username_0**: Yes, definitely didn't make sense. It seems that even though the products were listed as "enabled" in the dashboard the weren't in the DB. Saving the products again fixed the issue. Underscores are irrelevant in this case. A re-save was what was needed to correct the DB. Thanks for looking into this.  
  
  ---
  
  