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

Math IF/ELSE in version 5

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

X-Adept
  
Join Date: Sep 2004
Location: London, England
Posts: 750
 

Default Math IF/ELSE in version 5

I am attempting to display the stock level only if it is a low number, (i.e.. below 10) so items which have 431 in stock, is not necessary to display this information.

In version 4 it was quite straightforward, however I cannot work out how to do this in version 5. Can anybody help?


This is the pieces of code from the product/details/stock/body.tpl

({t(#X items available#,_ARRAY_(#count#^getAvailableAmount()))} )
__________________
Richard


Ultimate 5.4 testing
Reply With Quote
  #2  
Old 06-02-2015, 10:56 PM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Math IF/ELSE in version 5

Hello Richard,

Here is how I would do this to retain the compatibility with future X-Cart 5 versions:

1. Create a custom module

2. Copy the skins/default/en/product/details/stock/body.tpl file to skins/default/en/modules/[YOUR_DEV_ID]/[YOUR_MODULE_ID]/product/details/stock/body.tpl and change it as follows:
HTML Code:
... <span class="product-items-available" IF="isItemsAvailableVisible()">({t(#X items available#,_ARRAY_(#count#^getAvailableAmount()))})</span> ...

3. Decorate \XLite\View\Product\Details\Customer\Stock class as follows:

classes/XLite/Module/[YOUR_DEV_ID]/[YOUR_MODULE_ID]/View/Product/Details/Customer/Stock.php

PHP Code:
namespace XLite\Module\[YOUR_DEV_ID]\[YOUR_MODULE_ID]\View\Product\Details\Customer;

abstract class 
Stock  extends \XLite\View\Product\Details\Customer\Stock implements \XLite\Base\IDecorator
{
  protected function 
isItemsAvailableVisible()
  {
    
// Add your logic here and set $flag to TRUE when the stock level is to be displayed
    
return $flag;
  }

  protected function 
getDefaultTemplate()
  {
    return 
'modules/[YOUR_DEV_ID]/[YOUR_MODULE_ID]/product/details/stock/body.tpl';
  }



Haven't tested the code, but it should work as is.
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote

The following user thanks qualiteam for this useful post:
RichieRich (06-03-2015)
  #3  
Old 06-03-2015, 12:31 AM
  RichieRich's Avatar 
RichieRich RichieRich is offline
 

X-Adept
  
Join Date: Sep 2004
Location: London, England
Posts: 750
 

Default Re: Math IF/ELSE in version 5

Great thank you. I will add it. How can I set it to display the amount in stock only if there are less than 10 in stock.
__________________
Richard


Ultimate 5.4 testing
Reply With Quote
  #4  
Old 06-03-2015, 04:02 AM
  tony_sologubov's Avatar 
tony_sologubov tony_sologubov is offline
 

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

Default Re: Math IF/ELSE in version 5

You need to add the following method into the class suggested by Alex:

PHP Code:
protected function isVisible()
{
    
$return parent::isVisible();

    if (
$this->getProduct()->getInventory()->getAvailableAmount() > 10) {
        
$return false;
    }

    return 
$return;



and it should do the trick.

Did not test the code though.
__________________
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:
RichieRich (06-03-2015)
  #5  
Old 06-03-2015, 08:47 AM
  RichieRich's Avatar 
RichieRich RichieRich is offline
 

X-Adept
  
Join Date: Sep 2004
Location: London, England
Posts: 750
 

Default Re: Math IF/ELSE in version 5

Thank you Tony, it almost works perfectly, however it is disabling the the whole class. Is it possible to display an alternate message instead of disabling the class? Maybe it is with the if/else statement in the code.

For example it has;

In stock (8 available) or it won't show anything.

I would prefer to have another message;

In stock (more than 10 available)


Thanks in advance
__________________
Richard


Ultimate 5.4 testing
Reply With Quote
  #6  
Old 06-04-2015, 11:38 AM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Math IF/ELSE in version 5

Hello Richard,

Just combine the code provided by Tony with mine

PHP Code:
namespace XLite\Module\[YOUR_DEV_ID]\[YOUR_MODULE_ID]\View\Product\Details\Customer;

abstract class 
Stock  extends \XLite\View\Product\Details\Customer\Stock implements \XLite\Base\IDecorator
{
  protected function 
isItemsAvailableVisible()
  {
    return (
$this->getProduct()->getInventory()->getAvailableAmount() > 10);
  }

  protected function 
getDefaultTemplate()
  {
    return 
'modules/[YOUR_DEV_ID]/[YOUR_MODULE_ID]/product/details/stock/body.tpl';
  }



This way you keep the widget visible (isVisible() is not modified), but use a different template file that hides the portion of the message when isItemsAvailableVisible() method returns FALSE.

Don't forget to add the modified template to your module.
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote

The following user thanks qualiteam for this useful post:
RichieRich (06-04-2015)
  #7  
Old 06-04-2015, 12:02 PM
  RichieRich's Avatar 
RichieRich RichieRich is offline
 

X-Adept
  
Join Date: Sep 2004
Location: London, England
Posts: 750
 

Default Re: Math IF/ELSE in version 5

Great thanks a lot both for the help !
__________________
Richard


Ultimate 5.4 testing
Reply With Quote
  #8  
Old 06-04-2015, 12:08 PM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Math IF/ELSE in version 5

Try this:
HTML Code:
... <span class="product-items-available" IF="isItemsAvailableVisible()">({t(#X items available#,_ARRAY_(#count#^getAvailableAmount()))})</span> <span class="product-items-available-alternate" IF="!isItemsAvailableVisible()">({t(#Your message#})</span> ...
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
  #9  
Old 06-04-2015, 12:27 PM
  RichieRich's Avatar 
RichieRich RichieRich is offline
 

X-Adept
  
Join Date: Sep 2004
Location: London, England
Posts: 750
 

Default Re: Math IF/ELSE in version 5

Perfect thank you
__________________
Richard


Ultimate 5.4 testing
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)



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 05:16 PM.

   

 
X-Cart forums © 2001-2020