From 50737ed5d2113d990ebde80cc347f14e5b4e2cdc Mon Sep 17 00:00:00 2001 From: Lucas van Staden Date: Sat, 2 Apr 2022 00:29:39 +0000 Subject: [PATCH] Generate a fresh formkey at result build --- LICENCE | 0 Plugin/ItemFactory.php | 39 ++++++++++++++----- .../autocomplete/product_add_to_cart.phtml | 5 ++- 3 files changed, 34 insertions(+), 10 deletions(-) mode change 100644 => 100755 LICENCE diff --git a/LICENCE b/LICENCE old mode 100644 new mode 100755 diff --git a/Plugin/ItemFactory.php b/Plugin/ItemFactory.php index 08ff665..00e9c64 100755 --- a/Plugin/ItemFactory.php +++ b/Plugin/ItemFactory.php @@ -3,38 +3,59 @@ namespace ProxiBlue\HyvaSmileAutocompleteAddToCart\Plugin; use Magento\Checkout\Helper\Cart as CartHelper; +use Magento\Framework\Data\Form\FormKey; + +// phpcs:disable Generic.Files.LineLength.TooLong class ItemFactory { /** + * Cart helper to get add to cart url + * * @var CartHelper */ protected $cartHelper; /** + * Generate an up-to-date form key + * + * @var FormKey + */ + protected $formKey; + + /** + * Constructor + * * @param CartHelper $cartHelper + * @param FormKey $formKey */ public function __construct( - CartHelper $cartHelper + CartHelper $cartHelper, + FormKey $formKey ) { - $this->cartHelper = $cartHelper; + $this->cartHelper = $cartHelper; + $this->formKey = $formKey; } /** * Inject the add to cart url here * - * @param \Smile\ElasticsuiteCatalog\Model\Autocomplete\Product\ItemFactory $subject - * @param callable $proceed - * @param $data - * @return mixed + * @param \Smile\ElasticsuiteCatalog\Model\Autocomplete\Product\ItemFactory $subject Object + * @param array $data Array of values + * + * @return array + * + * @throws \Magento\Framework\Exception\LocalizedException */ - public function beforeCreate(\Smile\ElasticsuiteCatalog\Model\Autocomplete\Product\ItemFactory $subject, $data) + 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['add_url'] = $this->cartHelper->getAddUrl($data['product'], ['useUencPlaceholder'=>true]); + && $data['product']->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE + ) { + $data['add_url'] = $this->cartHelper->getAddUrl($data['product'], ['useUencPlaceholder' => true]); + $data['formkey'] = $this->formKey->getFormKey(); $data['type'] = 'product_add_to_cart'; } diff --git a/view/frontend/templates/catalog/autocomplete/product_add_to_cart.phtml b/view/frontend/templates/catalog/autocomplete/product_add_to_cart.phtml index 3b87e54..ba4a909 100755 --- a/view/frontend/templates/catalog/autocomplete/product_add_to_cart.phtml +++ b/view/frontend/templates/catalog/autocomplete/product_add_to_cart.phtml @@ -10,11 +10,14 @@ declare(strict_types=1); use Hyva\Theme\Model\ViewModelRegistry; use Hyva\Theme\ViewModel\HeroiconsOutline; +use Magento\Framework\Escaper; +use Magento\Framework\View\Element\Template; // phpcs:disable Generic.Files.LineLength.TooLong /** @var ViewModelRegistry $viewModels */ /** @var Escaper $escaper */ +/** @var Template $block */ /** @var HeroiconsOutline $heroiconsOutline */ $heroiconsOutline = $viewModels->require(HeroiconsOutline::class); @@ -34,7 +37,7 @@ $heroiconsOutline = $viewModels->require(HeroiconsOutline::class);
- getBlockHtml('formkey') ?> +