1

I have a website with about 200 accessories for a certain type some cars, the accessories are set up as normal products within Magento. However, the cars are stored in a new table within our Magento 2 database and we wish to list these within the website through the CMS pages.

. . I've created 7 empty categories to run alongside my shop:

  • Cars (ID 1)
    • SUV’s (ID 2)
      • Brand A (ID 3)
      • Brand B (ID 4)
    • 4X4 (ID 5)
      • Brand A (ID 6)
      • Brand B (ID 7)

Within design/frontend/myvendor/mytheme/Magento_Catalog/layout, I've then created xml files for those categories to call:

  • catalog_category_view_id_3.xml
  • catalog_category_view_id_4.xml
  • catalog_category_view_id_6.xml
  • catalog_category_view_id_7.xml

Each file contains code similar to this:

<view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/view.xsd">
    <referenceContainer name="main.content">
            <block class="Magento\Framework\View\Element\Template" 
                   name="guns.ddcat1" 
                   before="-" 
                   template="Magento_Catalog::html/all-cars.phtml"
            />
        </referenceContainer>
</view>

Within design/frontend/myvendor/mytheme/Magento_Catalog/templates/html we have a file called all-cars.html whereby it connects to the external database and then displays all the cars for that category at the following URL’s

  • example.com/cars/suv/brand-a/
  • example.com/cars/suv/brand-b/
  • example.com/cars/4x4/brand-a/
  • example.com/cars/4x4/brand-b/

That works fine up to this point and all cars are displayed on the correct pages. The following part is where I’m stuck…

I now need to view each car in detail. As the car list changes on a daily basis, we need dynamic pages, so I was going to make each car clickable…

  • example.com/cars/suv/brand-a/car1
  • example.com/cars/suv/brand-a/car2
  • example.com/cars/suv/brand-a/car3

I struggled with this, so as a simpler (but ugly) approach, I thought I could append the category with a variable:

  • example.com/cars/suv/brand-a/?carID=111
  • example.com/cars/suv/brand-a/?carID=112
  • example.com/cars/suv/brand-a/?carID=113

Then in all-cars.phtml, we could then listen out for it using the following code:

if(isset($_GET[‘carID'])) {
    //connect to database and show specific car
} else {
    //connect to database and show all cars
}

However, when I try this, the carID= is treated as a new sub-category, hence catalog_category_view_id_3.xml is not listened to and as a result, all-cars.phtml is not called whatsoever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.