4

I am working on a custom module that is pulling in 2 different collections. When I try to use a for-each to loop through either of the collections the page displays the collection correctly. However, when I try to loop through both collections (as in the code below) I get the following error:

report.CRITICAL: Warning: Invalid argument supplied for foreach() ... on 65 [] []

I would appreciate any insight.

<?php
    $purchasedFrom = $block->getPurchasedFromCollection();
    $registeredProducts = $block->getRegisteredProductsCollection();
 ?>   
<form class="form form-product-registration" action="<?php echo $this->getUrl("productregistration/newregistration/post") ?>" method="post" id="form-validate" enctype="multipart/form-data" data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>" autocomplete="off">
    <fieldset class="fieldset newReg">
            <legend class="legend">
                <span><?= $block->escapeHtml(__('Register a new Product')) ?></span>
            </legend>
        <br>
        <?= $block->getBlockHtml('formkey') ?>
        <div class="field required">
            <label class="label">
                <span><?= $block->escapeHtml(__('Purchase Date')) ?></span>
            </label>
            <div class="control">
                <input type="date" name="purchased_date">
            </div>                 
        </div>
        <div class="field required">
            <label class="label">
                <span><?= $block->escapeHtml(__('Serial Number')) ?></span>
            </label>
            <div class="control">
                <input type="text" name="serial_number">
            </div>                 
        </div>
        <div class="field required">
            <label class="label">
                <span><?= $block->escapeHtml(__('Sku')) ?></span>
            </label>
            <div class="control">
                <input type="text" name="sku">
            </div>                 
        </div>
        <div class="field">
            <label class="label">
                <span><?= $block->escapeHtml(__('Product Type')) ?></span>
            </label>
            <div class="control">
                <input type="text" name="product_name">
            </div>                 
        </div>
        <div class="field">
            <label class="label">
                <span><?= $block->escapeHtml(__('Purchased From')) ?></span>
            </label>
            <div class="control">
              <!--  <input type="text" name="purchased_from"> -->
                <select name="purchased_from"> 
                    <?php foreach ($purchasedFrom as $location):?>
                        <option value="<?php echo $location->getId(); ?>"><?php echo $location->getStoreName(); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>                 
        </div>

    <div class="actions-toolbar">
        <div class="primary">
            <button type="submit" class="action save primary" title="<?= $block->escapeHtmlAttr(__('Save Registration')) ?>"><span><?= $block->escapeHtml(__('Save')) ?></span></button>
        </div>
    </div>
</fieldset>
<fieldset class="fieldset reg">
    <legend class="legend"><span><?= $block->escapeHtml(__('Registered Products')) ?></span></legend><br>
    <div class="field">
        <table class="table">
            <thead>
              <tr>
                <th>Serial Number</th>
                <th>Product Type</th>
                <th>Purchase Date</th>
              </tr>
            </thead>
            <tbody>
                <?php if (!empty($registeredProducts)) { ?>
              <?php foreach ($registeredProducts as $registered): ?>  
              <tr>
                <td><?php echo $registered->getSerialNumber();?></td>
                <td><?php echo $registered->getProductName();?></td>
                <td><?php echo $registered->getPurchaseDate();?></td>
              </tr>
            <?php endforeach; ?>  
                <?php } ?>
            </tbody>
        </table>
    </div>
</fieldset>
</form>

error: [2018-07-16 14:31:20] report.CRITICAL: Warning: Invalid argument supplied for foreach() in /.../app/code/NK/ProductRegistration/view/frontend/templates/registration.phtml on line 65 [] []

public function getRegisteredProductsCollection()
{
    $registered = $this->model->getCollection()- 
    >addFieldToFilter('customer_id', $this->session->getCustomerId());
    return $registered;
}

So I was getting this error when I had two blocks pointing to the same template. When I was changed my code so that 2 models were contained within the same block and only one block was assigned to the template everything works correctly.

Can anyone tell me if two blocks can be assigned to the same template in a Magento 2 custom module?

  • Can you post the error? – Sukumar Gorai Jul 16 '18 at 14:43
  • Seems like the $purchasedFrom is empty. Maybe check what it returns – Nikolas Jul 16 '18 at 15:54
  • $purchasedFrom returns the collection correctly – Brian Jul 16 '18 at 16:50
  • can you please add the code for this getRegisteredProductsCollection() function? – Abhishek Panchal Jul 16 '18 at 19:35
0

In the constructor where you are injecting the model, inject its Factory class, and then both the functions get the collection like this.

If you don't know about the factory class, then just add "Factory" in the model name.

e.x Product is the name of you model class, then the product factory class will be "ProductFactory". Factories are auto generated by the magento. You dont need to generate these class. But after making the changes do run setup:di:compile in the cli.

$this->modelFactory->create()->getCollection()-> add whatever the filters you like.

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.