2

I am reworking how the grouped products are shown in M2. I have the following which shows the child products in a grid with pictures just like in a category. I am stuck as to how I would include an add to cart button on each item. Can anyone help? Thanks in advance.

JJ

<div class="grouped products wrapper grid products-grid">
<h2>Individual Products &darr;</h2>
    <ol class="products list items product-items">
        <?php if ($_hasAssociatedProducts): ?>
        <?php foreach ($_associatedProducts as $_item): ?>
        <li class="item product product-item">
          <div class="product-item-info" data-container="product-grid">
            <strong class="product-item-name">
              <a href="<?php /* @escapeNotVerified */ echo $_item->getProductUrl() ?>"> 
                <?php
                  $_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
                  $productImage = $_imagehelper->init($_item, 'product_base_image')
                  ->constrainOnly(FALSE)
                  ->keepAspectRatio(TRUE)
                  ->keepFrame(FALSE)->resize(240)->getUrl();?>
                  <img src="<?php echo $productImage;?>" width="300" height="300" alt="<?php echo $block->escapeHtml($_item->getItemName()) ?>"> 
                  <span class="collection"><?php echo $block->escapeHtml($_item->getItemCollection()) ?><br> </span>
                  <span class="item-name"><?php echo $block->escapeHtml($_item->getItemName()) ?> </span></a>
                  </strong> 
               <?php if ($block->getCanShowProductPrice($_product)): ?>
                  <?php if ($block->getCanShowProductPrice($_item)): ?>
                      <?php /* @escapeNotVerified */ echo $block->getProductPrice($_item) ?>
                  <?php endif; ?>
               <?php endif; ?>
            <?php if ($_product->isSaleable()): ?>
            <?php if ($_item->isSaleable()) : ?>

            <?php else: ?>
              <div class="stock unavailable" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
                  <span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span>
              </div>
            <?php endif; ?>
            <?php endif; ?>
            <?php if ($block->getCanShowProductPrice($_product)
            && $block->getCanShowProductPrice($_item)
            && trim($block->getProductPriceHtml(
              $_item,
              \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
            ))): ?>
                  <?php echo $block->getProductPriceHtml(
                      $_item,
                      \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
                  ) ?>
            <?php endif; ?>
          </div>
        </li>
        <?php endforeach; ?>
        </ol>
         <?php else: ?>
                            <?php /* @escapeNotVerified */ echo __('No options of this product are available.') ?>
        <?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.