Skip to content

Commit 851501e

Browse files
MC-31979: Product stock alert - unsubscribe for the product not working because cannot be accessed with GET method
1 parent ecaa3b7 commit 851501e

File tree

6 files changed

+110
-2
lines changed

6 files changed

+110
-2
lines changed

app/code/Magento/ProductAlert/Block/Email/Stock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function getProductUnsubscribeUrl($productId)
2727
{
2828
$params = $this->_getUrlParams();
2929
$params['product'] = $productId;
30-
return $this->getUrl('productalert/unsubscribe/stock', $params);
30+
return $this->getUrl('productalert/unsubscribe/email', $params);
3131
}
3232

3333
/**
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\ProductAlert\Controller\Unsubscribe;
10+
11+
use Magento\Framework\App\Action\Context;
12+
use Magento\Framework\App\Action\HttpGetActionInterface;
13+
use Magento\Framework\App\Action\Action;
14+
use Magento\Framework\View\Result\Page;
15+
use Magento\Framework\View\Result\PageFactory;
16+
17+
/**
18+
* Unsubscribing from 'Back in stock Alert'.
19+
*
20+
* Is used to transform a Get request that triggered in the email into the Post request endpoint
21+
*/
22+
class Email extends Action implements HttpGetActionInterface
23+
{
24+
/**
25+
* @var PageFactory
26+
*/
27+
private $resultPageFactory;
28+
29+
/**
30+
* @param Context $context
31+
* @param PageFactory $resultPageFactory
32+
*/
33+
public function __construct(
34+
Context $context,
35+
PageFactory $resultPageFactory
36+
) {
37+
$this->resultPageFactory = $resultPageFactory;
38+
parent::__construct($context);
39+
}
40+
41+
/**
42+
* Processes the the request triggered in Unsubscription email related to 'back in stock alert'.
43+
*
44+
* @return Page
45+
*/
46+
public function execute(): Page
47+
{
48+
$productId = (int)$this->getRequest()->getParam('product');
49+
/** @var Page $resultPage */
50+
$resultPage = $this->resultPageFactory->create();
51+
/** @var @va \Magento\Framework\View\Element\AbstractBlock $block */
52+
$block = $resultPage->getLayout()->getBlock('unsubscription_form');
53+
$block->setProductId($productId);
54+
return $resultPage;
55+
}
56+
}

app/code/Magento/ProductAlert/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"magento/module-backend": "*",
1111
"magento/module-catalog": "*",
1212
"magento/module-customer": "*",
13-
"magento/module-store": "*"
13+
"magento/module-store": "*",
14+
"magento/module-theme": "*"
1415
},
1516
"suggest": {
1617
"magento/module-config": "*"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9+
<body>
10+
<referenceContainer name="content">
11+
<block class="Magento\Framework\View\Element\Template" name="unsubscription_form" template="Magento_ProductAlert::email/email.phtml" />
12+
</referenceContainer>
13+
</body>
14+
</page>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var $block Magento\Framework\View\Element\Template */
8+
?>
9+
10+
<form action="<?= $block->escapeUrl($block->getUrl('productalert/unsubscribe/stock')) ?>"
11+
method="post"
12+
data-form="unsubscription_form">
13+
<?= /* @noEscape */ $block->getBlockHtml('formkey') ?>
14+
<input type="hidden" id="productId" name="product" value="<?= $block->escapeHtml($block->getProductId()) ?>" />
15+
</form>
16+
<script type="text/x-magento-init">
17+
{
18+
"[data-form=unsubscription_form]": {
19+
"Magento_ProductAlert/js/form-submitter": {}
20+
}
21+
}
22+
</script>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery'
8+
], function ($) {
9+
'use strict';
10+
11+
return function (data, element) {
12+
13+
$(element).submit();
14+
};
15+
});

0 commit comments

Comments
 (0)