Skip to content

Commit 5d8a3ac

Browse files
committed
Product's price is not correct inside wishlist
1 parent 05e9aaa commit 5d8a3ac

File tree

9 files changed

+209
-113
lines changed

9 files changed

+209
-113
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Helper\Catalog\Product;
7+
8+
use Magento\Catalog\Helper\Product\Configuration\ConfigurationInterface;
9+
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
10+
use Magento\Framework\App\Helper\AbstractHelper;
11+
12+
/**
13+
* Helper for fetching properties by product configuration item
14+
*/
15+
class Configuration extends AbstractHelper implements ConfigurationInterface
16+
{
17+
/**
18+
* Core data
19+
*
20+
* @var \Magento\Framework\Pricing\Helper\Data
21+
*/
22+
private $pricingHelper;
23+
24+
/**
25+
* Catalog product configuration
26+
*
27+
* @var \Magento\Catalog\Helper\Product\Configuration
28+
*/
29+
private $productConfiguration;
30+
31+
/**
32+
* @var \Magento\Framework\Escaper
33+
*/
34+
private $escaper;
35+
36+
/**
37+
* Serializer interface instance.
38+
*
39+
* @var \Magento\Framework\Serialize\Serializer\Json
40+
*/
41+
private $serializer;
42+
43+
/**
44+
* @param \Magento\Framework\App\Helper\Context $context
45+
* @param \Magento\Catalog\Helper\Product\Configuration $productConfiguration
46+
* @param \Magento\Framework\Pricing\Helper\Data $pricingHelper
47+
* @param \Magento\Framework\Escaper $escaper
48+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
49+
*/
50+
public function __construct(
51+
\Magento\Framework\App\Helper\Context $context,
52+
\Magento\Catalog\Helper\Product\Configuration $productConfiguration,
53+
\Magento\Framework\Pricing\Helper\Data $pricingHelper,
54+
\Magento\Framework\Escaper $escaper,
55+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
56+
) {
57+
$this->productConfiguration = $productConfiguration;
58+
$this->pricingHelper = $pricingHelper;
59+
$this->escaper = $escaper;
60+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
61+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
62+
parent::__construct($context);
63+
}
64+
65+
/**
66+
* Custom options with price value
67+
*
68+
* @param ItemInterface $item
69+
* @return array|bool|float|int|string|null
70+
* @throws \Magento\Framework\Exception\LocalizedException
71+
*
72+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
73+
*/
74+
public function getCustomOptions(ItemInterface $item) //phpcs:ignore Generic.Metrics.NestingLevel
75+
{
76+
$product = $item->getProduct();
77+
$options = [];
78+
$optionIds = $item->getOptionByCode('option_ids');
79+
if ($optionIds && $optionIds->getValue()) {
80+
$options = [];
81+
foreach (explode(',', $optionIds->getValue()) as $optionId) {
82+
$option = $product->getOptionById($optionId);
83+
if ($option) {
84+
$itemOption = $item->getOptionByCode('option_' . $option->getId());
85+
/** @var $group \Magento\Catalog\Model\Product\Option\Type\DefaultType */
86+
$group = $option->groupFactory($option->getType())
87+
->setOption($option)
88+
->setConfigurationItem($item)
89+
->setConfigurationItemOption($itemOption);
90+
$priceInfo = $product->getPriceInfo();
91+
$basePrice = $priceInfo->getPrice('final_price')->getAmount()->getValue();
92+
93+
if ('file' == $option->getType()) {
94+
$downloadParams = $item->getFileDownloadParams();
95+
if ($downloadParams) {
96+
$url = $downloadParams->getUrl();
97+
if ($url) {
98+
$group->setCustomOptionDownloadUrl($url);
99+
}
100+
$urlParams = $downloadParams->getUrlParams();
101+
if ($urlParams) {
102+
$group->setCustomOptionUrlParams($urlParams);
103+
}
104+
}
105+
}
106+
107+
$optionData = [
108+
'label' => $option->getTitle(),
109+
'value' => $group->getFormattedOptionValue($itemOption->getValue()),
110+
'print_value' => $group->getPrintableOptionValue($itemOption->getValue()),
111+
'option_id' => $option->getId(),
112+
'option_type' => $option->getType(),
113+
'custom_view' => $group->isCustomizedView(),
114+
];
115+
116+
if ($group->getOptionPrice($itemOption->getValue(), $basePrice) !== null) {
117+
$optionData['price'] = $this->pricingHelper->currency(
118+
$group->getOptionPrice($itemOption->getValue(), $basePrice)
119+
);
120+
$optionData['has_html'] = true;
121+
}
122+
123+
if ($optionData) {
124+
$options[] = $optionData;
125+
}
126+
127+
}
128+
}
129+
}
130+
131+
$addOptions = $item->getOptionByCode('additional_options');
132+
if ($addOptions) {
133+
$options = array_merge($options, $this->serializer->unserialize($addOptions->getValue()));
134+
}
135+
136+
return $options;
137+
}
138+
139+
/**
140+
* Retrieves product options list
141+
*
142+
* @param ItemInterface $item
143+
* @return array
144+
*/
145+
public function getOptions(ItemInterface $item)
146+
{
147+
$product = $item->getProduct();
148+
$attributes = $product->getTypeInstance()->getSelectedAttributesInfo($product);
149+
return array_merge($attributes, $this->getCustomOptions($item));
150+
}
151+
}

