<p>Well, if you submit to your add to cart form via ajax, you will already get a json response - with an <code>error</code> - you can simply grab that error, test for <code>Not a purchasable ID</code> and then change the text to whatever you want.</p>

<p>If you're submitting in a non ajax fashion, you won't get a JSON response no matter what clearly.</p>

<p>I agree with the comment above - I'd generally handle this in front end JS, by disabling the add to cart until a size is selected.  And I guess if you're allowing your site to use no-js, then in that case I would probably just pre-select the most popular option (Medium, presumably).</p>

<p>You can also write a plugin that listens to <a href="https://craftcommerce.com/docs/events-reference#commerce_cart.onbeforeaddtocart" rel="nofollow noreferrer">https://craftcommerce.com/docs/events-reference#commerce_cart.onbeforeaddtocart</a> and does whatever validation you need, and returns a response any way you want, including via JSON (i.e. <code>$this-&gt;returnJson(["success"=&gt;true]);</code> or e.g. <code>$this-&gt;returnErrorJson($errors);</code> where $errors holds your validation errors...)</p>

CC BY-SA 3.0
2016-12-26T12:41:10.523Z
Actually, when sending the form via ajax, you will get a 500 Internal Server Error if you don't add the purchasableId to the query. So I will have to validate this before actually sending anything to the back-end.
Floriskoch
CC BY-SA 3.0
2016-12-26T21:59:21.677Z
Well, that's a bug you should report to support@craftcommerce.com then - the intended behaviour would definitely not be a 500 internal server error.  E.g an incorrect `purchasableId` gives an error...and a missing one obviously should too.  Looking at the cart controller it does seem like it isn't handling a missing Id indeed....
Jeremy Daalder
CC BY-SA 3.0
2016-12-24T00:36:55.913Z
2016-12-24T00:36:55.913Z
Jeremy Daalder
<p>I'm creating a product page for a product with multiple variants. In this case the variant represents a different size (with unique SKU, stock, etc..).</p>

<p>It's laid out like so (don't mind the ugly styling):</p>

<p><a href="https://i.sstatic.net/FEJZI.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/FEJZI.png" alt="product box"></a></p>

<p>I don't want to pre-select a size option because I want to avoid people adding the wrong product to their cart. </p>

<p>In the template there's a hidden field with the <code>purchasableId</code> of the variant. The value attribute starts empty and when people click one of the options it is set to the corresponding id.</p>

<pre><code>&lt;input type="hidden" name="purchasableId" value=""&gt;
</code></pre>

<p>However, when people click 'Add to cart' right away, without selecting a size the following Craft\Exception is shown:</p>

<pre><code>Not a purchasable ID
</code></pre>

<p>How can I customize this error message to notify the customer that they have to select a size option?</p>

<p>I could of course solve this problem by using javascript but that doesn't seem very solid to me. So is there a way of validating the stuff added to the cart in the back-end and getting something like a JSON response?</p>

CC BY-SA 3.0
2016-12-23T20:34:32.043Z
Just an opinion, but this seems to me like exactly the sort of thing you would validate on the front end using js/css/whatever
aw04
<plugin-craftcommerce>
Back-end validation of product variant
# Back-end validation of product variant

(Asked by: username_0 on 2016-12-23)

I'm creating a product page for a product with multiple variants. In this case the variant represents a different size (with unique SKU, stock, etc..).
It's laid out like so (don't mind the ugly styling):

I don't want to pre-select a size option because I want to avoid people adding the wrong product to their cart. 
In the template there's a hidden field with the purchasableId of the variant. The value attribute starts empty and when people click one of the options it is set to the corresponding id.


However, when people click 'Add to cart' right away, without selecting a size the following Craft\Exception is shown:
Not a purchasable ID

How can I customize this error message to notify the customer that they have to select a size option?
I could of course solve this problem by using javascript but that doesn't seem very solid to me. So is there a way of validating the stuff added to the cart in the back-end and getting something like a JSON response?

**Tags:** plugin-craftcommerce
> **Comments:**
> **username_1**: Just an opinion, but this seems to me like exactly the sort of thing you would validate on the front end using js/css/whatever  

---

## 1 answers

### Answer by username_2 on 2016-12-24. Score: 2 | Accepted answer

Well, if you submit to your add to cart form via ajax, you will already get a json response - with an `error` - you can simply grab that error, test for `Not a purchasable ID` and then change the text to whatever you want.

If you're submitting in a non ajax fashion, you won't get a JSON response no matter what clearly.

I agree with the comment above - I'd generally handle this in front end JS, by disabling the add to cart until a size is selected. And I guess if you're allowing your site to use no-js, then in that case I would probably just pre-select the most popular option (Medium, presumably).

You can also write a plugin that listens to <https://craftcommerce.com/docs/events-reference#commerce_cart.onbeforeaddtocart> and does whatever validation you need, and returns a response any way you want, including via JSON (i.e. `$this->returnJson(["success"=>true]);` or e.g. `$this->returnErrorJson($errors);` where $errors holds your validation errors...)

> **Comments:**
> **username_0**: Actually, when sending the form via ajax, you will get a 500 Internal Server Error if you don't add the purchasableId to the query. So I will have to validate this before actually sending anything to the back-end.  
> **username_2**: Well, that's a bug you should report to support@craftcommerce.com then - the intended behaviour would definitely not be a 500 internal server error.  E.g an incorrect `purchasableId` gives an error...and a missing one obviously should too.  Looking at the cart controller it does seem like it isn't handling a missing Id indeed....  

---

