Show Products From Category Using Perch Block

  • I have a tricky one...


    I would like to show the products from a certain category. However I would like the functionality to live inside a perch:block.


    The user creates the block, the block asks for the category, then shows the products in that category on the front end.


    Is this possible?

  • Hi Tony,


    Yes, this is possible. One approach is to use layout includes inside the template and pass layout variables:


    HTML
    1. <perch:layout
    2. path="products"
    3. categories="<perch:categories id="category" set="products" label="Category"><perch:category id="catPath" append=","></perch:categories>"
    4. >


    Given that layouts are rendered at a later phase than normal content, when Perch renders them it will see the tag as something like:


    HTML
    1. <perch:layout
    2. path="products"
    3. categories="products/shoes/,products/shirts/,"
    4. >


    So the above is the equivalent to:


    PHP
    1. perch_layout('products', [
    2. 'categories' => 'products/shoes/,products/shirts/,'
    3. ]);



    Inside your templates/layouts/product.php, you can access the categories with perch_layout_var():