Skip to content

Instantly share code, notes, and snippets.

@dannevang
Forked from julesjanssen/gist:1017978
Created March 21, 2016 12:36
Show Gist options
  • Save dannevang/6e1c12973667c1861e4a to your computer and use it in GitHub Desktop.
Save dannevang/6e1c12973667c1861e4a to your computer and use it in GitHub Desktop.
Magento XML productfeed voor Beslist.nl
<?php
/**
*
* @license MIT License
*
*/
// om 't script wat tijd te geven
ini_set("memory_limit","320M");
ini_set("max_execution_time", 240);
set_time_limit(240);
$time_start = microtime(true);
$cachetime = 24 * 60 * 60; //1 dag
$verzendkosten = 5.95;
$maxproducts = 0;
ob_start();
// standaard Magento spul
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);
// indien je meer winkels hebt, stel hier (op basis van $_SERVER['MAGE_RUN_CODE']) de juiste storeID in
$storeID = 1;
$cachefile = dirname(__FILE__) .'/var/cache/'. md5('beslistproductfeed'. $storeID);
$filetime = is_file($cachefile) ? filemtime($cachefile) : 0;
if((time() - $cachetime) < $filetime){
header("Content-type: application/xml; charset=utf-8");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
readfile($cachefile);
}else{
// init db connectie
$pdo = new PDO(
'mysql:host=localhost;dbname=databasenaam',
'databaseuser',
'databasepass',
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
PDO::ATTR_CASE => PDO::CASE_LOWER
)
);
header("Content-type: application/xml; charset=utf-8");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo '<?xml version="1.0" encoding="utf-8"?>
<Products>';
$sql = "SELECT
p.entity_id,
p.sku,
p.type_id
FROM
catalog_product_entity p
LEFT JOIN
catalog_product_website w ON w.product_id = p.entity_id
WHERE
w.website_id = ". $storeID ."
ORDER BY
p.entity_id ASC";
if($maxproducts){
$sql .= "
LIMIT ". (int) $maxproducts;
}
$sth = $pdo->prepare($sql);
$sth->execute();
$products = $sth->fetchAll();
foreach($products as $product){
$sql = "SELECT
*
FROM
catalog_product_flat_". $storeID ."
WHERE
entity_id = ". $product['entity_id'];
$sth = $pdo->prepare($sql);
$sth->execute();
$flatproduct = $sth->fetch();
// product niet gevonden of niet zichtbaar
if($flatproduct == false || $flatproduct['visibility'] < 1) continue;
$special_from = !empty($flatproduct['special_from_date']) ? strtotime($flatproduct['special_from_date']) : 0;
$special_to = !empty($flatproduct['special_to_date']) ? strtotime($flatproduct['special_to_date']) : 0;
$price = (float) $flatproduct['price'];
if( !empty($flatproduct['special_price']) &&
floatVal($flatproduct['special_price']) < $price &&
$special_from <= time() &&
($special_to >= time() || $special_to == 0)
){
$price = (float) $flatproduct['special_price'];
}
$sql = "SELECT
c.entity_id,
c.name
FROM
catalog_category_flat_store_". $storeID ." c
LEFT JOIN
catalog_category_product cp ON cp.category_id = c.entity_id
WHERE
cp.product_id = ". $product['entity_id'] ." AND
c.parent_id <> 1
ORDER BY
c.level ASC";
$sth = $pdo->prepare($sql);
$sth->execute();
$categories = $sth->fetchAll();
$merk = $categories[0]['name'];
$categorie = $categories[1]['name'];
echo '
<Product>
<Categorie>'. htmlspecialchars($categorie) .'</Categorie>
<Merk>'. htmlspecialchars($merk) .'</Merk>
<Titel>'. htmlspecialchars($flatproduct['name']) .'</Titel>
<Omschrijving>'. htmlspecialchars($flatproduct['short_description']) .'</Omschrijving>
<Image>'. $basepath .'media/catalog/product'. $flatproduct['small_image'] .'</Image>
<Deeplink>'. $basepath . $flatproduct['url_path'] .'</Deeplink>
<Prijs>'. number_format($price, 2, '.', ',') .'</Prijs>
<Verzendkosten>'. number_format($verzendkosten, 2, '.', ',') .'</Verzendkosten>
<Levertijd>Op werkdagen voor 16.00 uur besteld, de volgende dag in huis</Levertijd>
<Productcode>'. $product['sku'] .'</Productcode>
</Product>';
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo '
<!-- '. number_format($time, 4) .' -->
<!-- count '. count($products) .' -->
</Products>';
$data = ob_get_clean();
file_put_contents($cachefile, $data);
echo $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment