My controller:
appcodeHeartListcolorControllerIndexWishlist.php
<?php
namespace HeartListcolorControllerIndex;
class Wishlist extends MagentoFrameworkAppActionAction {
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoWishlistHelperData $wishlistHelper,
MagentoFrameworkControllerResultJsonFactory $jsonFactory
) {
parent::__construct($context);
$this->wishlistHelper = $wishlistHelper;
$this->jsonFactory = $jsonFactory;
}
public function execute() {
$result = $this->jsonFactory->create();
$data = $this->wishlistHelper->getWishlistItemCollection()->getData();
return $result->setData(('status' => 200, 'items' => $data));
}
}
appcodeHeartListcoloretcfrontendroutes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route frontName="customwishlist" id="customwishlist">
<module name="Heart_Listcolor"/>
</route>
</router>
</config>
Now the product added but redirect to the wishlist landing page, how to add the product to Wishlist without redirecting and without refresh using ajax
My custom theme Magento_Wishlist
appdesignfrontendZeromy_themeMagento_Wishlisttemplatescatalogproductlistaddtowishlist.phtml
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** @var MagentoWishlistBlockCatalogProductProductListItemAddToWishlist $block */
?>
<?php if ($block->getWishlistHelper()->isAllow()) : ?>
<a href="#"
class="action towishlist"
title="<?= $block->escapeHtmlAttr(__('Add to Wish List')) ?>"
aria-label="<?= $block->escapeHtmlAttr(__('Add to Wish List')) ?>"
data-post='<?= /* @noEscape */ $block->getAddToWishlistParams($block->getProduct()) ?>'
role="button">
<!-- data-action="add-to-wishlist" -->
<img class="whislist-icon product-id-<?php echo $block->getProduct()->getId();?>" src="<?php /* @escapeNotVerified */ echo $block->getViewFileUrl('images/whislist.png'); ?>" />
<span><?= $block->escapeHtml(__('Add to Wish List')) ?></span>
</a>
<?php endif; ?>
<?php
$_objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $_objectManager->get('MagentoStoreModelStoreManagerInterface');
$currentStore = $storeManager->getStore();
$mediaUrl = $currentStore->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
$img = $mediaUrl."yt.png";
?>
<script>
require(('jquery'), function($){
jQuery.ajax({
url: '<?php echo $this->getUrl('customwishlist/index/wishlist') ?>',
method: 'get',
dataType: 'json',
success: function(data) {
var wislistAddesCheckData = data;
var itemLenth = wislistAddesCheckData.items.length;
for(i=0;i<itemLenth; i++){
var wislistAddedProductId = wislistAddesCheckData.items(i).product_id;
$(".product-id-"+wislistAddedProductId).attr('src','<?php echo $img; ?>');
}
}
});
});
</script>