Skip to content

Commit 8732c99

Browse files
committed
MAGETWO-90367: Unassigned Attribute Values Render as N/A in Product Comparison block
1 parent 9ba75c8 commit 8732c99

File tree

15 files changed

+380
-53
lines changed

15 files changed

+380
-53
lines changed

app/code/Magento/Catalog/Block/Product/Compare/ListCompare.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,23 @@ public function getProductAttributeValue($product, $attribute)
214214
return (string)$value == '' ? __('No') : $value;
215215
}
216216

217+
/**
218+
* Check if any of the products has a value set for the attribute.
219+
*
220+
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute
221+
* @return bool
222+
*/
223+
public function hasAttributeValueForProducts(\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute) : bool
224+
{
225+
foreach ($this->getItems() as $item) {
226+
if ($item->hasData($attribute->getAttributeCode())) {
227+
return true;
228+
}
229+
}
230+
231+
return false;
232+
}
233+
217234
/**
218235
* Retrieve Print URL
219236
*

app/code/Magento/Catalog/view/frontend/templates/product/compare/list.phtml

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
/* @var $block \Magento\Catalog\Block\Product\Compare\ListCompare */
1010
?>
11-
<?php $_total = $block->getItems()->getSize() ?>
12-
<?php if ($_total): ?>
11+
<?php $total = $block->getItems()->getSize() ?>
12+
<?php if ($total): ?>
1313
<a href="#" class="action print hidden-print" title="<?= /* @escapeNotVerified */ __('Print This Page') ?>">
1414
<span><?= /* @escapeNotVerified */ __('Print This Page') ?></span>
1515
</a>
@@ -24,14 +24,14 @@
2424
<caption class="table-caption"><?= /* @escapeNotVerified */ __('Compare Products') ?></caption>
2525
<thead>
2626
<tr>
27-
<?php $_i = 0 ?>
28-
<?php foreach ($block->getItems() as $_item): ?>
29-
<?php if ($_i++ == 0): ?>
27+
<?php $index = 0 ?>
28+
<?php foreach ($block->getItems() as $item): ?>
29+
<?php if ($index++ == 0): ?>
3030
<th scope="row" class="cell label remove"><span><?= /* @escapeNotVerified */ __('Remove Product') ?></span></th>
3131
<?php endif; ?>
3232
<td class="cell remove product hidden-print">
3333
<?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');?>
34-
<a href="#" data-post='<?= /* @escapeNotVerified */ $compareHelper->getPostDataRemove($_item) ?>'
34+
<a href="#" data-post='<?= /* @escapeNotVerified */ $compareHelper->getPostDataRemove($item) ?>'
3535
class="action delete" title="<?= /* @escapeNotVerified */ __('Remove Product') ?>">
3636
<span><?= /* @escapeNotVerified */ __('Remove Product') ?></span>
3737
</a>
@@ -41,35 +41,35 @@
4141
</thead>
4242
<tbody>
4343
<tr>
44-
<?php $_i = 0; ?>
45-
<?php $_helper = $this->helper('Magento\Catalog\Helper\Output'); ?>
46-
<?php /** @var $_item \Magento\Catalog\Model\Product */ ?>
47-
<?php foreach ($block->getItems() as $_item): ?>
48-
<?php if ($_i++ == 0): ?>
44+
<?php $index = 0; ?>
45+
<?php $helper = $this->helper('Magento\Catalog\Helper\Output'); ?>
46+
<?php /** @var $item \Magento\Catalog\Model\Product */ ?>
47+
<?php foreach ($block->getItems() as $item): ?>
48+
<?php if ($index++ == 0): ?>
4949
<th scope="row" class="cell label product"><span><?= /* @escapeNotVerified */ __('Product') ?></span></th>
5050
<?php endif; ?>
5151
<td data-th="<?= $block->escapeHtml(__('Product')) ?>" class="cell product info">
52-
<a class="product-item-photo" href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>" title="<?= /* @escapeNotVerified */ $block->stripTags($_item->getName(), null, true) ?>">
53-
<?= $block->getImage($_item, 'product_comparison_list')->toHtml() ?>
52+
<a class="product-item-photo" href="<?= /* @escapeNotVerified */ $block->getProductUrl($item) ?>" title="<?= /* @escapeNotVerified */ $block->stripTags($item->getName(), null, true) ?>">
53+
<?= $block->getImage($item, 'product_comparison_list')->toHtml() ?>
5454
</a>
5555
<strong class="product-item-name">
56-
<a href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>" title="<?= /* @escapeNotVerified */ $block->stripTags($_item->getName(), null, true) ?>">
57-
<?= /* @escapeNotVerified */ $_helper->productAttribute($_item, $_item->getName(), 'name') ?>
56+
<a href="<?= /* @escapeNotVerified */ $block->getProductUrl($item) ?>" title="<?= /* @escapeNotVerified */ $block->stripTags($item->getName(), null, true) ?>">
57+
<?= /* @escapeNotVerified */ $helper->productAttribute($item, $item->getName(), 'name') ?>
5858
</a>
5959
</strong>
60-
<?= $block->getReviewsSummaryHtml($_item, 'short') ?>
61-
<?= /* @escapeNotVerified */ $block->getProductPrice($_item, '-compare-list-top') ?>
60+
<?= $block->getReviewsSummaryHtml($item, 'short') ?>
61+
<?= /* @escapeNotVerified */ $block->getProductPrice($item, '-compare-list-top') ?>
6262
<div class="product-item-actions hidden-print">
6363
<div class="actions-primary">
64-
<?php if ($_item->isSaleable()): ?>
65-
<form data-role="tocart-form" action="<?= /* @escapeNotVerified */ $this->helper('Magento\Catalog\Helper\Product\Compare')->getAddToCartUrl($_item) ?>" method="post">
64+
<?php if ($item->isSaleable()): ?>
65+
<form data-role="tocart-form" action="<?= /* @escapeNotVerified */ $this->helper('Magento\Catalog\Helper\Product\Compare')->getAddToCartUrl($item) ?>" method="post">
6666
<?= $block->getBlockHtml('formkey') ?>
6767
<button type="submit" class="action tocart primary">
6868
<span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
6969
</button>
7070
</form>
7171
<?php else: ?>
72-
<?php if ($_item->getIsSalable()): ?>
72+
<?php if ($item->getIsSalable()): ?>
7373
<div class="stock available"><span><?= /* @escapeNotVerified */ __('In stock') ?></span></div>
7474
<?php else: ?>
7575
<div class="stock unavailable"><span><?= /* @escapeNotVerified */ __('Out of stock') ?></span></div>
@@ -78,7 +78,7 @@
7878
</div>
7979
<?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()) : ?>
8080
<div class="secondary-addto-links actions-secondary" data-role="add-to-links">
81-
<a href="#" data-post='<?= /* @escapeNotVerified */ $block->getAddToWishlistParams($_item) ?>' class="action towishlist" data-action="add-to-wishlist">
81+
<a href="#" data-post='<?= /* @escapeNotVerified */ $block->getAddToWishlistParams($item) ?>' class="action towishlist" data-action="add-to-wishlist">
8282
<span><?= /* @escapeNotVerified */ __('Add to Wish List') ?></span>
8383
</a>
8484
</div>
@@ -89,39 +89,41 @@
8989
</tr>
9090
</tbody>
9191
<tbody>
92-
<?php foreach ($block->getAttributes() as $_attribute): ?>
93-
<tr>
94-
<?php $_i = 0 ?>
95-
<?php foreach ($block->getItems() as $_item): ?>
96-
<?php if ($_i++ == 0): ?>
97-
<th scope="row" class="cell label">
98-
<span class="attribute label">
99-
<?= $block->escapeHtml($_attribute->getStoreLabel() ? $_attribute->getStoreLabel() : __($_attribute->getFrontendLabel())) ?>
100-
</span>
101-
</th>
102-
<?php endif; ?>
103-
<td class="cell product attribute">
104-
<div class="attribute value">
105-
<?php switch ($_attribute->getAttributeCode()) {
106-
case "price": ?>
107-
<?php
108-
/* @escapeNotVerified */ echo $block->getProductPrice(
109-
$_item,
110-
'-compare-list-' . $_attribute->getCode()
111-
)
112-
?>
113-
<?php break;
114-
case "small_image": ?>
115-
<?php $block->getImage($_item, 'product_small_image')->toHtml(); ?>
92+
<?php foreach ($block->getAttributes() as $attribute): ?>
93+
<?php $index = 0; ?>
94+
<?php if ($block->hasAttributeValueForProducts($attribute)): ?>
95+
<tr>
96+
<?php foreach ($block->getItems() as $item): ?>
97+
<?php if ($index++ == 0): ?>
98+
<th scope="row" class="cell label">
99+
<span class="attribute label">
100+
<?= $block->escapeHtml($attribute->getStoreLabel() ? $attribute->getStoreLabel() : __($attribute->getFrontendLabel())) ?>
101+
</span>
102+
</th>
103+
<?php endif; ?>
104+
<td class="cell product attribute">
105+
<div class="attribute value">
106+
<?php switch ($attribute->getAttributeCode()) {
107+
case "price": ?>
108+
<?php
109+
/* @escapeNotVerified */ echo $block->getProductPrice(
110+
$item,
111+
'-compare-list-' . $attribute->getCode()
112+
)
113+
?>
114+
<?php break;
115+
case "small_image": ?>
116+
<?php $block->getImage($item, 'product_small_image')->toHtml(); ?>
117+
<?php break;
118+
default: ?>
119+
<?= /* @escapeNotVerified */ $helper->productAttribute($item, $block->getProductAttributeValue($item, $attribute), $attribute->getAttributeCode()) ?>
116120
<?php break;
117-
default: ?>
118-
<?= /* @escapeNotVerified */ $_helper->productAttribute($_item, $block->getProductAttributeValue($_item, $_attribute), $_attribute->getAttributeCode()) ?>
119-
<?php break;
120-
} ?>
121-
</div>
122-
</td>
123-
<?php endforeach; ?>
124-
</tr>
121+
} ?>
122+
</div>
123+
</td>
124+
<?php endforeach; ?>
125+
</tr>
126+
<?php endif; ?>
125127
<?php endforeach; ?>
126128
</tbody>
127129
</table>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Test/etc/actionGroupSchema.xsd">
11+
<!-- Add Product to Compare from the category page and check message -->
12+
<actionGroup name="StorefrontAddCategoryProductToCompareActionGroup">
13+
<arguments>
14+
<argument name="productVar"/>
15+
</arguments>
16+
<moveMouseOver selector="{{StorefrontCategoryProductSection.ProductInfoByName(productVar.name)}}" stepKey="moveMouseOverProduct" />
17+
<click selector="{{StorefrontCategoryProductSection.ProductAddToCompareByName(productVar.name)}}" stepKey="clickAddProductToCompare"/>
18+
<waitForElement selector="{{StorefrontMessagesSection.success}}" time="30" stepKey="waitForAddCategoryProductToCompareSuccessMessage"/>
19+
<see selector="{{StorefrontMessagesSection.success}}" userInput="You added product {{productVar.name}} to the comparison list." stepKey="assertAddCategoryProductToCompareSuccessMessage"/>
20+
</actionGroup>
21+
22+
<!-- Add Product to Compare from the product page and check message -->
23+
<actionGroup name="StorefrontAddProductToCompareActionGroup">
24+
<arguments>
25+
<argument name="productVar"/>
26+
</arguments>
27+
<click selector="{{StorefrontProductInfoMainSection.productAddToCompare}}" stepKey="clickAddToCompare" />
28+
<waitForElement selector="{{StorefrontMessagesSection.success}}" time="30" stepKey="waitForAddProductToCompareSuccessMessage"/>
29+
<see selector="{{StorefrontMessagesSection.success}}" userInput="You added product {{productVar.name}} to the comparison list." stepKey="assertAddProductToCompareSuccessMessage"/>
30+
</actionGroup>
31+
32+
<!-- Check the product in compare sidebar -->
33+
<actionGroup name="StorefrontCheckCompareSidebarProductActionGroup">
34+
<arguments>
35+
<argument name="productVar"/>
36+
</arguments>
37+
<waitForElement selector="{{StorefrontComparisonSidebarSection.ProductTitleByName(productVar.name)}}" stepKey="waitForProduct"/>
38+
</actionGroup>
39+
40+
<!-- Open and check comparison page -->
41+
<actionGroup name="StorefrontOpenAndCheckComparisionActionGroup">
42+
<click selector="{{StorefrontComparisonSidebarSection.Compare}}" stepKey="clickCompare"/>
43+
<waitForLoadingMaskToDisappear stepKey="waitForComparePageloaded" />
44+
<seeInCurrentUrl url="{{StorefrontProductComparePage.url}}" stepKey="checkUrl"/>
45+
<seeInTitle userInput="Products Comparison List" stepKey="assertPageNameInTitle"/>
46+
<see userInput="Compare Products" selector="{{StorefrontProductCompareMainSection.PageName}}" stepKey="assertPageName"/>
47+
</actionGroup>
48+
49+
<!-- Check the simple product in comparison page -->
50+
<actionGroup name="StorefrontCheckCompareSimpleProductActionGroup">
51+
<arguments>
52+
<argument name="productVar"/>
53+
</arguments>
54+
<seeElement selector="{{StorefrontProductCompareMainSection.ProductLinkByName(productVar.name)}}" stepKey="assertProductName"/>
55+
<see userInput="${{productVar.price}}.00" selector="{{StorefrontProductCompareMainSection.ProductPriceByName(productVar.name)}}" stepKey="assertProductPrice1"/>
56+
<see userInput="{{productVar.sku}}" selector="{{StorefrontProductCompareMainSection.ProductAttributeByCodeAndProductName('SKU', productVar.name)}}" stepKey="assertProductPrice2"/>
57+
<seeElement selector="{{StorefrontProductCompareMainSection.ProductAddToCartByName(productVar.name)}}" stepKey="assertProductAddToCart"/>
58+
</actionGroup>
59+
60+
<!-- Clear the compare list -->
61+
<actionGroup name="StorefrontClearCompareActionGroup">
62+
<waitForElementVisible selector="{{StorefrontComparisonSidebarSection.ClearAll}}" time="30" stepKey="waitForClearAll"/>
63+
<click selector="{{StorefrontComparisonSidebarSection.ClearAll}}" stepKey="clickClearAll"/>
64+
<waitForElementVisible selector="{{ModalConfirmationSection.OkButton}}" time="30" stepKey="waitForClearOk"/>
65+
<scrollTo selector="{{ModalConfirmationSection.OkButton}}" stepKey="scrollToClearOk"/>
66+
<click selector="{{ModalConfirmationSection.OkButton}}" stepKey="clickClearOk"/>
67+
<waitForElement selector="{{StorefrontMessagesSection.message('You cleared the comparison list.')}}" time="30" stepKey="AssertMessageCleared"/>
68+
<waitForElement selector="{{StorefrontComparisonSidebarSection.NoItemsMessage}}" time="30" stepKey="assertNoItems"/>
69+
</actionGroup>
70+
</actionGroups>

