Search code examples
phpopencartfeed

Opencart Feed with product id and options


Any help would be very appreciated!

I want to create an new feed that wil export the product id and the option desctiptions that the product have.

Example:

<feed>
    <product>
        <product_id>001</product_id>
        <options>
            <option>option1</option>
            <option>option2</option>
            <option>option3</option>
        </options>
    </product>
</feed>

My code is:

<?php
class ControllerFeedOptions extends Controller {
   public function index() {
       $output  = '<?xml version="1.0" encoding="UTF-8"?>';
       $output .= '<feed xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

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

       $products = $this->model_catalog_product->getProducts();
       $options = $this->model_catalog_product->getProductOptions($product_id);
       foreach ($products as $product) {
         $output .= '<product>';
         $output .= '<product_id>' . $product['product_id'] . '</product_id>';
         $output .= '<options>';
         $output .= '<option>' . $options['name'] . '</option>';
         $output .= '</options>';
         $output .= '</product>';   
       }

       $output .= '</feed>';

       $this->response->addHeader('Content-Type: application/xml');
       $this->response->setOutput($output);

   }

}
?>

But the options are not displaying. It is clear that the options need some more coding. Can you help me with that?


Solution

  • When looping through products you also need to do additional loop through the product options.

        $products = $this->model_catalog_product->getProducts();
        foreach ($products as $product) {
            $output .= '  <product>';
            $output .= '    <product_id>' . $product['product_id'] . '</product_id>';
            $output .= '    <options>';
            $options = $this->model_catalog_product->getProductOptions($product['product_id']);
            foreach ($options as $option) {
                $output .= '      <option>';
                $output .= '        <name>' . $option['name'] . '</name>';
                $output .= '        <values>';
                foreach ($option['option_value'] as $value) {
                    $output .= '          <value>' . $value['name'] . '</value>';
                }
                $output .= '        </values>';
                $output .= '      </option>';
            }
            $output .= '    </options>';
            $output .= '  </product>';   
        }
    

    Try this code.

    Edit: Because one option can have multiple values and one product can have multiple options, you would need to loop even deeper. Meaning that even the XML feed structure would need to be deeper as well.

    I updated the code above to return the requested output.

    BTW: I do not know who the we is but if you (all) have no PHP nor OpenCart development skills I can only recommend to hire at least one PHP (and better with OpenCart knowledge) developer.