Skip to content

Commit

Permalink
Wrap cod ei n trap to log any failures and allow normal autocomplete …
Browse files Browse the repository at this point in the history
…row to render item (without qty/add-to-cart), if any error happens. Chnage way the stock item is gathered, as previous way gate stock ID not found errors
  • Loading branch information
ProxiBlue committed Apr 12, 2022
1 parent 1d5e76a commit 6590898
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions Plugin/ItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Magento\Catalog\Model\Product;
use Magento\Checkout\Helper\Cart as CartHelper;
use Magento\Framework\Data\Form\FormKey;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
use Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory;
use Psr\Log\LoggerInterface;

// phpcs:disable Generic.Files.LineLength.TooLong

Expand All @@ -28,9 +29,9 @@ class ItemFactory
protected $formKey;

/**
* @var StockItemRepository
* @var StockItemInterfaceFactory
*/
private $stockItemRepository;
private $stockItemInterfaceFactory;

/**
* Cached stock data
Expand All @@ -39,6 +40,11 @@ class ItemFactory
*/
private $stockItem = null;

/**
* @var LoggerInterface
*/
protected $logger;

/**
* Constructor
*
Expand All @@ -48,12 +54,14 @@ class ItemFactory
public function __construct(
CartHelper $cartHelper,
FormKey $formKey,
StockItemRepository $stockItemRepository
StockItemInterfaceFactory $stockItemInterfaceFactory,
LoggerInterface $logger
)
{
$this->cartHelper = $cartHelper;
$this->formKey = $formKey;
$this->stockItemRepository = $stockItemRepository;
$this->stockItemInterfaceFactory = $stockItemInterfaceFactory;
$this->logger = $logger;
}

/**
Expand All @@ -68,21 +76,26 @@ public function __construct(
*/
public function beforeCreate(\Smile\ElasticsuiteCatalog\Model\Autocomplete\Product\ItemFactory $subject, array $data)
{
if ($data['product']
&& $data['product']->isSaleable()
&& $data['product']->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
) {
$data['qty_min'] = $this->getMinQty($data['product']);
$data['qty_max'] = $this->getMaxQty($data['product']);
$data['qty_increments_enabled'] = $this->areQtyIncrementsEnabled($data['product']);
$data['qty_increments'] = $this->getQtyIncrements($data['product']);
$data['qty_len'] = $this->getMaxQtyLength($data['product']);
$data['qty_step'] = $this->getQtyStep($data['product']);
$data['qty_format'] = $this->getQtyFormat($data['product']);
$data['add_url'] = $this->cartHelper->getAddUrl($data['product'], ['useUencPlaceholder' => true]);
$data['formkey'] = $this->formKey->getFormKey();
$data['type'] = 'product_add_to_cart';
$data['form_name'] = "ac_addtocart_" . $data['product']->getId();
try {
if ($data['product']
&& $data['product']->isSaleable()
&& $data['product']->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
) {
$this->stockItem = null;
$data['qty_min'] = $this->getMinQty($data['product']);
$data['qty_max'] = $this->getMaxQty($data['product']);
$data['qty_increments_enabled'] = $this->areQtyIncrementsEnabled($data['product']);
$data['qty_increments'] = $this->getQtyIncrements($data['product']);
$data['qty_len'] = $this->getMaxQtyLength($data['product']);
$data['qty_step'] = $this->getQtyStep($data['product']);
$data['qty_format'] = $this->getQtyFormat($data['product']);
$data['add_url'] = $this->cartHelper->getAddUrl($data['product'], ['useUencPlaceholder' => true]);
$data['formkey'] = $this->formKey->getFormKey();
$data['type'] = 'product_add_to_cart';
$data['form_name'] = "ac_addtocart_" . $data['product']->getId();
}
} catch (\Exception $e) {
$this->logger->debug($e->getMessage());
}

return [$data];
Expand All @@ -96,7 +109,7 @@ public function beforeCreate(\Smile\ElasticsuiteCatalog\Model\Autocomplete\Produ
public function getStockItem(ProductInterface $product)
{
if (empty($this->stockItem)) {
$this->stockItem = $this->stockItemRepository->get($product->getId());
$this->stockItem = $this->stockItemInterfaceFactory->create()->load($product->getId());
}
return $this->stockItem;
}
Expand Down
File renamed without changes.

0 comments on commit 6590898

Please sign in to comment.