Skip to content

Commit 8c2e178

Browse files
authored
Merge pull request #3515 from magento-trigger/MAGETWO-95212
[trigger] MAGETWO-95212: Dynamic block call to getCurrentUrl method is returning ajax request value
2 parents 2ac6848 + df20af7 commit 8c2e178

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

app/code/Magento/Catalog/Block/Product/AbstractProduct.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public function __construct(\Magento\Catalog\Block\Product\Context $context, arr
125125

126126
/**
127127
* Retrieve url for add product to cart
128+
*
128129
* Will return product view page URL if product has required options
129130
*
130131
* @param \Magento\Catalog\Model\Product $product
@@ -473,7 +474,9 @@ public function getProductDetailsHtml(\Magento\Catalog\Model\Product $product)
473474
}
474475

475476
/**
476-
* @param null $type
477+
* Get the renderer that will be used to render the details block
478+
*
479+
* @param string|null $type
477480
* @return bool|\Magento\Framework\View\Element\AbstractBlock
478481
*/
479482
public function getDetailsRenderer($type = null)
@@ -489,6 +492,8 @@ public function getDetailsRenderer($type = null)
489492
}
490493

491494
/**
495+
* Return the list of details
496+
*
492497
* @return \Magento\Framework\View\Element\RendererList
493498
*/
494499
protected function getDetailsRendererList()

app/code/Magento/Catalog/Helper/Product/Compare.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,15 @@ public function getListUrl()
166166
*/
167167
public function getPostDataParams($product)
168168
{
169-
return $this->postHelper->getPostData($this->getAddUrl(), ['product' => $product->getId()]);
169+
$params = ['product' => $product->getId()];
170+
$requestingPageUrl = $this->_getRequest()->getParam('requesting_page_url');
171+
172+
if (!empty($requestingPageUrl)) {
173+
$encodedUrl = $this->urlEncoder->encode($requestingPageUrl);
174+
$params[\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED] = $encodedUrl;
175+
}
176+
177+
return $this->postHelper->getPostData($this->getAddUrl(), $params);
170178
}
171179

172180
/**

app/code/Magento/Catalog/Test/Mftf/Section/StorefrontCategoryProductSection.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
<element name="productPriceLabel" type="text" selector="//span[@class='price-label'][contains(text(),'{{var1}}')]" parameterized="true"/>
2929
<element name="productPriceLinkAfterLabel" type="text" selector="//span[@class='price-label'][contains(text(),'{{var1}}')]/following::span[contains(text(), '{{var2}}')]" parameterized="true"/>
3030
<element name="ProductAddToCartByName" type="button" selector="//main//li[.//a[contains(text(), '{{var1}}')]]//a[contains(@class, 'tocart')]" parameterized="true"/>
31-
<element name="ProductAddToCompareByName" type="text" selector="//main//li[.//a[contains(text(), '{{var1}}')]]//a[contains(@class, 'tocompare')]" parameterized="true"/>
31+
<!--<element name="ProductAddToCompareByName" type="text" selector="//main//li[.//a[contains(text(), '{{var1}}')]]//a[contains(@class, 'tocompare')]" parameterized="true"/>-->
32+
<element name="ProductAddToCompareByName" type="text" selector="//*[contains(@class,'product-item-info')][descendant::a[contains(text(), '{{var1}}')]]//a[contains(@class, 'tocompare')]" parameterized="true"/>
3233
<element name="ProductImageByNameAndSrc" type="text" selector="//main//li[.//a[contains(text(), '{{var1}}')]]//img[contains(@src, '{{src}}')]" parameterized="true"/>
3334
</section>
3435
</sections>

app/code/Magento/CatalogWidget/Block/Product/ProductsList.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
use Magento\Framework\Pricing\PriceCurrencyInterface;
1212
use Magento\Framework\Serialize\Serializer\Json;
1313
use Magento\Widget\Block\BlockInterface;
14+
use Magento\Framework\Url\EncoderInterface;
1415

1516
/**
1617
* Catalog Products List widget block
1718
*
1819
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
1921
*/
2022
class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implements BlockInterface, IdentityInterface
2123
{
@@ -94,6 +96,11 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
9496
*/
9597
private $json;
9698

99+
/**
100+
* @var \Magento\Framework\Url\EncoderInterface|null
101+
*/
102+
private $urlEncoder;
103+
97104
/**
98105
* @param \Magento\Catalog\Block\Product\Context $context
99106
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
@@ -104,6 +111,7 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
104111
* @param \Magento\Widget\Helper\Conditions $conditionsHelper
105112
* @param array $data
106113
* @param Json|null $json
114+
* @param \Magento\Framework\Url\EncoderInterface|null $urlEncoder
107115
*/
108116
public function __construct(
109117
\Magento\Catalog\Block\Product\Context $context,
@@ -114,7 +122,8 @@ public function __construct(
114122
\Magento\CatalogWidget\Model\Rule $rule,
115123
\Magento\Widget\Helper\Conditions $conditionsHelper,
116124
array $data = [],
117-
Json $json = null
125+
Json $json = null,
126+
EncoderInterface $urlEncoder = null
118127
) {
119128
$this->productCollectionFactory = $productCollectionFactory;
120129
$this->catalogProductVisibility = $catalogProductVisibility;
@@ -123,6 +132,7 @@ public function __construct(
123132
$this->rule = $rule;
124133
$this->conditionsHelper = $conditionsHelper;
125134
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
135+
$this->urlEncoder = $urlEncoder ?: ObjectManager::getInstance()->get(EncoderInterface::class);
126136
parent::__construct(
127137
$context,
128138
$data
@@ -153,6 +163,7 @@ protected function _construct()
153163
* Get key pieces for caching block content
154164
*
155165
* @return array
166+
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
156167
*/
157168
public function getCacheKeyInfo()
158169
{
@@ -230,6 +241,7 @@ protected function _beforeToHtml()
230241
* Prepare and return product collection
231242
*
232243
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
244+
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
233245
*/
234246
public function createCollection()
235247
{
@@ -409,6 +421,22 @@ private function getPriceCurrency()
409421
return $this->priceCurrency;
410422
}
411423

424+
/**
425+
* @inheritdoc
426+
*/
427+
public function getAddToCartUrl($product, $additional = [])
428+
{
429+
$requestingPageUrl = $this->getRequest()->getParam('requesting_page_url');
430+
431+
if (!empty($requestingPageUrl)) {
432+
$additional['useUencPlaceholder'] = true;
433+
$url = parent::getAddToCartUrl($product, $additional);
434+
return str_replace('%25uenc%25', $this->urlEncoder->encode($requestingPageUrl), $url);
435+
}
436+
437+
return parent::getAddToCartUrl($product, $additional);
438+
}
439+
412440
/**
413441
* Get widget block name
414442
*
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
11+
<section name="ProductListWidgetSection">
12+
<element name="AddToCartByName" type="button" selector="//*[contains(@class,'product-item-info')][descendant::a[contains(text(), '{{arg1}}')]]//button[contains(@class,'tocart')]" parameterized="true"/>
13+
<element name="AddToCompareByName" type="button" selector="//*[contains(@class,'product-item-info')][descendant::a[contains(text(), '{{arg1}}')]]//button[contains(@class,'tocompare')]" parameterized="true"/>
14+
</section>
15+
</sections>

0 commit comments

Comments
 (0)