Skip to content

[2.3.5] Incorrect html structure after MC-30989 #27920

Closed
@hostep

Description

@hostep

Preconditions (*)

  1. Magento 2.3.5 or 2.4-develop (I'm using commmit 1e28f35)

Steps to reproduce (*)

  1. Look at this piece of code:
    <?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; ?>
  2. 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; ?>
  1. 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:

  • <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>
  • <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 (*)

  1. 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.

Metadata

Metadata

Assignees

Labels

Fixed in 2.4.xThe issue has been fixed in 2.4-develop branchIssue: Format is validGate 1 Passed. Automatic verification of issue format passedReported on 2.3.5Indicates original Magento version for the Issue report.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions