-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Description
Preconditions (*)
- Magento 2.3.5 or 2.4-develop (I'm using commmit 1e28f35)
Steps to reproduce (*)
- Look at this piece of code:
magento2/app/code/Magento/Catalog/view/frontend/templates/product/list/items.phtml
Lines 245 to 292 in 1e28f35
<?php if ($_item->isSaleable()):?> <div class="actions-primary"> <?php if (!$_item->getTypeInstance()->isPossibleBuyFromList($_item)):?> <button class="action tocart primary" data-mage-init='{"redirectUrl": {"url": "<?= $block->escapeUrl($block->getAddToCartUrl($_item)) ?>"}}' type="button" title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>"> <span><?= $block->escapeHtml(__('Add to Cart')) ?></span> </button> <?php else :?> <?php /** @var $viewModel PreparePostData */ $viewModel = $block->getViewModel(); $postArray = $viewModel->getPostData( $block->escapeUrl($block->getAddToCartUrl($_item)), ['product' => $_item->getEntityId()] ); $value = $postArray['data'][ActionInterface::PARAM_NAME_URL_ENCODED]; ?> <form data-role="tocart-form" data-product-sku="<?= $block->escapeHtmlAttr($_item->getSku()) ?>" action="<?= $block->escapeUrl($block->getAddToCartUrl($_item)) ?>" method="post"> <input type="hidden" name="product" value="<?= /* @noEscape */ (int)$_item->getEntityId() ?>"> <input type="hidden" name="<?= /* @noEscape */ ActionInterface::PARAM_NAME_URL_ENCODED?>" value="<?= /* @noEscape */ $value ?>"> <?= $block->getBlockHtml('formkey') ?> <button type="submit" title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>" class="action tocart primary"> <span><?= $block->escapeHtml(__('Add to Cart')) ?></span> </button> </form> <?php endif; ?> <?php else:?> <?php if ($_item->getIsSalable()):?> <div class="stock available"> <span><?= $block->escapeHtml(__('In stock')) ?></span> </div> <?php else:?> <div class="stock unavailable"> <span><?= $block->escapeHtml(__('Out of stock')) ?></span> </div> <?php endif; ?> <?php endif; ?> </div> <?php endif; ?> - Remove a bunch of the inner structure until you have the following remaining:
<?php if ($showCart):?>
<?php if ($_item->isSaleable()):?>
<div class="actions-primary">
<?php else:?>
<?php endif; ?>
</div>
<?php endif; ?>
- Notice that the opening
<div>
and closing</div>
aren't in the same block, which can most likely result in incorrect html structure being outputted.
Expected result (*)
Either:
<?php if ($showCart):?>
<?php if ($_item->isSaleable()):?>
<div class="actions-primary">
</div>
<?php else:?>
<?php endif; ?>
<?php endif; ?>
Or:
<?php if ($showCart):?>
<div class="actions-primary">
<?php if ($_item->isSaleable()):?>
<?php else:?>
<?php endif; ?>
</div>
<?php endif; ?>
If I have to guess, it should probably take the second form, as that's the structure what other templates are following as well:
magento2/app/code/Magento/Catalog/view/frontend/templates/product/compare/list.phtml
Lines 70 to 87 in 1e28f35
<div class="actions-primary"> <?php if ($item->isSaleable()) :?> <form data-role="tocart-form" action="<?= $block->escapeUrl($this->helper(Magento\Catalog\Helper\Product\Compare::class)->getAddToCartUrl($item)) ?>" method="post"> <?= $block->getBlockHtml('formkey') ?> <button type="submit" class="action tocart primary"> <span><?= $block->escapeHtml(__('Add to Cart')) ?></span> </button> </form> <?php else :?> <?php if ($item->getIsSalable()) :?> <div class="stock available"><span><?= $block->escapeHtml(__('In stock')) ?></span></div> <?php else :?> <div class="stock unavailable"><span><?= $block->escapeHtml(__('Out of stock')) ?></span></div> <?php endif; ?> <?php endif; ?> </div> magento2/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml
Lines 83 to 109 in 1e28f35
<div class="actions-primary"<?= strpos($pos, $viewMode . '-primary') ? $escaper->escapeHtmlAttr($position) : '' ?>> <?php if ($_product->isSaleable()) :?> <?php $postParams = $block->getAddToCartPostParams($_product); ?> <form data-role="tocart-form" data-product-sku="<?= $escaper->escapeHtml($_product->getSku()) ?>" action="<?= $escaper->escapeUrl($postParams['action']) ?>" method="post"> <input type="hidden" name="product" value="<?= /* @noEscape */ $postParams['data']['product'] ?>"> <input type="hidden" name="<?= /* @noEscape */ Action::PARAM_NAME_URL_ENCODED ?>" value="<?= /* @noEscape */ $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>"> <?= $block->getBlockHtml('formkey') ?> <button type="submit" title="<?= $escaper->escapeHtmlAttr(__('Add to Cart')) ?>" class="action tocart primary"> <span><?= $escaper->escapeHtml(__('Add to Cart')) ?></span> </button> </form> <?php else :?> <?php if ($_product->isAvailable()) :?> <div class="stock available"><span><?= $escaper->escapeHtml(__('In stock')) ?></span></div> <?php else :?> <div class="stock unavailable"><span><?= $escaper->escapeHtml(__('Out of stock')) ?></span></div> <?php endif; ?> <?php endif; ?> </div> - ...
Actual result (*)
- See points 2 & 3 of steps to reproduce
Discussion
This seems to have been introduced in Magento 2.3.5 and 2.4-develop by MC-30989, the <div class="actions-primary">
was moved inside the <?php if ($_item->isSaleable()):?>
while it should probably have been left outside of it.