1

I have passing category id through xml i'm trying to check it in block file.but it did not work

foreach ($productCollection as $product) {
            //  get product id
       $productIds[]=$this->getProductData($product->getProductId());
    }
    // get category // IDEA:
    $filterCats = explode(',',$this->getData('parentcat'));
    // filter bestselling products using category id
    if($filterCats != NULL){
    $collection      = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection')->addIdFilter($productIds)->addCategoriesFilter(array('in' => $filterCats));
     }

     $collection      = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection')->addIdFilter($productIds);

xml

  <block class="Mageplaza\Productslider\Block\BestSellerProducts" name="product.slider.bestsellerproducts" template="productslider.phtml" after="banner.block.1">
        <arguments>
            <argument name="products_count" xsi:type="number">8</argument>
            <argument name="margin" xsi:type="string">5</argument>
          <argument name="parentcat" xsi:type="number">2</argument>
            <argument name="heading" xsi:type="string">Best Sellers</argument>
            <argument name="description" xsi:type="string">Here is your Best Sellers description </argument>
        </arguments>
    </block>
2
  • You need to check the condition at last after the $collection you have added. Commented Sep 25, 2018 at 10:57
  • if($filterCats == 0){ $collection = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection')->addIdFilter($productIds); } $collection = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection')->addIdFilter($productIds)->addCategoriesFilter(array('in' => $filterCats)); I have added like this but it won't work
    – Nismath vi
    Commented Sep 25, 2018 at 11:54

1 Answer 1

0

According to docks you should use

$this->getParentcat();

see here: https://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-instructions.html (at the bottom)

It will be better for semantic if you name the parameter parent_cat and then use function: $this->getParentCat();

Let me know if it solves the problem ;)

3
  • i doesn't matter it is parentcat or parent_cat it works .i want to check the condition
    – Nismath vi
    Commented Sep 25, 2018 at 10:47
  • Maybe it is a problem with addCategoriesFilter ? Did you try regular addFieldToFilter method?
    – Jan Żaba
    Commented Sep 25, 2018 at 10:51
  • yeah it works. without if condition
    – Nismath vi
    Commented Sep 25, 2018 at 10:53

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.