Created
September 4, 2017 17:33
-
-
Save chornobils/f3ec87b057b836c7e5ddad288a65fed1 to your computer and use it in GitHub Desktop.
Sylius remaining Twig helper. Returns remaining number of product variant.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Inventory; | |
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface; | |
use Sylius\Component\Inventory\Model\StockableInterface; | |
/** | |
* @author Paweł Jędrzejewski <[email protected]> | |
*/ | |
final class RemainingAvailabilityChecker implements AvailabilityCheckerInterface { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function isStockAvailable(StockableInterface $stockable) | |
{ | |
return $this->isStockSufficient($stockable, 1); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function isStockSufficient(StockableInterface $stockable, $quantity) | |
{ | |
return !$stockable->isTracked() || $quantity <= ($stockable->getOnHand() - $stockable->getOnHold()); | |
} | |
public function getRemainingNumber(StockableInterface $stockable) { | |
return $stockable->getOnHand() - $stockable->getOnHold(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Inventory; | |
use AppBundle\Inventory\RemainingInventoryHelper; | |
/** | |
* @author Paweł Jędrzejewski <[email protected]> | |
*/ | |
final class RemainingInventoryExtension extends \Twig_Extension { | |
/** | |
* @var RemainingInventoryHelper | |
*/ | |
private $helper; | |
/** | |
* @param RemainingInventoryHelper $helper | |
*/ | |
public function __construct(RemainingInventoryHelper $helper) | |
{ | |
$this->helper = $helper; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getFunctions() | |
{ | |
return [ | |
new \Twig_SimpleFunction('sylius_inventory_is_available', [$this->helper, 'isStockAvailable']), | |
new \Twig_SimpleFunction('sylius_inventory_is_sufficient', [$this->helper, 'isStockSufficient']), | |
new \Twig_SimpleFunction('sylius_inventory_get_remaining', [$this->helper, 'getItemsRemainingNumber']), | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Inventory; | |
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface; | |
use Sylius\Component\Inventory\Model\StockableInterface; | |
use Symfony\Component\Templating\Helper\Helper; | |
/** | |
* @author Paweł Jędrzejewski <[email protected]> | |
*/ | |
final class RemainingInventoryHelper extends Helper { | |
/** | |
* @var AvailabilityCheckerInterface | |
*/ | |
private $checker; | |
/** | |
* @param AvailabilityCheckerInterface $checker | |
*/ | |
public function __construct(AvailabilityCheckerInterface $checker) | |
{ | |
$this->checker = $checker; | |
} | |
/** | |
* @param StockableInterface $stockable | |
* | |
* @return bool | |
*/ | |
public function isStockAvailable(StockableInterface $stockable) | |
{ | |
return $this->checker->isStockAvailable($stockable); | |
} | |
/** | |
* @param StockableInterface $stockable | |
* @param int $quantity | |
* | |
* @return bool | |
*/ | |
public function isStockSufficient(StockableInterface $stockable, $quantity) | |
{ | |
return $this->checker->isStockSufficient($stockable, $quantity); | |
} | |
/** | |
* @param \Sylius\Component\Inventory\Model\StockableInterface $stockable | |
* @return int | |
*/ | |
public function getItemsRemainingNumber(StockableInterface $stockable) { | |
return $this->checker->getRemainingNumber($stockable); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return 'sylius_inventory'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sylius.availability_checker.remaining: | |
class: AppBundle\Inventory\RemainingAvailabilityChecker | |
sylius.templating.helper.inventory: | |
class: AppBundle\Inventory\RemainingInventoryHelper | |
arguments: | |
- "@sylius.availability_checker.remaining" | |
tags: | |
- { name: templating.helper, alias: sylius_inventory } | |
sylius.twig.extension.inventory: | |
class: AppBundle\Inventory\RemainingInventoryExtension | |
public: false | |
arguments: | |
- "@sylius.templating.helper.inventory" | |
tags: | |
- { name: twig.extension } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if sylius_inventory_get_remaining(item.productVariant) < 10 %} | |
<div class='attention'>{{ sylius_inventory_get_remaining(item.productVariant) }} pieces left, hurry up!</div> | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment