1

I have added new column of SR NO in cart table. But <tbody> tag generating dynamically. I want to add sr no for each row. but When I am trying it is not working and showing only 1 in each row.

Can you please let me know?

Here my code of default.phtml"

<?php   
 _item = $block->getItem();
    $helper1 = $this->helper('\Magento\Checkout\Helper\Cart');
    $totalItem = $helper1->getItemsCount();
    $product = $_item->getProduct();
    $i=1;
    $isVisibleProduct = $product->isVisibleInSiteVisibility();
    /** @var \Magento\Msrp\Helper\Data $helper */
    $helper = $this->helper('Magento\Msrp\Helper\Data');
    $canApplyMsrp = $helper->isShowBeforeOrderConfirm($product) && $helper->isMinimalPriceLessMsrp($product);


    ?>
    <?php
      // $i=1;

    //while($i<=$totalItem){

      ?>





    <tbody class="cart item">
        <tr class="item-info">
            <td class="col srn">

                <span><?php echo  $i . "<br>";

                      $i++;
                 ?></span> 

            </td>

            <td data-th="<?= $block->escapeHtml(__('Item')) ?>" class="col item">
                <?php if ($block->hasProductUrl()):?>
                    <a href="<?= /* @escapeNotVerified */ $block->getProductUrl() ?>"
                       title="<?= $block->escapeHtml($block->getProductName()) ?>"
                       tabindex="-1"
                       class="product-item-photo">
                <?php else:?>
                    <span class="product-item-photo">
                <?php endif;?>
                <?= $block->getImage($block->getProductForThumbnail(), 'cart_page_product_thumbnail')->toHtml() ?>
                <?php if ($block->hasProductUrl()):?>
                    </a>
                <?php else: ?>
                    </span>
                <?php endif; ?>
                <div class="product-item-details">
                    <strong class="product-item-name">
                        <?php if ($block->hasProductUrl()):?>
                            <a href="<?= /* @escapeNotVerified */ $block->getProductUrl() ?>"><?= $block->escapeHtml($block->getProductName()) ?></a>
                        <?php else: ?>
                            <?= $block->escapeHtml($block->getProductName()) ?>
                        <?php endif; ?>
                    </strong>
                    <?php if ($_options = $block->getOptionList()):?>
                        <dl class="item-options">
                            <?php foreach ($_options as $_option) : ?>
                                <?php $_formatedOptionValue = $block->getFormatedOptionValue($_option) ?>
                                <dt><?= $block->escapeHtml($_option['label']) ?></dt>
                                <dd>
                                    <?php if (isset($_formatedOptionValue['full_view'])): ?>
                                        <?= /* @escapeNotVerified */ $_formatedOptionValue['full_view'] ?>
                                    <?php else: ?>
                                        <?= $block->escapeHtml($_formatedOptionValue['value'], ['span']) ?>
                                    <?php endif; ?>
                                </dd>
                            <?php endforeach; ?>
                        </dl>
                    <?php endif;?>
                    <?php if ($messages = $block->getMessages()): ?>
                        <?php foreach ($messages as $message): ?>
                            <div class="cart item message <?= /* @escapeNotVerified */ $message['type'] ?>"><div><?= $block->escapeHtml($message['text']) ?></div></div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                    <?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>
                    <?php if ($addInfoBlock): ?>
                        <?= $addInfoBlock->setItem($_item)->toHtml() ?>
                    <?php endif;?>
                </div>
            </td>
            <td class="col size">
                 <span><?php if ($_options = $block->getOptionList()):?>
                        <dl class="item-options">
                            <?php foreach ($_options as $_option) : ?>
                           <span><?php if($block->escapeHtml($_option['label']) == 'Product Size') { ?></span>
                           <dd>
                            <?php echo $_option['value']; ?> 
                           </dd> 
                           <?php } ?>   
                            <?php endforeach; ?>
                        </dl>
                    <?php endif;?></span>
            </td> 
            <td class="col color">
                 <span><?php if ($_options = $block->getOptionList()):?>
                        <dl class="item-options">
                            <?php foreach ($_options as $_option) : ?>
                           <span><?php if($block->escapeHtml($_option['label']) == 'Color') { ?></span>
                           <dd>
                            <?php echo $_option['value']; ?> 
                           </dd> 
                           <?php } ?>   
                            <?php endforeach; ?>
                        </dl>
                    <?php endif;?></span>
            </td>   
            <?php if ($canApplyMsrp): ?>
                <td class="col msrp" data-th="<?= $block->escapeHtml(__('Price')) ?>">
                    <span class="pricing msrp">
                        <span class="msrp notice"><?= /* @escapeNotVerified */ __('See price before order confirmation.') ?></span>
                        <?php $helpLinkId = 'cart-msrp-help-' . $_item->getId(); ?>
                        <a href="#" class="action help map" id="<?= /* @escapeNotVerified */ ($helpLinkId) ?>" data-mage-init='{"addToCart":{"helpLinkId": "#<?= /* @escapeNotVerified */ $helpLinkId ?>","productName": "<?= /* @escapeNotVerified */ $product->getName() ?>","showAddToCart": false}}'>
                            <span><?= /* @escapeNotVerified */ __("What's this?") ?></span>
                        </a>
                    </span>
                </td>
            <?php else: ?>
                <td class="col price" data-th="<?= $block->escapeHtml(__('Price')) ?>">
                    <?= $block->getUnitPriceHtml($_item) ?>
                </td>
            <?php endif; ?>
            <td class="col qty" data-th="<?= $block->escapeHtml(__('Qty')) ?>">
                <div class="field qty">
                    <label class="label" for="cart-<?= /* @escapeNotVerified */ $_item->getId() ?>-qty">
                        <span><?= /* @escapeNotVerified */ __('Qty') ?></span>
                    </label>
                    <div class="control qty">
                        <button type="button"   id="<?= /* @escapeNotVerified */ $_item->getId() ?>-dec"  class="decreaseQty"></button>
                       <input id="cart-<?= /* @escapeNotVerified */ $_item->getId() ?>-qty"
                           name="cart[<?= /* @escapeNotVerified */ $_item->getId() ?>][qty]"
                           data-cart-item-id="<?= /* @escapeNotVerified */ $_item->getSku() ?>"
                           value="<?= /* @escapeNotVerified */ $block->getQty() ?>"
                           type="number"
                           size="4"
                           title="<?= $block->escapeHtml(__('Qty')) ?>"
                           class="input-text qty"
                           data-validate="{required:true,'validate-greater-than-zero':true}"
                           data-role="cart-item-qty"/>                                                     

                          <button type="button"  id="<?= /* @escapeNotVerified */ $_item->getId() ?>-upt" class="increaseQty"></button>

                     <!--   <div class="qty_control">
                                <button type="button"   id="<?= /* @escapeNotVerified */ $_item->getId() ?>-dec"  class="decreaseQty"></button>
                                <button type="button"  id="<?= /* @escapeNotVerified */ $_item->getId() ?>-upt" class="increaseQty"></button>
                          </div> -->
                    </div>
                </div>
            </td>

            <td class="col subtotal" data-th="<?= $block->escapeHtml(__('Subtotal')) ?>">
                <?php if ($canApplyMsrp): ?>
                    <span class="cart msrp subtotal">--</span>
                <?php else: ?>
                    <?= $block->getRowTotalHtml($_item) ?>
                <?php endif; ?>
            </td>
            <?php //} ?>
        </tr>
        <tr class="item-actions adde">
            <td colspan="4">
                <div class="actions-toolbar">
                    <?= /* @escapeNotVerified */ $block->getActions($_item) ?>
                </div>
            </td>
        </tr>
    </tbody>

1 Answer 1

0

Try below solution.

Magento_Checkout/templates/cart/form.phtml

    <?php $i = 1;?>
    <?php foreach ($block->getItems() as $_item): ?>
          <?php $_item->setCartItemNo($i); ?>
          <?= $block->getItemHtml($_item) ?>
          <?php $i++; ?>
   <?php endforeach ?>

Magento_Checkout/templates/cart/item/default.phtml

$cartNo = $_item->getCartItemNo();

<td class="col no" data-th="<?= $block->escapeHtml(__('No.')) ?>"><?= $cartNo ?></td>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.