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)
-   -   Math IF/ELSE in version 5 (https://forum.x-cart.com/showthread.php?t=72215)

RichieRich 06-02-2015 02:52 PM

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()))} )

qualiteam 06-02-2015 10:56 PM

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.

RichieRich 06-03-2015 12:31 AM

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.

tony_sologubov 06-03-2015 04:02 AM

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.

RichieRich 06-03-2015 08:47 AM

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

qualiteam 06-04-2015 11:38 AM

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.

RichieRich 06-04-2015 12:02 PM

Re: Math IF/ELSE in version 5
 
Great thanks a lot both for the help !

qualiteam 06-04-2015 12:08 PM

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>
...


RichieRich 06-04-2015 12:27 PM

Re: Math IF/ELSE in version 5
 
Perfect thank you


All times are GMT -8. The time now is 11:36 AM.

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