1

I am trying to change the behavior of "Move to wishlist" button in the cart to add the product in the wishlist while still keep the product in the cart.

I created a custom block AddToWishlist, inside my custom module, which is very similar to the block Magento\Wishlist\Block\Cart\Item\Renderer\Actions\MoveToWishlist, here is my code:

AddToWishlist.php

namespace MyNamespace\Wishlist\Block\Cart\Item\Renderer\Actions;
use Magento\Framework\View\Element\Template;
use Magento\Wishlist\Helper\Data;

class AddToWishlist extends \Magento\Checkout\Block\Cart\Item\Renderer\Actions\Generic
{
    /**
     * @var Data
     */
    protected $wishlistHelper;

    /**
     * @param Template\Context $context
     * @param Data $wishlistHelper
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        Data $wishlistHelper,
        array $data = []
    ) {
        $this->wishlistHelper = $wishlistHelper;
        parent::__construct($context, $data);
    }

    /**
     * Check whether "add to wishlist" button is allowed in cart
     *
     * @return bool
     */
    public function isAllowInCart()
    {
        return $this->wishlistHelper->isAllowInCart();
    }

    /**
     * Get JSON POST params for moving from cart
     *
     * @return string
     */
    public function getAddFromCartParams()
    {
        return $this->wishlistHelper->getAddParams($this->getItem()->getProduct());
    }
}

Here is the code of the template:

add_to_wishlist.phtml

<?php if ($block->isAllowInCart() && $block->isProductVisibleInSiteVisibility()): ?>
<a href="#"
   data-post='<?php /* @escapeNotVerified */ echo $block->getAddFromCartParams(); ?>'
   class="use-ajax action action-towishlist">
    <span><?php /* @escapeNotVerified */ echo __('Add to Wish List'); ?></span>
</a>

And finally I added the block in the cart in the checkout_cart_item_renderers.xml layout:

checkout_cart_item_renderers.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="checkout.cart.item.renderers.default.actions">
            <referenceBlock name="checkout.cart.item.renderers.default.actions.move_to_wishlist" remove="true" />

            <block class="MyNamespace\Wishlist\Block\Cart\Item\Renderer\Actions\AddToWishlist" name="checkout.cart.item.renderers.default.actions.add_to_wishlist" template="cart/item/renderer/actions/add_to_wishlist.phtml" before="checkout.cart.item.renderers.default.actions.edit"/>
        </referenceBlock>
        <referenceBlock name="checkout.cart.item.renderers.simple.actions">
            <referenceBlock name="checkout.cart.item.renderers.simple.actions.move_to_wishlist" remove="true" />

            <block class="MyNamespace\Wishlist\Block\Cart\Item\Renderer\Actions\AddToWishlist" name="checkout.cart.item.renderers.simple.actions.add_to_wishlist" template="cart/item/renderer/actions/add_to_wishlist.phtml" before="checkout.cart.item.renderers.simple.actions.edit"/>
        </referenceBlock>
        <referenceBlock name="checkout.cart.item.renderers.bundle.actions">
            <referenceBlock name="checkout.cart.item.renderers.bundle.actions.move_to_wishlist" remove="true" />

            <block class="MyNamespace\Wishlist\Block\Cart\Item\Renderer\Actions\AddToWishlist" name="checkout.cart.item.renderers.bundle.actions.add_to_wishlist" template="cart/item/renderer/actions/add_to_wishlist.phtml" before="checkout.cart.item.renderers.bundle.actions.edit"/>
        </referenceBlock>
    </body>
</page>

This functionality is partially working but I still have two problems.

First - When I am adding a simple product it is added to the wish list as expected, but when I try to add a bundle product then the product selections are not added correctly in the wishlist.

The second problem is that I do not want to redirect the customer to the wishlist after adding the product there, the customer should remain in the cart page, which is not he case in the current situation.

I will be thankfull if someone help me fixint the issue or if someone can suggest me a better way to change the "Move to wishlist" functionality by keeping the product in the cart.

1

Pls try use

  1. Overwrite controller Magento\Wishlist\Controller\Index\Fromcart

  2. Comment code in function execute

//$this->cart->getQuote()->removeItem($itemId);
//$this->cart->save();
  • Thanks for the response. In addition - for the second part of my question - in order to stay in the cart I have changed $this->wishlistHelper->getAddParams($this->getItem()->getProduct()); with $this->wishlistHelper->getMoveFromCartParams($this->getItem()->getId()) in AddToWishlist.php ` public function getAddFromCartParams() { return $this->wishlistHelper->getMoveFromCartParams($this->getItem()->getId()); } ` – Milko Lorinkov Oct 26 '16 at 19:17

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.