Magento2 – Add to Wishlist Button in Cart Page

magento2.1.0

In magento2 ADD TO WISHLIST button functionality not provided by default on cart page so I want to implement ADD TO WISHLIST functionality on cart page how can I do that?

So user can directly add item to wishlist from cart page.

UPDATE

I have enabled templatepath hint for frontend and I see that on cart page move_to_wishlist.phtml file is called from app/design/frontend/Forever/theme/Magento_Wishlist/templates/cart/item/renderer/actions

move_to_wishlist.phtml file code is below.

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

But move to wishlist button is not showing on cart page so I have remove if condition from above code so it's working perfect and showing move to wishlist on cart page and working.

So my question is when I remove if ($block->isAllowInCart() && $block->isProductVisibleInSiteVisibility()) code from move_to_wishlist.phtml wishlist button is showing otherwise not so does there issue with block file or core code ?

Any help will be appreciated.

Best Answer

By default this functionality already exist in M2. You can check following file

1. Magento/Wishlist/view/frontend/layout/checkout_cart_item_renderers.xml

2. Magento/Wishlist/view/frontend/templates/cart/item/renderer/actions/move_to_wishlist.phtml

In template file, there is an condition like

<?php if ($block->isAllowInCart() && $block->isProductVisibleInSiteVisibility()): ?>

If this condition is true then you can able to view 'Move to Wishlist' under product image that means enter image description here

Simple thought is if you login then you can able to view this button in cart page.

[Updated]

Following two function make the 'Move to Wishlist' button visible.

#Magento/Wishlist/Block/Cart/Item/Renderer/Actions/MoveToWishlist.php
/**
 * Check whether "add to wishlist" button is allowed in cart
 *
 * @return bool
 */
public function isAllowInCart()
{
    return $this->wishlistHelper->isAllowInCart();
}

#Magento/Checkout/Block/Cart/Item/Renderer/Actions/Generic.php
/**
 * Check if product is visible in site visibility
 *
 * @return bool
 * @codeCoverageIgnore
 */
public function isProductVisibleInSiteVisibility()
{
    return $this->getItem()->getProduct()->isVisibleInSiteVisibility();
}

First logic implement in helper class.

#Magento/Wishlist/Helper/Data.php

/**
 * Check is allow wishlist action in shopping cart
 *
 * @return bool
 */
public function isAllowInCart()
{
    return $this->isAllow() && $this->getCustomer();
}

Here $this->isAllow() return true if 'wishlist' module output is active(menas this module is not disabled from Admin->Stores->Configuration->Advanced) and and active this module too. $this->getCustomer() return true if login.

#Magento/Checkout/Block/Cart/Item/Renderer/Actions/Generic.php
/**
 * Check if product is visible in site visibility
 *
 * @return bool
 * @codeCoverageIgnore
 */
public function isProductVisibleInSiteVisibility()
{
    return $this->getItem()->getProduct()->isVisibleInSiteVisibility();
}