I am finding the way to call my custom extension's model inside my phtml file. and i am using the following code to get the model inside my phtml file.

<?php $price_rules=$objectManager->getModel('Startupready_Myrules/Quantitybreaks'); $price_rules->load($priceRuleList); ?>

But after calling like this. I found that no css of js are applying in the front-end. Is there any way to call my custom created extension's model? Thanks in advance.

  • Do the process in block related to your phtml, then return the result and use in the template. Not recommended to use object manager. Is there any reason for processing the data in phtml ? – Krishna ijjada Sep 17 '16 at 7:18
  • can you please be more elaborate your answer...I am not getting you..also i would be more helpful if you can use the code to tell me what is the mean...thanks for your reply.. – Yudi Sep 17 '16 at 7:22
  • and the reason to process the data is that i have to show some data in list page which is related to products.. So i created my custom extension by the help of that i can show the required data. but the problem is that i am not understanding the phenomena of how do i call that extension in my list page..? – Yudi Sep 17 '16 at 7:31
  • "I found that no css of js are applying in the front-end" it's because you've got a PHP error. Check your logs. – Design by Adrian Oct 21 '16 at 8:56

Assume you have a custom block (Startupready/Myrules/Block) for your template. Inside the block call the model and render the data like getPosts() in the below example, in your case $price_rules->load($priceRuleList); and return the result.

In the templates call $result = $block->load();

Then process the data.

I have placed some sample example code how to call, by using this approach you can process your code.

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Ashsmith\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->_postCollectionFactory = $postCollectionFactory;

 * @return \Ashsmith\Blog\Model\ResourceModel\Post\Collection
public function getPosts()
    // Check if posts has already been defined
    // makes our block nice and re-usable! We could
    // pass the 'posts' data to this block, with a collection
    // that has been filtered differently!
    if (!$this->hasData('posts')) {
        $posts = $this->_postCollectionFactory
            ->addFilter('is_active', 1)
        $this->setData('posts', $posts);
    return $this->getData('posts');
  • Thanks for your reply. As you told me that with the help of <?php $result = $block->load(); ?> , i can render the data. But my dear friend how do i give the path of my extension before <?php ...->load(); ?>. Because if i put like as you told me. then it wouldn't get the path of my extension. my extension is in app/code/Startupready folder. – Yudi Sep 17 '16 at 7:53
  • I totally confused, please provide the clear code with your intention in the question ..........:) – Krishna ijjada Sep 17 '16 at 8:00
  • public function load($categories = false) { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $rules = $objectManager->create('Magento\Salesrule\Rule\Collection')->load(); foreach($rules as $rule){ ................ my custom code which returns the data................ } } this is a file inside my custom extension form where i am getting my data which i want to show. and as you know that after install the extension its location is inside app/code/myextension. now i am want to ask you that how can i render this data and show inside the list.phtml file. – Yudi Sep 17 '16 at 8:03
  • I mean what you are trying to do? – Krishna ijjada Sep 17 '16 at 8:05
  • 1
    as like magento 1 i was using this $price_rules = Mage::getModel('startupready_Myrules/quantitybreaks'); $price_rules->load($priceRuleList); to render the data form extension. now is there any way to get data like this in magento 2? – Yudi Sep 17 '16 at 8:08

You can use ObjectManager to call method of any model. just for an example-

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $price_rules = $objectManager->get('CustomModel\namespace\Model\Quantitybreaks'); $price_rules->load($price_rule_category); ?>

But try to use dependency injection instead of this way, However this is also a quick way to do it.

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.