Skip to content

Instantly share code, notes, and snippets.

@cornobils
Created September 4, 2017 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cornobils/f3ec87b057b836c7e5ddad288a65fed1 to your computer and use it in GitHub Desktop.
Save cornobils/f3ec87b057b836c7e5ddad288a65fed1 to your computer and use it in GitHub Desktop.
Sylius remaining Twig helper. Returns remaining number of product variant.
<?php
namespace AppBundle\Inventory;
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
use Sylius\Component\Inventory\Model\StockableInterface;
/**
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
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();
}
}
<?php
namespace AppBundle\Inventory;
use AppBundle\Inventory\RemainingInventoryHelper;
/**
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
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']),
];
}
}
<?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 <pawel@sylius.org>
*/
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';
}
}
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 }
{% 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