当前位置: 技术文章>> Magento 2:如何将组产品单独添加到购物车中

文章标题:Magento 2:如何将组产品单独添加到购物车中
  • 文章分类: Magento
  • 16002 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


在Magento 2中将组产品单独添加到购物车的步骤:

第1步: 您需要覆盖主题中的grouped.phtml文件。为此,请转到以下文件路径。

app\design\frontend\Theme\Yourtheme\Magento_GroupedProduct\templates\product\view\type\grouped.phtml

并按如下方式添加代码。

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
 
// @codingStandardsIgnoreFile
 
/**
 * Grouped product data template
 *
 * @var $block \Magento\Catalog\Block\Product\View\BaseImage
 * @var $block \Magento\GroupedProduct\Block\Product\View\Type\Grouped
 */
?>
 
<?php $block->setPreconfiguredValue(); ?>
<?php $_product = $block->getProduct(); ?>
<?php $_associatedProducts = $block->getAssociatedProducts(); ?>
<?php $_hasAssociatedProducts = count($_associatedProducts) > 0; ?>
<?php $grouped = $_product->load($_product->getId()); ?>                    
<?php $associatedProducts = $grouped->getTypeInstance()->getAssociatedProducts($grouped); ?>
 
</form>
 
<div class="table-wrapper grouped">
    <table class="table data grouped" id="super-product-table">
        <caption class="table-caption"><?= /* @escapeNotVerified */ __('Grouped product items') ?></caption>
        <thead>
            <tr>
                <th class="col item" scope="col"><?= /* @escapeNotVerified */ __('Product Name') ?></th>
                <?php if ($_product->isSaleable()): ?>
                    <th class="col qty" scope="col"><?= /* @escapeNotVerified */ __('Qty') ?></th>
                <?php endif; ?>
            </tr>
        </thead>
 
        <?php if ($_hasAssociatedProducts): ?>
        <?php foreach ($associatedProducts as $_item): ?>
        <tbody>
            <tr>
                <td data-th="<?= $block->escapeHtml(__('Product Name')) ?>" class="col item">
                    <strong class="product-item-name"><?= $block->escapeHtml($_item->getName()) ?></strong>
                    <?php if ($block->getCanShowProductPrice($_product)): ?>
                        <?php if ($block->getCanShowProductPrice($_item)): ?>
                            <?= /* @escapeNotVerified */ $block->getProductPrice($_item) ?>
                        <?php endif; ?>
                     <?php endif; ?>
                </td>
                <?php if ($_product->isSaleable()): ?>
                    <td data-th="<?= $block->escapeHtml(__('Qty')) ?>" class="col qty">
                        <?php if ($_item->isSaleable()) : ?>
                            <div class="control qty">
                                <form data-role="tocart-form" action="<?php echo $this->getAddToCartUrl($_item); ?>" method="post">
                                    <input type="hidden" name="product" value="<?php echo $_item->getId(); ?>">
                                    <input type="hidden" name="related_product" id="related-products-field" value="">
                                    <input type="number"
                                       name="qty"
                                       value="<?= /* @escapeNotVerified */ $_item->getQty() * 1 ?>"
                                       title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                                       class="input-text qty required"
                                       />
 
                                    <?php echo $block->getBlockHtml('formkey')?>
 
                                    <button type="submit" title="Add to Cart" class="addtocart">Add to Cart</button>
                                </form>
                            </div>
 
                        <?php else: ?>
                            <div class="stock unavailable" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
                                <span><?= /* @escapeNotVerified */ __('Out of stock') ?></span>
                            </div>
                        <?php endif; ?>
                    </td>
                    <td class="addtocart">
                    </td>
                <?php endif; ?>
            </tr>
            <?php if ($block->getCanShowProductPrice($_product)
                && $block->getCanShowProductPrice($_item)
                && trim($block->getProductPriceHtml(
                    $_item,
                    \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
                ))): ?>
                <tr class="row-tier-price">
                    <td colspan="2">
                        <?= $block->getProductPriceHtml(
                            $_item,
                            \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE
                        ) ?>
                    </td>
                </tr>
            <?php endif; ?>
        </tbody>
        <?php endforeach; ?>
        <?php else: ?>
            <tbody>
                <tr>
                    <td class="unavailable"
                        colspan="<?php if ($_product->isSaleable()): ?>4<?php else : ?>3<?php endif; ?>">
                        <?= /* @escapeNotVerified */ __('No options of this product are available.') ?>
                    </td>
                </tr>
            </tbody>
        <?php endif; ?>
    </table>
</div>
<div id="validation-message-box"></div>
 
<form>

步骤2:之后,您需要运行以下命令。

sudo php bin/magento cache:flush

结论:

这样,您可以在Magento 2中将组产品单独添加到购物车中。


推荐文章