app/code/Magento/ConfigurableProduct/Helper/Product/Configuration/Plugin.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
*/
77
namespace Magento\ConfigurableProduct\Helper\Product\Configuration;
88

9+
/**
10+
* @deprecated
11+
* @see Magento\ConfigurableProduct\Helper\Catalog\Product\Configuration
12+
*/
913
class Plugin
1014
{
1115
/**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Observer;
7+
8+
use Magento\ConfigurableProduct\Helper\Catalog\Product\Configuration;
9+
use Magento\Framework\Event\Observer;
10+
use Magento\Framework\Event\ObserverInterface;
11+
12+
/**
13+
* Initiates render options
14+
*/
15+
class InitOptionRendererObserver implements ObserverInterface
16+
{
17+
/**
18+
* Initialize product options renderer with configurable specific params
19+
*
20+
* @param Observer $observer
21+
* @return $this
22+
*/
23+
public function execute(Observer $observer)
24+
{
25+
$block = $observer->getBlock();
26+
$block->addOptionsRenderCfg('configurable', Configuration::class);
27+
return $this;
28+
}
29+
}

app/code/Magento/ConfigurableProduct/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<plugin name="configurable_product" type="Magento\ConfigurableProduct\Model\Order\Admin\Item\Plugin\Configurable" sortOrder="50" />
3131
</type>
3232
<type name="Magento\Catalog\Helper\Product\Configuration">
33-
<plugin name="configurable_product" type="Magento\ConfigurableProduct\Helper\Product\Configuration\Plugin" sortOrder="50" />
33+
<plugin name="configurable_product" type="Magento\ConfigurableProduct\Helper\Product\Configuration\Plugin" sortOrder="50" disabled="true"/>
3434
</type>
3535
<type name="Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend">
3636
<plugin name="ConfigurableProduct::skipValidation" type="Magento\ConfigurableProduct\Plugin\Model\Attribute\Backend\AttributeValidation"/>

app/code/Magento/ConfigurableProduct/etc/frontend/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
99
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
10+
<type name="Magento\Catalog\Helper\Product\ConfigurationPool">
11+
<arguments>
12+
<argument name="instancesByType" xsi:type="array">
13+
<item name="configurable" xsi:type="object">Magento\ConfigurableProduct\Helper\Catalog\Product\Configuration</item>
14+
</argument>
15+
</arguments>
16+
</type>
1017
<type name="Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface">
1118
<plugin name="Magento_ConfigurableProduct_Plugin_Model_ResourceModel_Attribute_InStockOptionSelectBuilder" type="Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Attribute\InStockOptionSelectBuilder"/>
1219
<plugin name="option_select_website_filter" type="Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Attribute\ScopedOptionSelectBuilder"/>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
9+
<event name="product_option_renderer_init">
10+
<observer name="configurable_observer" instance="Magento\ConfigurableProduct\Observer\InitOptionRendererObserver"/>
11+
</event>
12+
</config>

app/code/Magento/Wishlist/Block/Customer/Wishlist/Item/Options.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public function getConfiguredOptions()
118118
}
119119
}
120120
$options[$index]['value'] = $option['value'];
121+
if (array_key_exists('price', $option)) {
122+
$options[$index]['value'] = $option['value'] .' '. $option['price'];
123+
}
121124
}
122125
}
123126

app/code/Magento/Wishlist/Pricing/ConfiguredPrice/ConfigurableProduct.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ConfigurableProduct extends AbstractPrice
1717
/**
1818
* Price type final.
1919
*/
20-
public const PRICE_CODE = 'final_price';
20+
const PRICE_CODE = 'final_price';
2121

2222
/**
2323
* @var ItemInterface
@@ -57,17 +57,7 @@ public function getConfiguredRegularAmount(): \Magento\Framework\Pricing\Amount\
5757
*/
5858
public function getValue()
5959
{
60-
$product = $this->getProduct();
61-
$price = $product->getPriceInfo()->getPrice(self::PRICE_CODE)->getValue();
62-
63-
if ($product->getProductOptionsCollection()) {
64-
foreach ($product->getProductOptionsCollection() as $configurationOptionsVal) {
65-
$configurableOptionsData = $configurationOptionsVal->getValues();
66-
foreach ($configurableOptionsData as $configurableOptionData) {
67-
$price += $configurableOptionData->getPrice();
68-
}
69-
}
70-
}
60+
$price = $this->getProduct()->getPriceInfo()->getPrice(self::PRICE_CODE)->getValue();
7161

7262
return max(0, $price);
7363
}

0 commit comments

Comments
 (0)