dev/tests/acceptance/tests/functional/Magento/FunctionalTest/Catalog/Data/CategoryData.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
<data key="name_lwr" unique="suffix">simplecategory</data>
1414
<data key="is_active">true</data>
1515
</entity>
16+
<entity name="ApiCategory" type="category">
17+
<data key="name" unique="suffix">ApiCategory</data>
18+
<data key="is_active">true</data>
19+
</entity>
1620
<entity name="SimpleSubCategory" type="category">
1721
<data key="name" unique="suffix">SimpleSubCategory</data>
1822
<data key="name_lwr" unique="suffix">simplesubcategory</data>

dev/tests/acceptance/tests/functional/Magento/FunctionalTest/Catalog/Data/ProductAttributeData.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,25 @@
3232
<data key="used_for_sort_by">true</data>
3333
<requiredEntity type="FrontendLabel">ProductAttributeFrontendLabel</requiredEntity>
3434
</entity>
35+
<entity name="productAttributeWithDropdownTwoOptions" type="ProductAttribute">
36+
<data key="attribute_code">testattribute</data>
37+
<data key="frontend_input">select</data>
38+
<data key="scope">global</data>
39+
<data key="is_required">false</data>
40+
<data key="is_unique">false</data>
41+
<data key="is_searchable">true</data>
42+
<data key="is_visible">true</data>
43+
<data key="is_visible_in_advanced_search">true</data>
44+
<data key="is_visible_on_front">true</data>
45+
<data key="is_filterable">true</data>
46+
<data key="is_filterable_in_search">true</data>
47+
<data key="used_in_product_listing">true</data>
48+
<data key="is_used_for_promo_rules">true</data>
49+
<data key="is_comparable">true</data>
50+
<data key="is_used_in_grid">true</data>
51+
<data key="is_visible_in_grid">true</data>
52+
<data key="is_filterable_in_grid">true</data>
53+
<data key="used_for_sort_by">true</data>
54+
<requiredEntity type="FrontendLabel">ProductAttributeFrontendLabel</requiredEntity>
55+
</entity>
3556
</entities>

0 commit comments

Comments
 (0)