Search code examples
feedopencartcategoriesproduct

OpenCart XML feed - display only products from specific category


After a few days I gave up on this - I couldn't figure it out.

This is the relevant part of my OpenCart XML feed code:

Code:

     $this->load->model('catalog/category');

     $this->load->model('catalog/product');

     $this->load->model('tool/image');

     $products = $this->model_catalog_product->getProducts();

     foreach ($products as $product) {
        if ($product['description']) {
           $output .= '<PRODUCT>';
           $output .= '<NAME>' . $product['name'] . '</NAME>';
           $output .= '<DESCRIPTION>' . $product['description'] . '</DESCRIPTION>';
           $output .= '</PRODUCT>';

I would like to be able to display only products from certain categories for example by category_id.

Let's say, that I wanted to display products from category (id) 1 and 2, what changes should be made?

I'm using OpenCart 1.5.1.3


Solution

  • If you looked at the getProducts() function, you would see that it accepts array as an argument. You can pass filter parameters to it, and get products for specific category, manufacturer etc. Params are:

    filter_category_id
    filter_sub_category
    filter_filter
    filter_name
    filter_tag
    filter_description
    filter_manufacturer_id
    

    [EDIT: Based on comment 1: EXAMPLE]

    Create an array that you want to pass as an argument, for example (to get all products from category id 10 and it's sub categories)

    $data = array(
        'filter_category_id' => '10',
        'filter_sub_category' => 'true'
    );
    

    Then call

    getProducts($data);
    

    or do it inline

    getProducts(array('filter_category_id' => '10'));
    

    If you need more than one category, get them separately then join them:

    $set1 = $this->model_catalog_product->getProducts(array('filter_category_id' => '10'));
    $set2 = $this->model_catalog_product->getProducts(array('filter_category_id' => '11'));
    $products = $set1 + $set2;
    ...