Search code examples
prestashopprestashop-1.6

Create XML export feed in PrestaShop


can someone direct me to some documentation how to create custom XML feed in PrestaShop 1.6+. I searched official documentation but I didn't find what I needed.

The task is easy - create custom XML feed from which can other e-shop takes the products.


Solution

  • You can get list of products using getProducts() then use SimpleXMLElement for generating xml.

    include('config/config.inc.php');
    include('init.php'); 
    $productObj = new Product();
    $products = $productObj -> getProducts($id_lang, 0, 0, 'id_product', 'DESC' );
    
    $xml = new SimpleXMLElement('<xml/>');
    foreach($products as $product) {
    $productXml = $xml->addChild('product');
    $productXml->addChild('id', $product->id);
    $productXml->addChild('name', $product->name);
    $productXml->addChild('description', $product->description);
    }
    Header('Content-type: text/xml');
    print($xml->asXML());
    

    Output will be..

    <xml>
       <product>
             <id>ID</id>
             <name>NAME</name>
             <description>DESCRIPTION</description>
       </product>
       <product>
             <id>ID</id>
             <name>NAME</name>
             <description>DESCRIPTION</description>
       </product>
       ...
       ...
       ...
    </xml>
    

    see function getProducts() description in classes/Product.php to know about the parameters.

    /**
    * Get all available products
    *
    * @param integer $id_lang Language id
    * @param integer $start Start number
    * @param integer $limit Number of products to return
    * @param string $order_by Field for ordering
    * @param string $order_way Way for ordering (ASC or DESC)
    * @return array Products details
    */
    public static function getProducts($id_lang, $start, $limit, $order_by, $order_way, $id_category = false,
        $only_active = false, Context $context = null) {...}
    

    You can place xml.php file with in your prestashop root directory and can access this xml by visiting or sending request to yourdomain.com/xml.php.

    Or if you want to create your module for xml then you need to place the code in your front controller and then you can access the xml file by visiting yourdomain.com/index.php?fc=module&module=<ModuleName>&controller=<XMLFunction>. Read Prestashop Documentation to know more about prestashop module structure.