0

My script :

<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','/pub/media/yt.png');
}
}
});
});
</script>

My path : app\design\frontend\Zero\my_theme\Magento_Wishlist\templates\catalog\product\list\addto\wishlist.phtml

How to include the image path in $(".product-id-"+wislistAddedProductId).attr('src','/pub/media/yt.png');

1 Answer 1

1

You can use this below code :

Method 1 :

<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 $this->getUrl('pub/media/').yt.png ?>');
                }
            }
        });
    });
</script>

Method 2 :

<?php
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $_objectManager->get('Magento\Store\Model\StoreManagerInterface'); 
$currentStore = $storeManager->getStore();
$mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.