protected function getProductCountAttribute()
    {
        $iProductCount = $this->getAttribute('product_count');
        if ($iProductCount !== null) {
            return $iProductCount;
        }

        //Get product count from cache
        $arCacheTag = [static::class];
        $sCacheKey = 'product_count_'.$this->id;

        $iProductCount = CCache::get($arCacheTag, $sCacheKey);
        if ($iProductCount !== null) {
            return $iProductCount;
        }

        //Calculate product count from child categories
        $iProductCount = 0;
        $obChildCategoryCollect = $this->children;
        if ($obChildCategoryCollect->isNotEmpty()) {
            /** @var CategoryItem $obChildCategoryItem */
            foreach ($obChildCategoryCollect as $obChildCategoryItem) {
                if ($obChildCategoryItem->isEmpty()) {
                    continue;
                }

                $iProductCount += $obChildCategoryItem->product_count;
            }
        }

        $iProductCount += ProductCollection::make()->active()->category($this->id)->count();

        CCache::forever($arCacheTag, $sCacheKey, $iProductCount);
        $this->setAttribute('product_count', $iProductCount);

        return $iProductCount;
    }
protected function getProductCountAttribute()
    {
        $iProductCount = $this->getAttribute('product_count');
        if ($iProductCount !== null) {
            return $iProductCount;
        }

        //Get product count from cache
        $arCacheTag = [static::class];
        $sCacheKey = 'product_count_'.$this->id;

        $iProductCount = CCache::get($arCacheTag, $sCacheKey);
        if ($iProductCount !== null) {
            return $iProductCount;
        }

        //Calculate product count from child categories
        $iProductCount = 0;
        $obChildCategoryCollect = $this->children;
        if ($obChildCategoryCollect->isNotEmpty()) {
            /** @var CategoryItem $obChildCategoryItem */
            foreach ($obChildCategoryCollect as $obChildCategoryItem) {
                if ($obChildCategoryItem->isEmpty()) {
                    continue;
                }

                $iProductCount += $obChildCategoryItem->product_count;
            }
        }

        $iProductCount += ProductCollection::make()->active()->category($this->id)->count();

        CCache::forever($arCacheTag, $sCacheKey, $iProductCount);
        $this->setAttribute('product_count', $iProductCount);

        return $iProductCount;
    }
protected function getProductCountAttribute ( ) { $ iProductCount = $ this -> getAttribute ( 'product_count' ) ; if ( $ iProductCount !== null ) { return $ iProductCount ; } //Get product count from cache $ arCacheTag = [ static :: class ] ; $ sCacheKey = 'product_count_' . $ this -> id ; $ iProductCount = CCache :: get ( $ arCacheTag , $ sCacheKey ) ; if ( $ iProductCount !== null ) { return $ iProductCount ; } //Calculate product count from child categories $ iProductCount = 0 ; $ obChildCategoryCollect = $ this -> children ; if ( $ obChildCategoryCollect -> isNotEmpty ( ) ) { /** @var CategoryItem $obChildCategoryItem */ foreach ( $ obChildCategoryCollect as $ obChildCategoryItem ) { if ( $ obChildCategoryItem -> isEmpty ( ) ) { continue ; } $ iProductCount += $ obChildCategoryItem -> product_count ; } } $ iProductCount += ProductCollection :: make ( ) -> active ( ) -> category ( $ this -> id ) -> count ( ) ; CCache :: forever ( $ arCacheTag , $ sCacheKey , $ iProductCount ) ; $ this -> setAttribute ( 'product_count' , $ iProductCount ) ; return $ iProductCount ; }
Get product count for category
@return int
Get product count for category