Is it good practice to get product collections in helper function and calling in phtml file custom module Magento 2Managing System Configuration data in helper class (DRY Principle)I created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?getting error in while running Cutom admin controller URL in magento 2Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleCreate Dirty Playground In Magento 2.1.6Magento2 Why I can't get POST data from .MagentoFrameworkAppRequestInterface $request?Zend_Json_Exception Decoding failed: Syntax error some time in Magento (All versions)Magento 2.3 Can't view module's front end page output?Deleted ShipperHQ module causing error in “All Customers” section of Magento 2Captcha Helper method override not working in Magento2

Can a successful book series let the bad guy win?

How to securely dispose of a smartphone?

/etc/hosts not working

Why doesn't SpaceX land boosters in Africa?

Does a lens with a bigger max. aperture focus faster than a lens with a smaller max. aperture?

Can dual citizens open crypto exchange accounts where U.S. citizens are prohibited?

How far can gerrymandering go?

What election rules and voting rights are guaranteed by the US Constitution?

How do I present a future free of gender stereotypes without being jarring or overpowering the narrative?

Can I use Alchemist's fire to turn my sword into a virtual Flame Blade?

How do ohm meters measure high resistances?

When casting a spell with a long casting time, what happens if you don't spend your action on a turn to continue casting?

Avoiding repetition when using the "snprintf idiom" to write text

Why would anyone even use a Portkey?

Sharing referee/AE report online to point out a grievous error in refereeing

If I were to build a J3 cub twice the size of the original using the same CG would it fly?

Why was p[:] designed to work differently in these two situations?

How did they film the Invisible Man being invisible, in 1933?

pgfmath does not work

If a USA citizen marries a foreign citizen who has a kid from a previous marriage

What was the point of separating stdout and stderr?

Calculus, Water Poured into a Cone: Why is Derivative Non-linear?

How can I deal with extreme temperatures in a hotel room?

The Lucas argument vs the theorem-provers--who wins and why?



Is it good practice to get product collections in helper function and calling in phtml file custom module Magento 2


Managing System Configuration data in helper class (DRY Principle)I created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?getting error in while running Cutom admin controller URL in magento 2Magento 2.1: Invoke urlBuilder->getUrl() in a controller in a custom moduleCreate Dirty Playground In Magento 2.1.6Magento2 Why I can't get POST data from .MagentoFrameworkAppRequestInterface $request?Zend_Json_Exception Decoding failed: Syntax error some time in Magento (All versions)Magento 2.3 Can't view module's front end page output?Deleted ShipperHQ module causing error in “All Customers” section of Magento 2Captcha Helper method override not working in Magento2






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4
















Hi all, i wanted to know either is it good practice to get product
collections in helper function and calling that function in phtml file
within custom module Magento 2



Because in my case helper function not working properly.




Please check below Code




appcodeVendorModuleHelperData.php




<?php
namespace VendorModuleHelper;

class Data extends MagentoFrameworkAppHelperAbstractHelper


protected $_objectManager;
const NEWMEMBER = "NEWMEMBER";

public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoFrameworkObjectManagerInterface $objectmanager
)
$this->_objectManager = $objectmanager;
parent::__construct($context);


public function getProductCollections()
$productSku = self::NEWMEMBER;
$loggedInProducts = $this->_objectManager->get('MagentoCatalogModelProduct')
->getCollection()
->addAttributeToFilter('sku', array('in' => $productSku));
return $loggedInProducts;







appcodeVendorModuleviewfrontendtemplatesMember.phtml




$helper = $this->helper('VendorModuleHelperData');
$getProductCollections = $helper->getProductCollections();









