2

I'm new to Magento, and I'm not sure how to make this work. I tried searching a lot but could not find anything related.

I have a block function in my custom module which takes 2 parameter

    public function getCatURL($category, $subCategory)
    {
        $collection = $this->collectionFactory->create();
        $collection->addAttributeToFilter('name', strtolower($subCategory));
        $collection->addIsActiveFilter(true);
        $catURL = null;
        foreach ($collection as $cat) {
            $parentCat =  $cat->getParentCategory();
            if (strtolower($parentCat->getName()) == strtolower($category)) {
                $catURL = $cat->getURL();
            }
        }

        return $catURL;
    }

From my phtml, i want to pass the function instance to my requirejs file, which will call the function with function parameters.

<script type="text/x-magento-init">
    {
        "*": {
            "retailAssistant": {
                "CatURL": <?php /* @escapeNotVerified */ echo $block->getCatURL; ?>,
            }
        }
    }
</script>

I expect to call the cart URL function with parameters based on some action.

define([
  "Vendor_Test/js/test",
], function(){
        "use strict";
        return function(config) {
          config.cartURL('men', 'tops');
        }})

Running with this throws:

1 exception(s):
Exception #0 (Exception): Notice: Undefined property: Vendor\Test\Block\Controller::$getCatURL
2
  • $block->getCatURL instend of $block->getCatURL($category, $subCategory) Commented Aug 14, 2021 at 10:59
  • I didn't understand. Can you please explain?
    – Mohsin
    Commented Aug 14, 2021 at 11:41

1 Answer 1

0

Couple of things I can notice.

<script type="text/x-magento-init">
   {
       "*": {
           "retailAssistant": {
               "CatURL": <?php /* @escapeNotVerified */ echo $block->getCatURL; ?>,
           }
       }
   }
</script>

Should be below because template Block is expecting to have two parameters.

<script type="text/x-magento-init">
    {
        "*": {
            "retailAssistant": {
                "CatURL": "<?php /* @escapeNotVerified */ echo $block->getCatURL('men','tops'); ?>",
            }
        }
    }
</script>

You have this somewhere in a js file

define([
  "Vendor_Test/js/test",
], function(){
        "use strict";
        return function(config) {
          config.cartURL('men', 'tops');
        }})

The above code should be as follows because you are trying to access the CatURL, here you could not pass two parameters i.e. category and subcategory.

define([
  "Vendor_Test/js/test",
], function(){
        "use strict";
        return function(config) {
            console.log(config);        
            var categoryURL = config.CatURL;
            console.log(categoryURL);
        }})

This will help you resolve syntax errors and you can access CatURL in JS file.

Clear cache is needed.

1
  • Hi @sandip, Thanks for the answer. But I'm looking for a way to send parameters from a js file.
    – Mohsin
    Commented Aug 16, 2021 at 4:50

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.