Jump to content

Recommended Posts

Hello I have a slight problem with an php procedure for exporting my products to an xml file.
 
First of all i want to make it a module so it is save to use,
second how can i store the outcome to an xml file on the server and
last is it possible to leave out some categorys.
 
The php file is included. Who can help me with this?
 
Greetz Willem
 
 

<?php
/* 
Prodedure for extracting products to an XML file 
   2014 - Willem Palm 
*/
   
include(dirname(__FILE__).'/config/config.inc.php');
require_once(dirname(__FILE__).'/init.php');

// $confPasswd = 'test';

$tags = array(
  'name' => 'Titel',
  'url' => 'Product-url',
  'imageUrl' => 'URL-Afbeelding',
  'price_inc' => 'Prijs',
  'description' => 'Omschrijving',
  'lager' => 'Voorraad',
  'reference' => 'Artikelcode',
  'categoryName' => 'category',
  'subCategoryName' => 'subCategory',
  
  );


function my_strip_tags($t)
{
  $t = preg_replace('/</',' <',$t);
  $t = preg_replace('/>/','> ',$t);
  $t = preg_replace('/[\n\r\t]/',' ',$t);
  $t = preg_replace('/  /',' ',$t); 
  return strip_tags($t);
}

function round_up ($value, $places=0) {
  if ($places < 0) { $places = 0; }
  $mult = pow(10, $places);
  return ceil($value * $mult) / $mult;
 }

function print_field($k, $v)
{
  global $tags;
  if (isset($tags[$k])) {
    $k = $tags[$k];
    $v = htmlspecialchars($v, ENT_NOQUOTES, "UTF-8");
    print "    <$k>$v</$k>\n";
  }
}


header("Content-Type:text/xml; charset=utf-8");
print '<?xml version="1.0" encoding="UTF-8"?>'."\n";
print "<products>\n";
$configuration = Configuration::getMultiple(array(
  'PS_LANG_DEFAULT', 
  'PS_SHIPPING_FREE_PRICE',
  'PS_SHIPPING_HANDLING',
  'PS_SHIPPING_METHOD', 
  'PS_SHIPPING_FREE_WEIGHT',
  'PS_CARRIER_DEFAULT'));
$id_lang = intval($configuration['PS_LANG_DEFAULT']);
$id_zone = intval($defaultCountry->id_zone);
$link = new Link();
$id_carrier = $configuration['PS_CARRIER_DEFAULT'];
$carrier = new Carrier(intval($id_carrier));
$carrierTax = 0;
if ($carrier->id_tax) {
  $tax = new Tax(intval($carrier->id_tax));
  if (Validate::isLoadedObject($tax) AND Tax::zoneHasTax(intval($tax->id), intval($id_zone)) AND !Tax::excludeTaxeOption())
    $carrierTax = $tax->rate;
}
$passwd = Tools::getValue('passwd');
$categories = Category::getCategories($id_lang, true, false);
$manufacturers = Manufacturer::getManufacturers(false, $id_lang);
if ($passwd == $confPasswd) {
  $products = Product::getProducts($id_lang, 0, 0, 'price', 'ASC', false, true);
}
else {
  $products = array();
}
$manMap =array();
foreach ($manufacturers as $manufacturer) {
  $manMap[$manufacturer['id_manufacturer']] = $manufacturer;
}
$catMap =array();
foreach ($categories as $category) {
  $catMap[$category['id_category']] = $category;
}
foreach ($products as $product) {
  $prodObj = new Product($product['id_product'], false, $id_lang);
  $product['url'] = $link->getProductLink($prodObj);
  $cover = Product::getCover($product['id_product']);
  $product['imageUrl'] = $link->getImageLink($prodObj->link_rewrite, $product['id_product'].'-'.$cover['id_image'], 'large');
  if (intval($configuration['PS_SHIPPING_METHOD']))
    $shipping_cost = $carrier->getDeliveryPriceByWeight($product['weight'], $id_zone);
  else
    $shipping_cost = $carrier->getDeliveryPriceByPrice($product['price'], $id_zone);
    $shipping_cost *= 1 + ($carrierTax / 100);
    $shipping_cost += floatval($configuration['PS_SHIPPING_HANDLING']);
    $product['shipping_cost'] = $shipping_cost;
    $product['price_inc'] = round_up($product['price'],2) * (1 + $product['tax_rate'] / 100);
    $product['manufacturerName'] = $manMap[$product['id_manufacturer']]['name'];
    $product['description_short'] = my_strip_tags($product['description_short']);
    $product['leveringstid'] = ($carrier->delay[4]);
    $product['lager'] = $product['quantity'];


  //print "  <product>\n";
  $product_num++;
  echo '<product place="'.$product_num.'">'."\n";
  foreach ($product as $k => $v) {
    print_field($k, $v);
  }
  $catList = Product::getIndexedCategories($product['id_product']);
  foreach ($catList as $cat) {
    $catNames = array();
    $catId = $cat['id_category'];
    while ($catId && $catId != 1) {
      array_unshift($catNames, trim($catMap[$catId]['name']));
      $catId = $catMap[$catId]['id_parent'];
    }
    print_field('categoryName', implode(' | ', $catNames));
  }
  print "  </product>\n";
}
print "</products>\n";

?>

 
xml_export.php

Edited by Willem (see edit history)

Share this post


Link to post
Share on other sites

First of all i want to make it a module so it is save to use,

 

Hi, to create module, you can help you to the prestashop documentation http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module

 

second how can i store the outcome to an xml file on the server and

 

To save file, you must write into file with php method : file_put_contents for example.

Acually, you change the header page and output content to browsing page.

 

last is it possible to leave out some categorys.

 

Yes but you must filter products list before or filter your result array during the foreach.

 

Regards

Share this post


Link to post
Share on other sites
  • 2 months later...

Hi, to create module, you can help you to the prestashop documentation http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module

 

 

To save file, you must write into file with php method : file_put_contents for example.

Acually, you change the header page and output content to browsing page.

 

 

Yes but you must filter products list before or filter your result array during the foreach.

 

Regards

 

Hi, in that script, I never go further than:

$products = Product::getProducts($id_lang, 0, 0, 'price', 'ASC', false, true);

I get timeout. Any tip? TIA

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More