I've used app/design/frontend/<your_vendor_name>/<your_theme_name>/Magento_Catalog/templates/product/list/items.phtml to set $showCart = true; so that an Add to Cart button is used for related products rather than checkboxes.
<?php
switch ($type = $block->getType()) {
case 'related-rule':
if ($exist = $block->hasItems()) {
$type = 'related';
$class = $type;
$image = 'related_products_list';
$title = __('Related Products');
$items = $block->getAllItems();
$limit = $block->getPositionLimit();
$shuffle = (int) $block->isShuffled();
$canItemsAddToCart = $block->canItemsAddToCart();
$showAddTo = true;
$showCart = true;
$templateType = null;
$description = false;
}
break;
Is there a way for me to remove the checkboxes for adding the related products to the cart?
I know I could use CSS to simply hide the checkboxes but am wondering if there is a better way?
2 Answers 2
I found these posts to be helpful in overriding the checkboxes on the Related Products and replacing with Add to Cart buttons:
In vendor/magento/module-catalog/view/frontend/templates/product/list/items.phtml
Change $canItemsAddToCart = $block->canItemsAddToCart(); to $canItemsAddToCart = false; in the switch statement, case: related-rule and/or related.
Some template customization may come after this to completely remove the checkboxes, and I suggest copying this vendor file into your theme
app/design/frontend/
Vendor/theme/Magento_Catalog/templates/product/list.phtml
You can remove it by remove input checkbox from this line on same file.
<?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
<?php if (!$_item->getRequiredOptions()): ?>
<div class="field choice related">
<input type="checkbox" class="checkbox related" id="related-checkbox<?php /* @escapeNotVerified */ echo $_item->getId() ?>" name="related_products[]" value="<?php /* @escapeNotVerified */ echo $_item->getId() ?>" />
<label class="label" for="related-checkbox<?php /* @escapeNotVerified */ echo $_item->getId() ?>"><span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span></label>
</div>
<?php endif; ?>
<?php endif; ?>
Above code will show check box. now just replace this line with below code.
<?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
<?php if (!$_item->getRequiredOptions()): ?>
<div class="field choice related">
<label class="label" for="related-checkbox<?php /* @escapeNotVerified */ echo $_item->getId() ?>"><span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span></label>
</div>
<?php endif; ?>
<?php endif; ?>