1

One product "Outofstock" and save in wishlist then after, when after login in wishlist form click on "Add All to Cart" button. But it's not working because of "Outofstock" product in wishlist. So how can remove when if any one "Outofstock" product in wishlist, "Add All to Cart" button is disabled.

Note : Outofstock product need to add in wishlist.

  • So, your question is only: How to disable "Add All to Cart" button when one of products is out stock in Wishlist page? – Khoa TruongDinh May 31 '18 at 6:22
  • Yes. @KhoaTruongDinh – Payal Patel May 31 '18 at 6:30
1

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.

Your Answer

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

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