`
use Magento\Customer\Model\Session;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\NotFoundException;
use Magento\Wishlist\Controller\WishlistProviderInterface;
use Magento\Wishlist\Model\Item;
use Magento\Wishlist\Model\Product\AttributeValueProvider;
use Magento\Wishlist\Model\WishlistFactory;
/**
Class Remove
@package Codesicle\WishlistAjax\Plugin\Wishlist\Controller\Index
/
class Remove
{
/*
- @var WishlistProviderInterface
*/
protected $wishlistProvider;
/**
- @var AttributeValueProvider
*/
private $attributeValueProvider;
/**
- @var Session
*/
protected $customerSession;
/**
- @var JsonFactory
*/
protected $jsonFactory;
/**
- @var WishlistFactory
*/
protected $wishlistFactory;
public function __construct(
WishlistProviderInterface $wishlistProvider,
AttributeValueProvider $attributeValueProvider = null,
JsonFactory $jsonFactory,
Session $customerSession,
WishlistFactory $wishlistFactory
)
{
$this->wishlistProvider = $wishlistProvider;
$this->attributeValueProvider = $attributeValueProvider
?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class);
$this->jsonFactory = $jsonFactory;
$this->customerSession = $customerSession;
$this->wishlistFactory = $wishlistFactory;
}
public function aroundExecute($subject, $proceed)
{
$id = (int)$this->getRequest()->getParam('item');
$item = $subject->_objectManager->create(Item::class)->load($id);
if (!$item->getId()) {
throw new NotFoundException(__('Page not found.'));
}
$wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
if (!$wishlist) {
throw new NotFoundException(__('Page not found.'));
}
try {
$item->delete();
$wishlist->save();
$productName = $this->attributeValueProvider
->getRawAttributeValue($item->getProductId(), 'name');
$response = [
'errors' => false,
'message' => __('%1 has been removed from your Wish List.', $productName)
];
} catch (\Magento\Framework\Exception\LocalizedException $exception) {
$response = [
'errors' => true,
'message' => __('We can\'t delete the item from Wish List right now because of an error: %1.', $exception->getMessage())
];
} catch (\Exception $exception) {
$response = [
'errors' => true,
'message' => __('We can\'t delete the item from the Wish List right now.')
];
}
$this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
$resultJson = $this->jsonFactory->create();
return $resultJson->setData($response);
}
use Magento\Customer\Model\Session;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\NotFoundException;
use Magento\Wishlist\Controller\WishlistProviderInterface;
use Magento\Wishlist\Model\Item;
use Magento\Wishlist\Model\Product\AttributeValueProvider;
use Magento\Wishlist\Model\WishlistFactory;
/**
* Class Remove
* @package Codesicle\WishlistAjax\Plugin\Wishlist\Controller\Index
*/
class Remove
{
/**
* @var WishlistProviderInterface
*/
protected $wishlistProvider;
/**
* @var AttributeValueProvider
*/
private $attributeValueProvider;
/**
* @var Session
*/
protected $customerSession;
/**
* @var JsonFactory
*/
protected $jsonFactory;
/**
* @var WishlistFactory
*/
protected $wishlistFactory;
public function __construct(
WishlistProviderInterface $wishlistProvider,
AttributeValueProvider $attributeValueProvider = null,
JsonFactory $jsonFactory,
Session $customerSession,
WishlistFactory $wishlistFactory
)
{
$this->wishlistProvider = $wishlistProvider;
$this->attributeValueProvider = $attributeValueProvider
?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class);
$this->jsonFactory = $jsonFactory;
$this->customerSession = $customerSession;
$this->wishlistFactory = $wishlistFactory;
}
public function aroundExecute($subject, $proceed)
{
$id = (int)$this->getRequest()->getParam('item');
$item = $subject->_objectManager->create(Item::class)->load($id);
if (!$item->getId()) {
throw new NotFoundException(__('Page not found.'));
}
$wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
if (!$wishlist) {
throw new NotFoundException(__('Page not found.'));
}
try {
$item->delete();
$wishlist->save();
$productName = $this->attributeValueProvider
->getRawAttributeValue($item->getProductId(), 'name');
$response = [
'errors' => false,
'message' => __('%1 has been removed from your Wish List.', $productName)
];
} catch (\Magento\Framework\Exception\LocalizedException $exception) {
$response = [
'errors' => true,
'message' => __('We can\'t delete the item from Wish List right now because of an error: %1.', $exception->getMessage())
];
} catch (\Exception $exception) {
$response = [
'errors' => true,
'message' => __('We can\'t delete the item from the Wish List right now.')
];
}
$this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
$resultJson = $this->jsonFactory->create();
return $resultJson->setData($response);
}