コード例 #1
0
    /**
     * Used by export.php to build the feed required by Twenga.
     * See detailed comments in the body of the method
     * @see Twenga::preparedValues() to see how needed tags for feed are filled
     */
    public function buildXML()
    {
        // this check if the module is installed and if the site is registered at Twenga
        $bool_site_exists = true;
        if (self::$obj_twenga->getHashkey() === NULL) {
            $this->_errors[] = $this->l('The hash key must be set for used Twenga API.');
            $bool_site_exists = false;
        }
        if ($bool_site_exists) {
            try {
                $bool_site_exists = self::$obj_twenga->siteExist();
            } catch (Exception $e) {
                $this->_errors[] = $e->getMessage() . $this->l('Some parameters missing, or the site doesn\'t exist');
                $bool_site_exists = false;
            }
        }
        if (!$bool_site_exists) {
            return $this->displayErrors();
        }
        // Now method build the XML
        $xmlstr = '<?xml version="1.0" encoding="utf-8"?><catalog></catalog>';
        $xml = new SimpleXMLElement($xmlstr);
        $parameters = Configuration::getMultiple(array('PS_REWRITING_SETTINGS', 'PS_LANG_DEFAULT', 'PS_SHIPPING_FREE_PRICE', 'PS_SHIPPING_HANDLING', 'PS_SHIPPING_METHOD', 'PS_SHIPPING_FREE_WEIGHT', 'PS_COUNTRY_DEFAULT'));
        $lang = (int) $parameters['PS_LANG_DEFAULT'];
        $language = new Language($lang);
        $carrier = new Carrier(Configuration::get('PS_CARRIER_DEFAULT'), $language->id);
        $defaultCountry = new Country(Configuration::get('PS_COUNTRY_DEFAULT'), $language->id);
        $link = new Link();
        $result = Db::getInstance()->ExecuteS('
		SELECT `id_product` FROM `' . _DB_PREFIX_ . 'product` WHERE `active` = 1');
        foreach ($result as $k => $row) {
            $product = new Product((int) $row['id_product']);
            if (Validate::isLoadedObject($product) and $product->active) {
                // Check if product declinations exist.
                $combinations = $this->getCombinations($product, $lang);
                // Set an empty value even no combinations was found to make foreach usable.
                if (empty($combinations)) {
                    $combinations[] = array();
                }
                foreach ($combinations as $combination) {
                    // prepared values before insert it in node structure.
                    // In this way we can structure code with checking method and displaying method for more lisibility.
                    $product_values = $this->preparedValues($product, $combination, $lang, $link, $carrier);
                    // create the product node for each products and declinations
                    $product_node = $xml->addChild('product', '');
                    // required Fields
                    $product_node->addChild('product_url', $product_values['product_url']);
                    $product_node->addChild('designation', '<![CDATA[' . $product_values['designation'] . ']]>');
                    $product_node->addChild('price', $product_values['price']);
                    $product_node->addChild('category', '<![CDATA[' . $product_values['category'] . ']]>');
                    $product_node->addChild('image_url', $product_values['image_url']);
                    $product_node->addChild('description', '<![CDATA[' . $product_values['description'] . ']]>');
                    $product_node->addChild('brand', $product_values['brand']);
                    // optionnals fields
                    $product_node->addChild('merchant_id', $product_values['merchant_id']);
                    $product_node->addChild('manufacturer_id', $product_values['manufacturer_id']);
                    $product_node->addChild('shipping_cost', $product_values['shipping_cost']);
                    $product_node->addChild('in_stock', $product_values['in_stock']);
                    $product_node->addChild('stock_detail', $product_values['stock_detail']);
                    $product_node->addChild('condition', $product_values['condition']);
                    $product_node->addChild('upc_ean', $product_values['upc_ean']);
                    $product_node->addChild('product_type', $product_values['product_type']);
                    $product_node->addChild('isbn', $product_values['isbn']);
                    $product_node->addChild('eco_tax', $product_values['eco_tax']);
                }
            }
        }
        $str_xml = $xml->asXML();
        $str_xml = str_replace(array('&lt;![CDATA[', ']]&gt;', '&#13;'), array('<![CDATA[', ']]>', ''), $str_xml);
        return $str_xml;
    }