0

I'm using expresso store and apart from regular products I have gift boxes which contain multiple products. They are a separate channel and I'm using playa to pick the products for each gift box entry.

what I need to do is check the stock level for each individual product and if any of those products are out of stock then the gift box should not be available for purchase.

If I put the conditional inside the loop then I will get 3-4 results and I only need one of them to be 0 in order to hide the add to cart button.

So I need a conditional that will wrap around all of the related entries for the gift box and if any of the items have a stock level of 0 it should disable to button.

EDIT: so the code I have for normal products is as follows:

{if total_stock > 0}
<input type="submit" name="submit" value="add to cart"/>                    
{if:else}
<p>Product is unavailable</p>
{/if}

what I need for gift boxes should be something like:

{if "{exp:playa:children field="gift_products"}{stock}{/exp:playa:children}"
 *="0"}
<p>Product is unavailable</p>
{if:else}
<input type="submit" name="submit" value="add to cart"/> 
{/if}

so if the stock level of multiple related products is 0 add to cart should not be available

  • Could you update this question with the relevant code block from your template? It will make it easier for us to help you. – jrothafer Jan 22 '16 at 23:20
  • I just did, I haven't gotten far with the code I'm afraid so there isn't much more to tell – Dovla Jan 23 '16 at 23:49
1

You can't obviously check for just one of them out of stock numerically, so you have to concatenate them together with a key identifier, so for each loop start the total stock with a character "#", then if "#0" exists, you know that one of the items is out (albeit you won't know which).

So assuming product A has 5, Product B has 0 and Product C has 2, your loop will output something like this: "#5#0#2"

{if "{exp:playa:children field="gift_products" var_prefix="product"}{exp:store:product entry_id="{product:entry_id}"}#{total_stock}{/exp:store:product}{/exp:playa:children}" *= "#0"}
    <p>Product is unavailable</p>
{if:else}
    <input type="submit" name="submit" value="add to cart"/> 
{/if}

I've added the product tag pair as I'm assuming it's needed, along with the var_prefix parameter, but not tested the exact syntax, may well require tweaking.

  • thanks, this actually worked perfectly, only thing I had to do was use single quotes instead of double quotes for playa...also the store wrapper wasn't needed because it adds a form and an inline javascript which actually messes up the conditional, I just used the store fieldtype along with the total_stock variable like so: {store:total_stock} – Dovla Jan 25 '16 at 22:01

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.