share|improve this question






























    4
















    Hi all, i wanted to know either is it good practice to get product
    collections in helper function and calling that function in phtml file
    within custom module Magento 2



    Because in my case helper function not working properly.




    Please check below Code




    appcodeVendorModuleHelperData.php




    <?php
    namespace VendorModuleHelper;

    class Data extends MagentoFrameworkAppHelperAbstractHelper


    protected $_objectManager;
    const NEWMEMBER = "NEWMEMBER";

    public function __construct(
    MagentoFrameworkAppHelperContext $context,
    MagentoFrameworkObjectManagerInterface $objectmanager
    )
    $this->_objectManager = $objectmanager;
    parent::__construct($context);


    public function getProductCollections()
    $productSku = self::NEWMEMBER;
    $loggedInProducts = $this->_objectManager->get('MagentoCatalogModelProduct')
    ->getCollection()
    ->addAttributeToFilter('sku', array('in' => $productSku));
    return $loggedInProducts;







    appcodeVendorModuleviewfrontendtemplatesMember.phtml




    $helper = $this->helper('VendorModuleHelperData');
    $getProductCollections = $helper->getProductCollections();









    share|improve this question


























      4












      4








      4


      1







      Hi all, i wanted to know either is it good practice to get product
      collections in helper function and calling that function in phtml file
      within custom module Magento 2



      Because in my case helper function not working properly.




      Please check below Code




      appcodeVendorModuleHelperData.php




      <?php
      namespace VendorModuleHelper;

      class Data extends MagentoFrameworkAppHelperAbstractHelper


      protected $_objectManager;
      const NEWMEMBER = "NEWMEMBER";

      public function __construct(
      MagentoFrameworkAppHelperContext $context,
      MagentoFrameworkObjectManagerInterface $objectmanager
      )
      $this->_objectManager = $objectmanager;
      parent::__construct($context);


      public function getProductCollections()
      $productSku = self::NEWMEMBER;
      $loggedInProducts = $this->_objectManager->get('MagentoCatalogModelProduct')
      ->getCollection()
      ->addAttributeToFilter('sku', array('in' => $productSku));
      return $loggedInProducts;







      appcodeVendorModuleviewfrontendtemplatesMember.phtml




      $helper = $this->helper('VendorModuleHelperData');
      $getProductCollections = $helper->getProductCollections();









      share|improve this question

















      Hi all, i wanted to know either is it good practice to get product
      collections in helper function and calling that function in phtml file
      within custom module Magento 2



      Because in my case helper function not working properly.




      Please check below Code




      appcodeVendorModuleHelperData.php




      <?php
      namespace VendorModuleHelper;

      class Data extends MagentoFrameworkAppHelperAbstractHelper


      protected $_objectManager;
      const NEWMEMBER = "NEWMEMBER";

      public function __construct(
      MagentoFrameworkAppHelperContext $context,
      MagentoFrameworkObjectManagerInterface $objectmanager
      )
      $this->_objectManager = $objectmanager;
      parent::__construct($context);


      public function getProductCollections()
      $productSku = self::NEWMEMBER;
      $loggedInProducts = $this->_objectManager->get('MagentoCatalogModelProduct')
      ->getCollection()
      ->addAttributeToFilter('sku', array('in' => $productSku));
      return $loggedInProducts;







      appcodeVendorModuleviewfrontendtemplatesMember.phtml




      $helper = $this->helper('VendorModuleHelperData');
      $getProductCollections = $helper->getProductCollections();






      module phtml magento2.3.1 helper






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 21 at 6:43







      Hitesh Balpande

















      asked Jun 21 at 6:30









      Hitesh BalpandeHitesh Balpande

      5442 silver badges14 bronze badges




      5442 silver badges14 bronze badges




















          1 Answer
          1






          active

          oldest

          votes


















          3














          As per the Magento architecture MVC - Model View Controller it is not good practice to use the logic code in helper file.



          Follow Model-View-Control (MVC) pattern



          Make sure your extension adheres to the MVC Pattern, and that it does not violate any of its principles.



          Some important things to check in your extensions:



          1. Make sure your Business Logic, Configuration, and SQL are implemented in the correct places.

          2. Make sure that CSS, JavaScript, HTML, and XML code are all in the appropriate files (i.e. they should not be inline).

          3. Use appropriate logic in a Block, Helper, Template, Controller, or Model.

          4. Ensure correct module design.

          Below are some other things to know while customization and create extension.



          Use dependency injection:



          Direct class instantiation is not recommended because the class can be rewritten. If the class is created directly, any rewrites will not be applied and it breaks Magento’s class rewrite capability. We encourage you to become familiar with how we use dependency injection to get an instance of a class.



          Use the PHP_CodeSniffer tool:



          PHP_CodeSniffer is a set of PHP scripts that checks your code for violations of a particular coding standard. It can be used in conjunction with the Magento Coding Standard to check your code for some of the more common Magento and PHP problems. Using these two tools will ensure that your extension code meets many of Magento’s coding standards. It also has the added benefits of keeping your code clean and maintainable.



          I hope it helps!






          share|improve this answer



























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "479"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f279114%2fis-it-good-practice-to-get-product-collections-in-helper-function-and-calling-in%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            As per the Magento architecture MVC - Model View Controller it is not good practice to use the logic code in helper file.



            Follow Model-View-Control (MVC) pattern



            Make sure your extension adheres to the MVC Pattern, and that it does not violate any of its principles.



            Some important things to check in your extensions:



            1. Make sure your Business Logic, Configuration, and SQL are implemented in the correct places.

            2. Make sure that CSS, JavaScript, HTML, and XML code are all in the appropriate files (i.e. they should not be inline).

            3. Use appropriate logic in a Block, Helper, Template, Controller, or Model.

            4. Ensure correct module design.

            Below are some other things to know while customization and create extension.



            Use dependency injection:



            Direct class instantiation is not recommended because the class can be rewritten. If the class is created directly, any rewrites will not be applied and it breaks Magento’s class rewrite capability. We encourage you to become familiar with how we use dependency injection to get an instance of a class.



            Use the PHP_CodeSniffer tool:



            PHP_CodeSniffer is a set of PHP scripts that checks your code for violations of a particular coding standard. It can be used in conjunction with the Magento Coding Standard to check your code for some of the more common Magento and PHP problems. Using these two tools will ensure that your extension code meets many of Magento’s coding standards. It also has the added benefits of keeping your code clean and maintainable.



            I hope it helps!






            share|improve this answer





























              3














              As per the Magento architecture MVC - Model View Controller it is not good practice to use the logic code in helper file.



              Follow Model-View-Control (MVC) pattern



              Make sure your extension adheres to the MVC Pattern, and that it does not violate any of its principles.



              Some important things to check in your extensions:



              1. Make sure your Business Logic, Configuration, and SQL are implemented in the correct places.

              2. Make sure that CSS, JavaScript, HTML, and XML code are all in the appropriate files (i.e. they should not be inline).

              3. Use appropriate logic in a Block, Helper, Template, Controller, or Model.

              4. Ensure correct module design.

              Below are some other things to know while customization and create extension.



              Use dependency injection:



              Direct class instantiation is not recommended because the class can be rewritten. If the class is created directly, any rewrites will not be applied and it breaks Magento’s class rewrite capability. We encourage you to become familiar with how we use dependency injection to get an instance of a class.



              Use the PHP_CodeSniffer tool:



              PHP_CodeSniffer is a set of PHP scripts that checks your code for violations of a particular coding standard. It can be used in conjunction with the Magento Coding Standard to check your code for some of the more common Magento and PHP problems. Using these two tools will ensure that your extension code meets many of Magento’s coding standards. It also has the added benefits of keeping your code clean and maintainable.



              I hope it helps!






              share|improve this answer



























                3












                3








                3







                As per the Magento architecture MVC - Model View Controller it is not good practice to use the logic code in helper file.



                Follow Model-View-Control (MVC) pattern



                Make sure your extension adheres to the MVC Pattern, and that it does not violate any of its principles.



                Some important things to check in your extensions:



                1. Make sure your Business Logic, Configuration, and SQL are implemented in the correct places.

                2. Make sure that CSS, JavaScript, HTML, and XML code are all in the appropriate files (i.e. they should not be inline).

                3. Use appropriate logic in a Block, Helper, Template, Controller, or Model.

                4. Ensure correct module design.

                Below are some other things to know while customization and create extension.



                Use dependency injection:



                Direct class instantiation is not recommended because the class can be rewritten. If the class is created directly, any rewrites will not be applied and it breaks Magento’s class rewrite capability. We encourage you to become familiar with how we use dependency injection to get an instance of a class.



                Use the PHP_CodeSniffer tool:



                PHP_CodeSniffer is a set of PHP scripts that checks your code for violations of a particular coding standard. It can be used in conjunction with the Magento Coding Standard to check your code for some of the more common Magento and PHP problems. Using these two tools will ensure that your extension code meets many of Magento’s coding standards. It also has the added benefits of keeping your code clean and maintainable.



                I hope it helps!






                share|improve this answer















                As per the Magento architecture MVC - Model View Controller it is not good practice to use the logic code in helper file.



                Follow Model-View-Control (MVC) pattern



                Make sure your extension adheres to the MVC Pattern, and that it does not violate any of its principles.



                Some important things to check in your extensions:



                1. Make sure your Business Logic, Configuration, and SQL are implemented in the correct places.

                2. Make sure that CSS, JavaScript, HTML, and XML code are all in the appropriate files (i.e. they should not be inline).

                3. Use appropriate logic in a Block, Helper, Template, Controller, or Model.

                4. Ensure correct module design.

                Below are some other things to know while customization and create extension.



                Use dependency injection:



                Direct class instantiation is not recommended because the class can be rewritten. If the class is created directly, any rewrites will not be applied and it breaks Magento’s class rewrite capability. We encourage you to become familiar with how we use dependency injection to get an instance of a class.



                Use the PHP_CodeSniffer tool:



                PHP_CodeSniffer is a set of PHP scripts that checks your code for violations of a particular coding standard. It can be used in conjunction with the Magento Coding Standard to check your code for some of the more common Magento and PHP problems. Using these two tools will ensure that your extension code meets many of Magento’s coding standards. It also has the added benefits of keeping your code clean and maintainable.



                I hope it helps!







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 21 at 6:59

























                answered Jun 21 at 6:53









                Chirag PatelChirag Patel

                3,6186 silver badges28 bronze badges




                3,6186 silver badges28 bronze badges



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f279114%2fis-it-good-practice-to-get-product-collections-in-helper-function-and-calling-in%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                    Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                    Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form