Skip to content

Commit de4872a

Browse files
committed
MAGE-674 Add frontend capture of facet routing params for redirect after add to cart from PLP
1 parent 52ef8f5 commit de4872a

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

Block/Algolia.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,12 @@ public function getLastOrder()
270270
return $this->checkoutSession->getLastRealOrder();
271271
}
272272

273-
public function getAddToCartParams()
273+
public function getAddToCartParams() : array
274274
{
275-
$url = $this->getAddToCartUrl();
276-
277275
return [
278-
'action' => $url,
276+
'action' => $this->_urlBuilder->getUrl('checkout/cart/add', []),
279277
'formKey' => $this->formKey->getFormKey(),
278+
'redirectUrlParam' => ActionInterface::PARAM_NAME_URL_ENCODED
280279
];
281280
}
282281

@@ -285,6 +284,9 @@ public function getTimestamp()
285284
return $this->date->gmtTimestamp('today midnight');
286285
}
287286

287+
/**
288+
* @deprecated This function is deprecated as redirect routes must be derived on the frontend not backend
289+
*/
288290
protected function getAddToCartUrl($additional = [])
289291
{
290292
$continueUrl = $this->urlHelper->getEncodedUrl($this->_urlBuilder->getCurrentUrl());

Block/Configuration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ public function getConfiguration()
233233
'path' => $path,
234234
'level' => $level,
235235
'parentCategory' => $parentCategoryName,
236+
'url' => $this->getUrl('*/*/*', ['_use_rewrite' => true, '_forced_secure' => true])
236237
],
237238
'showCatsNotIncludedInNavigation' => $config->showCatsNotIncludedInNavigation(),
238239
'showSuggestionsOnNoResultsPage' => $config->showSuggestionsOnNoResultsPage(),
@@ -381,4 +382,4 @@ protected function getLandingPageConfiguration()
381382
{
382383
return $this->isLandingPage() ? $this->getCurrentLandingPage()->getConfiguration() : json_encode([]);
383384
}
384-
}
385+
}

view/frontend/templates/instant/hit.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ $origFormatedVar = $block->escapeHtml('price' . $priceKey . '_original_formated'
8686
<input type="hidden" name="queryID" value="{{__queryID}}">
8787
<input type="hidden" name="product" value="{{objectID}}">
8888
{{#_highlightResult.default_bundle_options}}<input type="hidden" name="bundle_option[{{ optionId }}]" value="{{selectionId}}">{{/_highlightResult.default_bundle_options}}
89-
<input type="hidden" name="uenc" value="{{ addToCart.uenc }}">
89+
<input type="hidden" name="{{ addToCart.redirectUrlParam }}" value="{{ addToCart.uenc }}">
9090
<input name="form_key" type="hidden" value="{{ addToCart.formKey }}">
9191
<button type="submit" title="<?php echo __('Add to Cart') ?>" class="action tocart primary"
9292
data-objectid="{{objectID}}"

view/frontend/web/instantsearch.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,18 @@ define(
638638

639639
var isStarted = false;
640640

641+
// Capture active redirect URL with IS facet params for add to cart from PLP
642+
search.on('render', () => {
643+
const cartForms = document.querySelectorAll('[data-role="tocart-form"]');
644+
cartForms.forEach((form, i) => {
645+
const ts = Date.now();
646+
form.addEventListener('submit', e => {
647+
const url = `${algoliaConfig.request.url}${window.location.search}`;
648+
e.target.elements[algoliaConfig.instant.addToCartParams.redirectUrlParam].value = AlgoliaBase64.mageEncode(url);
649+
})
650+
});
651+
});
652+
641653
function startInstantSearch() {
642654
if (isStarted === true) {
643655
return;
@@ -689,4 +701,4 @@ define(
689701
return options;
690702
}
691703
}
692-
);
704+
);

view/frontend/web/internals/common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ define(['jquery', 'algoliaBundle'], function ($, algoliaBundle) {
198198

199199
hit.addToCart = {
200200
'action': action,
201+
'redirectUrlParam': algoliaConfig.instant.addToCartParams.redirectUrlParam,
201202
'uenc': AlgoliaBase64.mageEncode(action),
202203
'formKey': algoliaConfig.instant.addToCartParams.formKey
203204
};

0 commit comments

Comments
 (0)