The most important thing is that we need to know where is this button: vendor/magento/module-wishlist/view/frontend/templates/button/tocart.phtml
<?php if ($block->getWishlist()->getItemsCount() && $block->getWishlist()->isSalable()): ?>
<button type="button" data-role="all-tocart" title="<?= $block->escapeHtmlAttr(__('Add All to Cart')) ?>" class="action tocart">
<span><?= $block->escapeHtml(__('Add All to Cart')) ?></span>
</button>
<?php endif;?>
As we can see, Magento checked the salable item(s) already $block->getWishlist()->isSalable()
. But, seem that doesn't enough... I'm going to remove this check and use our custom one.
Let's create a custom module:
Vendor/Wishlist/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Wishlist\Block\Customer\Wishlist\Button"
type="Vendor\Wishlist\Block\Customer\Wishlist\Button" />
</config>
We will override block Magento\Wishlist\Block\Customer\Wishlist\Button
. And create our custom method to check all the salable items.
Vendor/Wishlist/Block/Customer/Wishlist/Button.php
<?php
namespace Vendor\Wishlist\Block\Customer\Wishlist;
class Button extends \Magento\Wishlist\Block\Customer\Wishlist\Button
{
/**
* @return bool
*/
public function canAddAllToCart()
{
$wishlist = $this->getWishlist();
foreach ($wishlist->getItemCollection() as $item) {
if (!$item->getProduct()->getIsSalable()) {
return false;
}
}
return true;
}
}
Set the custom template for wishlist button.
Vendor/Wishlist/view/frontend/layout/wishlist_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="customer.wishlist.button.toCart">
<action method="setTemplate">
<argument name="template" xsi:type="string">Vendor_Wishlist::buttons/tocart.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
Replace $block->getWishlist()->isSalable()
by our custom method: canAddAllToCart()
Vendor/Wishlist/view/frontend/templates/buttons/tocart.phtml
<?php
// @codingStandardsIgnoreFile
/** @var \Vendor\Wishlist\Block\Customer\Wishlist\Button $block */
?>
<?php if ($block->getWishlist()->getItemsCount() && $block->canAddAllToCart()): ?>
<button type="button" data-role="all-tocart" title="<?= $block->escapeHtmlAttr(__('Add All to Cart')) ?>" class="action tocart">
<span><?= $block->escapeHtml(__('Add All to Cart')) ?></span>
</button>
<?php endif;?>
Remember to create module.xml
and registration.php
files.
Magento version is 2.2.2.