ProductCategory::ProductsShowable PHP Method

ProductsShowable() public method

Retrieve a set of products, based on the given parameters. Checks get query for sorting and pagination.
public ProductsShowable ( boolean $recursive = true ) : PaginatedList
$recursive boolean include sub-categories
return PaginatedList
    public function ProductsShowable($recursive = true)
    {
        // Figure out the categories to check
        $groupids = array($this->ID);
        if (!empty($recursive) && self::config()->include_child_groups) {
            $groupids += $this->AllChildCategoryIDs();
        }
        $products = Product::get()->leftJoin('Product_ProductCategories', '"Product_ProductCategories"."ProductID" = "Product"."ID"')->filterAny(array('ParentID' => $groupids, 'Product_ProductCategories.ProductCategoryID' => $groupids));
        if (self::config()->must_have_price) {
            if (Product::has_extension('ProductVariationsExtension')) {
                $products = $products->filterAny(array("BasePrice:GreaterThan" => 0, "Variations.Price:GreaterThan" => 0));
            } else {
                $products = $products->filter("BasePrice:GreaterThan", 0);
            }
        }
        $this->extend('updateProductsShowable', $products);
        return $products;
    }

Usage Example

 public function testZeroPriceWithVariations()
 {
     Config::inst()->update('ProductCategory', 'must_have_price', true);
     $products = $this->electronics->ProductsShowable();
     $this->assertEquals(0, $products->count(), 'No product should be returned as there\'s no price set');
     // Create a variation for HDTV
     ProductVariation::create(array('InternalItemID' => '50-Inch', 'Price' => 1200, 'ProductID' => $this->hdtv->ID))->write();
     $products = $this->electronics->ProductsShowable();
     $this->assertDOSEquals(array(array('URLSegment' => 'hdtv')), $products, 'HDTV has a priced extension and should now show up in the list of products');
 }