Skip to content

Commit

Permalink
Generate a fresh formkey at result build
Browse files Browse the repository at this point in the history
  • Loading branch information
ProxiBlue committed Apr 2, 2022
1 parent 4d09665 commit 50737ed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
Empty file modified LICENCE
100644 → 100755
Empty file.
39 changes: 30 additions & 9 deletions Plugin/ItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -34,7 +37,7 @@ $heroiconsOutline = $viewModels->require(HeroiconsOutline::class);
<div class="col-span-2 content-end items-end">
<form method="post"
x-bind:action="searchResult.add_url">
<?= /** @noEscape */ $block->getBlockHtml('formkey') ?>
<input name="form_key" type="hidden" x-bind:value="searchResult.formkey">
<button class="btn btn-primary"
aria-label="<?= $escaper->escapeHtmlAttr(__('Add to Cart')) ?>"
>
Expand Down

0 comments on commit 50737ed

Please sign in to comment.