1

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.

4

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();
}
  • Please see my updated question. – Dhaval Jul 21 '16 at 7:50
  • when I remove (comment) <?php if ($block->isAllowInCart() && $block->isProductVisibleInSiteVisibility()): ?> wishlist button is showing otherwise not. so can you tell me which condition it is checking and why condition is not true by default? – Dhaval Jul 21 '16 at 7:51
  • One more thing when I open block file MoveToWishlist.php on vendor/magento/module-wishlist/Block/Cart/Item/Renderer/Actions path but in this file I can't see isProductVisibleInSiteVisibility() function and this function is used in template file so I thing that's why if condition is return false. what do you thing? any suggestion ? any explanation? – Dhaval Jul 21 '16 at 7:55
  • Check updated answer – Sohel Rana Jul 21 '16 at 8:12
  • If this answer help to solve your question, don't forget to accept answer. It's helpful for another. – Sohel Rana Jul 21 '16 at 8:23

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.