X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   Remove variant selections that are out of stock (https://forum.x-cart.com/showthread.php?t=71567)

totaltec 03-06-2015 12:59 PM

Remove variant selections that are out of stock
 
Okay, you know I rarely ask questions, but this time I am stumped. I am trying to remove options from the select box when they are out of stock.

On the product page, we have attributes selections like size and color. We only want to show the options that are in stock. So I need to edit the template and add an if statement that calls a method and tells me if the selected variant is in stock.

The template I am working in is skins/default/en/product/attribute_value/select/body.tpl

Here is the default code:
Code:

<span class="title">{attribute.name}</span>
<select class="form-control" name="{getName()}" data-attribute-id="{attribute.id}">
  <option FOREACH="getAttrValue(),v" selected="{isSelectedValue(v)}" value="{v.id}">{getOptionTitle(v)}{getModifierTitle(v):h}</option>
</select>


Likely I need to modify this template like so:
Code:

<span class="title">{attribute.name}</span>
<select class="form-control" name="{getName()}" data-attribute-id="{attribute.id}">
  {foreach:getAttrValue(),v}
    {if:isInStock(v)}
      <option selected="{isSelectedValue(v)}" value="{v.id}">{getOptionTitle(v)}{getModifierTitle(v):h}</option>
    {end:}
  {end:}
</select>


But what I am struggling with is how to write the method "ifInStock()" and what class to put it in. Any help would be most appreciated. Tony? :-)

tony_sologubov 03-10-2015 04:31 AM

Re: Remove variant selections that are out of stock
 
Hi, Mike!

X-Cart already has the isInStock() method defined in the
Code:

\XLite\Module\XC\ProductVariants\View\Product\Details\Customer\Stock
class that does what you need. However, it works with the currently selected variant instead of accepting values of selected options and then saying whether this configuration does have in-stock variant or not.

That is why I need to clarify the task a little bit. What if we have 2 colors (white and black) and 3 sizes (S, M, L). Black + L variant is out of stock. Does it mean that one first load, product page will show all values in two select-boxes, but if we choose Black, then we will only need to show S and M options in the second select-box? Or are you expecting it to work in a different way?

Tony.

totaltec 03-10-2015 04:39 AM

Re: Remove variant selections that are out of stock
 
Quote:

Originally Posted by tony_sologubov
Does it mean that one first load, product page will show all values in two select-boxes, but if we choose Black, then we will only need to show S and M options in the second select-box?

Tony,
Thanks a bunch for looking at this for me. I sincerely appreciate it. Yes you are correct, in the case of two attributes we are hoping to show all options until selection of the first removes the possibility of the second. But in the case of a single attribute, (in our example they are shoe sizes) it will only show the sizes that are in stock upon initial page load.

Actually, it would be OK if it only worked on products with a single attribute, and products with multiple attributes were ignored and worked the same as they do now.

cflsystems 03-10-2015 05:19 AM

Re: Remove variant selections that are out of stock
 
It will not work good for multiple options. If you have 2 options (color and size) and you select color which returns only 2 out of 3 sizes - now you cannot select the 3rd size and adjust colors this way. In order to go back you have to reset color selection so you can select size...
Also what if customer starts from size instead? Now color is limited (or it should be)...

Another approach to make it clearer would be to not take out option but make it gray and non-selectable. That way customer can see all options and be informed

tony_sologubov 03-13-2015 01:45 AM

Re: Remove variant selections that are out of stock
 
Hi Mike!

Sorry, it took me longer than expected to send you code sample. Here we go though.

1) Have a look at the HTML code that represents options selector in store-front. It is something similar to:
Code:

<ul class="attribute-values">
  <li>
<span class="title">color</span>
<select class="form-control" name="attribute_values[51]" data-attribute-id="51">
  <option value="248">white</option>
<option selected="selected" value="249">black</option>
</select>
</li>
<li>
<span class="title">size</span>
<select class="form-control" name="attribute_values[52]" data-attribute-id="52">
  <option selected="selected" value="250">s</option>
<option value="251">m</option>
<option value="252">l</option>
</select>
</li>
</ul>


Values marked by green are IDs of product attributes. In other words, they are IDs of attribute Color and attribute Size.

Values marked by red are IDs of product attribute values. In other words, they are IDs of White, Black, S, M and L attribute values.

2) Below is a test script that gives you an idea of how to check availability of a variant with given attribute values:

PHP Code:

<?php

//X-Cart initializtion
require_once 'top.inc.php';
require_once 
'top.inc.additional.php';

$product = \XLite\Core\Database::getRepo('\XLite\Model\Product')->find(5);

$attributes = array(
    
51 => 248,
    
52 => 250,
    );

$preparedAttributes $product->prepareAttributeValues($attributes);
$pVariant $product->getVariant($preparedAttributes);
$result $pVariant->isOutOfStock();
var_dump($result);


This script assumes that you check attribute combination for product with ID = 5:
PHP Code:

$product = \XLite\Core\Database::getRepo('\XLite\Model\Product')->find(5); 


It also assumes that the script will be run from root folder of X-Cart 5.

Please, let me know if this code sample is helpful.

Tony.


All times are GMT -8. The time now is 12:58 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.