Skip to content

Commit

Permalink
Merge pull request #3515 from magento-trigger/MAGETWO-95212
Browse files Browse the repository at this point in the history
[trigger] MAGETWO-95212: Dynamic block call to getCurrentUrl method is returning ajax request value
  • Loading branch information
joanhe authored Dec 7, 2018
2 parents 2ac6848 + df20af7 commit 8c2e178
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
7 changes: 6 additions & 1 deletion app/code/Magento/Catalog/Block/Product/AbstractProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public function __construct(\Magento\Catalog\Block\Product\Context $context, arr

/**
* Retrieve url for add product to cart
*
* Will return product view page URL if product has required options
*
* @param \Magento\Catalog\Model\Product $product
Expand Down Expand Up @@ -473,7 +474,9 @@ public function getProductDetailsHtml(\Magento\Catalog\Model\Product $product)
}

/**
* @param null $type
* Get the renderer that will be used to render the details block
*
* @param string|null $type
* @return bool|\Magento\Framework\View\Element\AbstractBlock
*/
public function getDetailsRenderer($type = null)
Expand All @@ -489,6 +492,8 @@ public function getDetailsRenderer($type = null)
}

/**
* Return the list of details
*
* @return \Magento\Framework\View\Element\RendererList
*/
protected function getDetailsRendererList()
Expand Down
10 changes: 9 additions & 1 deletion app/code/Magento/Catalog/Helper/Product/Compare.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,15 @@ public function getListUrl()
*/
public function getPostDataParams($product)
{
return $this->postHelper->getPostData($this->getAddUrl(), ['product' => $product->getId()]);
$params = ['product' => $product->getId()];
$requestingPageUrl = $this->_getRequest()->getParam('requesting_page_url');

if (!empty($requestingPageUrl)) {
$encodedUrl = $this->urlEncoder->encode($requestingPageUrl);
$params[\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED] = $encodedUrl;
}

return $this->postHelper->getPostData($this->getAddUrl(), $params);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
<element name="productPriceLabel" type="text" selector="//span[@class='price-label'][contains(text(),'{{var1}}')]" parameterized="true"/>
<element name="productPriceLinkAfterLabel" type="text" selector="//span[@class='price-label'][contains(text(),'{{var1}}')]/following::span[contains(text(), '{{var2}}')]" parameterized="true"/>
<element name="ProductAddToCartByName" type="button" selector="//main//li[.//a[contains(text(), '{{var1}}')]]//a[contains(@class, 'tocart')]" parameterized="true"/>
<element name="ProductAddToCompareByName" type="text" selector="//main//li[.//a[contains(text(), '{{var1}}')]]//a[contains(@class, 'tocompare')]" parameterized="true"/>
<!--<element name="ProductAddToCompareByName" type="text" selector="//main//li[.//a[contains(text(), '{{var1}}')]]//a[contains(@class, 'tocompare')]" parameterized="true"/>-->
<element name="ProductAddToCompareByName" type="text" selector="//*[contains(@class,'product-item-info')][descendant::a[contains(text(), '{{var1}}')]]//a[contains(@class, 'tocompare')]" parameterized="true"/>
<element name="ProductImageByNameAndSrc" type="text" selector="//main//li[.//a[contains(text(), '{{var1}}')]]//img[contains(@src, '{{src}}')]" parameterized="true"/>
</section>
</sections>
30 changes: 29 additions & 1 deletion app/code/Magento/CatalogWidget/Block/Product/ProductsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Widget\Block\BlockInterface;
use Magento\Framework\Url\EncoderInterface;

/**
* Catalog Products List widget block
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implements BlockInterface, IdentityInterface
{
Expand Down Expand Up @@ -94,6 +96,11 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
*/
private $json;

/**
* @var \Magento\Framework\Url\EncoderInterface|null
*/
private $urlEncoder;

/**
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
Expand All @@ -104,6 +111,7 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
* @param \Magento\Widget\Helper\Conditions $conditionsHelper
* @param array $data
* @param Json|null $json
* @param \Magento\Framework\Url\EncoderInterface|null $urlEncoder
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
Expand All @@ -114,7 +122,8 @@ public function __construct(
\Magento\CatalogWidget\Model\Rule $rule,
\Magento\Widget\Helper\Conditions $conditionsHelper,
array $data = [],
Json $json = null
Json $json = null,
EncoderInterface $urlEncoder = null
) {
$this->productCollectionFactory = $productCollectionFactory;
$this->catalogProductVisibility = $catalogProductVisibility;
Expand All @@ -123,6 +132,7 @@ public function __construct(
$this->rule = $rule;
$this->conditionsHelper = $conditionsHelper;
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
$this->urlEncoder = $urlEncoder ?: ObjectManager::getInstance()->get(EncoderInterface::class);
parent::__construct(
$context,
$data
Expand Down Expand Up @@ -153,6 +163,7 @@ protected function _construct()
* Get key pieces for caching block content
*
* @return array
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
*/
public function getCacheKeyInfo()
{
Expand Down Expand Up @@ -230,6 +241,7 @@ protected function _beforeToHtml()
* Prepare and return product collection
*
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
*/
public function createCollection()
{
Expand Down Expand Up @@ -409,6 +421,22 @@ private function getPriceCurrency()
return $this->priceCurrency;
}

/**
* @inheritdoc
*/
public function getAddToCartUrl($product, $additional = [])
{
$requestingPageUrl = $this->getRequest()->getParam('requesting_page_url');

if (!empty($requestingPageUrl)) {
$additional['useUencPlaceholder'] = true;
$url = parent::getAddToCartUrl($product, $additional);
return str_replace('%25uenc%25', $this->urlEncoder->encode($requestingPageUrl), $url);
}

return parent::getAddToCartUrl($product, $additional);
}

/**
* Get widget block name
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
<section name="ProductListWidgetSection">
<element name="AddToCartByName" type="button" selector="//*[contains(@class,'product-item-info')][descendant::a[contains(text(), '{{arg1}}')]]//button[contains(@class,'tocart')]" parameterized="true"/>
<element name="AddToCompareByName" type="button" selector="//*[contains(@class,'product-item-info')][descendant::a[contains(text(), '{{arg1}}')]]//button[contains(@class,'tocompare')]" parameterized="true"/>
</section>
</sections>

0 comments on commit 8c2e178

Please sign in to comment.