1

I have some issue with initialization uiComponent and passing parameters to component.

So I have code, example below:

 <?php

// @codingStandardsIgnoreFile

/** @var \Magento\Wishlist\Block\Catalog\Product\View\AddTo\Wishlist $block */
$productStatus = $block->getData('productStatus');
?>
<?php if ($block->isWishListAllowed()) : ?>
    <div data-bind="scope: 'wishListComponent'">
        <!-- ko template: getTemplate() --><!-- /ko -->
    </div>

    <script type="text/x-magento-init">
        {
            "*": {
                "Magento_Ui/js/core/app": {
                    "components": {
                        "wishListComponent": {
                            "component": "addToWishlistComponent",
                            "params": <?= /* @noEscape */ $block->getWishlistParams() ?>,
                            "isAdded": "<?= /* @noEscape */ $productStatus->isAddedToWishlist($block->getProduct()) ?>"
                        }
                    }
                }
            }
        }
    </script>
<?php endif; ?>
<script type="text/x-magento-init">
    {
        "body": {
            "addToWishlist": <?= /* @noEscape */ $block->getWishlistOptionsJson() ?>
        }
    }
</script>

My problem - i can`t pass correct data on this line:

"isAdded": "<?= /* @noEscape */ $productStatus->isAddedToWishlist($block->getProduct()) ?>" 

Because Magento cache it and after each reloading page i get the same data, but after reloading page data should be changed, and i have to get changed data ( It work correctly only after cache clearing, but it is not what i want.

So. How i can solve this (without turn off cache) ? Maybe my solution is wrong and there is better way to pass data like this ? Thanks )

1

You can get Data from customer Data local storage and you pass

like below:

            this.wishlist = customerData.get('wishlist');
         var wishListItems = this.wishlist().items;
        $.each( wishListItems, function( index, item ) {
            if(item.product_id === id) {
               //do your code here or pass data as per your requirement
            }
        });

you can get all the added wishlist data here

  • Thanks for your answer. But i confused, when i try to get wishlist items i get correct information about counter but items array is incorrect ibb.co/gR3158y. Do you was facing with similar problem ? – maybe_vp May 28 at 11:29
  • yes therefor you have to check \Magento\Wishlist\CustomerData\Wishlist getItems method where SIDEBAR_ITEMS_NUMBER variable used overide this method and remove this limit @maybe_vp – Rutvee Sojitra May 28 at 11:32
  • I think it good solution for me. But i faced with some strange thing, after added to wishlist item appears in customerData after second page refresh. What do i mean: I added product to wishlist -> refresh page(there no item in customerData) -> refresh again (item there is in customerData). Do you have any idea ? It's my mistake ? – maybe_vp May 28 at 13:51
  • I have used thing in many projects and data update not work in page refresh ....so it's not issue of wish list... may be other js interrupt ... please check your console may be you can found something – Rutvee Sojitra May 29 at 4:22
  • Figured out and it was my mistake. Thank you very much it very useful thing ) – maybe_vp May 29 at 7:13

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.