Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DominicWatts/bf65a8bf71965ccb48beda1515e7e7a3 to your computer and use it in GitHub Desktop.
Save DominicWatts/bf65a8bf71965ccb48beda1515e7e7a3 to your computer and use it in GitHub Desktop.
magento 2 category collection cheatsheet

Category Collection Cheatsheet

Import statement

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;

/**
 * @var CategoryCollectionFactory
 */
protected $categoryCollectionFactory;

/**
 * @param CategoryCollectionFactory $productCollectionFactory
 */
public function __construct(
    CategoryCollectionFactory $categoryCollectionFactory
) {
    $this->categoryCollectionFactory = $categoryCollectionFactory;
}

Create Collection

$collection = $this->categoryCollectionFactory
    ->create()
    ->addAttributeToSelect('*');

Filters

// Is Equal To
$collection->addAttributeToFilter('is_active', ['eq' => 1]);

// Is Not Equal To
$collection->addAttributeToFilter('is_active', ['neq' => 1]);

// Greater Than
$collection->addAttributeToFilter('level', ['gt' => 3]);

// Less Than
$collection->addAttributeToFilter('level', ['lt' => 3]);

// Greater Than or Equal To
$collection->addAttributeToFilter('entity_id', ['gteq' => 4]);

// Less Than or Equal To
$collection->addAttributeToFilter('entity_id', ['lteq' => 4]);

// Contains – with % wildcards
$collection->addAttributeToFilter('name', ['like' => 'DVD%']);

// Does Not Contain – with % wildcards
$collection->addAttributeToFilter('name', ['nlike' => 'ABC%']);

// In Array
$collection->addAttributeToFilter('entity_id', ['in' => [1,3,12]]);

// Not In Array
$collection->addAttributeToFilter('entity_id', ['nin' => [1,2,12]]);

// Is NULL
$collection->addAttributeToFilter('description', 'null');
$collection->addAttributeToFilter('description', ['null' => true]);

// Is Not NULL
$collection->addAttributeToFilter('description', 'notnull');
$collection->addAttributeToFilter('description', ['notnull' => true]);

Simple filter

$collection->addAttributeToFilter('attribute', $filter);

More complex filter

$collection->addAttributeToFilter(
    'attribute',
    ['in' => 'filter']
);

Or filter

$collection->addAttributeToFilter([
    [
        'attribute' => 'description',
        'null' => true,
    ],
    [
        'attribute' => 'description',
        'eq' => '',
    ],
]);
$collection->addFieldToFilter([
    ['description', 'description'],
    [
        ['is' => new \Zend_Db_Expr('null')],
        ['eq' => '']
    ],
]);

Filter by Category ID

$categoryId = 1; // int
$categoryId = "1,2,3"; // comma string
$categoryId = [1, 2, 3]; // array
$collection->addIdFilter($categoryId);

Filter by Store ID

$collection->setStore($storeId);
$collection->setStoreId($storeId); // AbstractCollection

Select Attribute

$collection->addAttributeToSelect($attributeCode);

Get first item

return $collection->getFirstItem();

Get raw attribute value

return $collection->getFirstItem()->getData($attributeCode);

Is Active Filter

$collection->addIsActiveFilter();

Return only IDs

return $collection->getAllIds();

Debug Query

echo (string) $collection->getSelect();
$collection->printLogQuery(true);

Add Paths Filter

$paths = "1/2/3";
$paths = ["1/2/3", "4/5/6"];
$collection->addPathsFilter($paths);

Level filter

$collection->addLevelFilter($level); // lteq $level
$collection->addRootLevelFilter(); // neq 1

Order By

$collection->addOrderField($field);

Add Website Names

$collection->addWebsiteNamesToResult();

Joins url rewrite rules to collection

$collection->joinUrlRewrite();

Generic Collection

// Sort Collection
// Order by Attribute Ascending
$collection->setOrder('price', 'ASC');

// Order by Attribute Descending
$collection->setOrder('name', 'DESC');

// Random Order
$collection->setOrder('rand()');

// Limit Collection
$collection->setPageSize(10);
$collection->setCurPage(1);

// Count Results
$collection->count();

Free Up Memory

$collection->clear();

Category Helper

Tree

use Magento\Catalog\Helper\Category as HelperCategory;

public function __construct(
    HelperCategory $helper
) {
    $this->helper = $helper;
}
/**
 * Retrieve current store categories
 * @param bool|string $sorted
 * @param bool $asCollection
 * @param bool $toLoad
 * @return \Magento\Framework\Data\Tree\Node\Collection or
 * \Magento\Catalog\Model\ResourceModel\Category\Collection or array
 */
$categories = $this->helper->getStoreCategories(false, true, true);

Get URL

$url = $this->helper->getCategoryUrl($category);

Can Show

$bool = $this->helper->canShow($category);

Category Usefuls

Get associated products

// @return \Magento\Framework\Data\Collection\AbstractDb
$category->getProductCollection();

URL

$category->getUrl();
$category->getImageUrl();

Parent

$category->getParentCategory();
$category->getParentId();
$category->getParentIds();

Children

/**
 * Retrieve children ids comma separated
 *
 * @param boolean $recursive
 * @param boolean $isActive
 * @param boolean $sortByPosition
 * @return string
 */
$category->getAllChildren(true, true, false);
$category->hasChildren(); // bool

Product Count

$category->getProductCount();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment