Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Remove variant selections that are out of stock

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 03-06-2015, 12:59 PM
  totaltec's Avatar 
totaltec totaltec is offline
 

X-Guru
  
Join Date: Jan 2007
Location: Louisville, KY USA
Posts: 5,823
 

Default 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?
__________________
Mike White - Now Accepting new clients and projects! Work with the best, get a US based development team for just $125 an hour. Call 1-502-773-6454, email mike at babymonkeystudios.com, or skype b8bym0nkey

XcartGuru
X-cart Tutorials | X-cart 5 Tutorials

Check out the responsive template for X-cart.
Reply With Quote
  #2  
Old 03-10-2015, 04:31 AM
  tony_sologubov's Avatar 
tony_sologubov tony_sologubov is offline
 

X-Cart team
  
Join Date: Jan 2009
Posts: 2,431
 

Default 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.
__________________
Found a bug in X-Cart? Post it to our bug tracker!
Know how to make X-Cart better? Suggest an idea!

Last edited by tony_sologubov : 03-13-2015 at 01:16 AM.
Reply With Quote

The following user thanks tony_sologubov for this useful post:
totaltec (03-10-2015)
  #3  
Old 03-10-2015, 04:39 AM
  totaltec's Avatar 
totaltec totaltec is offline
 

X-Guru
  
Join Date: Jan 2007
Location: Louisville, KY USA
Posts: 5,823
 

Default 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.
__________________
Mike White - Now Accepting new clients and projects! Work with the best, get a US based development team for just $125 an hour. Call 1-502-773-6454, email mike at babymonkeystudios.com, or skype b8bym0nkey

XcartGuru
X-cart Tutorials | X-cart 5 Tutorials

Check out the responsive template for X-cart.
Reply With Quote
  #4  
Old 03-10-2015, 05:19 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,026
 

Default 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
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote

The following 2 users thank cflsystems for this useful post:
Ksenia (03-13-2015), totaltec (03-10-2015)
  #5  
Old 03-13-2015, 01:45 AM
  tony_sologubov's Avatar 
tony_sologubov tony_sologubov is offline
 

X-Cart team
  
Join Date: Jan 2009
Posts: 2,431
 

Default 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.
__________________
Found a bug in X-Cart? Post it to our bug tracker!
Know how to make X-Cart better? Suggest an idea!
Reply With Quote

The following user thanks tony_sologubov for this useful post:
totaltec (03-13-2015)
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 09:50 PM.

   

 
X-Cart forums © 2001-2020