Files
wyczarujprezent.pl/xml/csv-generate.php
2024-10-28 22:14:22 +01:00

188 lines
6.3 KiB
PHP

<?php
ini_set('display_errors', 'on');
error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT ^ E_WARNING ^ E_DEPRECATED);
include('../config/config.inc.php');
include('../app/config/parameters.php');
include('../config/defines.inc.php');
include( '../init.php' );
class SimpleXMLElementExtended extends SimpleXMLElement
{
/**
* Add value as CData to a given XML node
*
* @param SimpleXMLElement $node SimpleXMLElement object representing the child XML node
* @param string $value A text to add as CData
* @return void
*/
private function addCDataToNode(SimpleXMLElement $node, $value = '')
{
if ($domElement = dom_import_simplexml($node))
{
$domOwner = $domElement->ownerDocument;
$domElement->appendChild($domOwner->createCDATASection("{$value}"));
}
}
/**
* Add child node with value as CData
*
* @param string $name The child XML node name to add
* @param string $value A text to add as CData
* @return SimpleXMLElement
*/
public function addChildWithCData($name = '', $value = '')
{
$newChild = parent::addChild($name);
if ($value) $this->addCDataToNode($newChild, "{$value}");
return $newChild;
}
/**
* Add value as CData to the current XML node
*
* @param string $value A text to add as CData
* @return void
*/
public function addCData($value = '')
{
$this->addCDataToNode($this, "{$value}");
}
}
$context = Context::getContext();
$id_lang = (int)Context::getContext()->language->id;
$start = 0;
$limit = '';
$order_by = 'id_product';
$order_way = 'DESC';
$id_category = false;
$only_active = true;
$products_partial = Product::getProducts($id_lang, $start, $limit, $order_by, $order_way, $id_category, $only_active, $context);
$products = Product::getProductsProperties($context->language->id, $products_partial);
$xml = new SimpleXMLElementExtended('<?xml version="1.0"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0"/>');
$xml->addChild('title', 'Redline');
$xml->addChild('updated', date('Y-m-d'));
foreach ( $products as $product )
{
$product_combinations = false;
$combination_tmp = false;
$product_csv = false;
$product_tmp = new Product( $product['id_product'] );
$combinations = $product_tmp -> getAttributeCombinations( $context -> language -> id, true );
foreach ( $combinations as $com )
{
$combination_tmp[ $com['id_product_attribute'] ][] = $com;
}
foreach ( $combination_tmp as $key => $val )
{
$product_com['id_product_attribute'] = $key;
$product_com['quantity'] = Product::getQuantity( $product['id_product'], $key );
$product_com['reference'] = $val[0]['reference'];
foreach ( $val as $row )
{
if ( $row['group_name'] == 'Rozmiar' )
$product_com['gsize'] = $row['attribute_name'];
if ( $row['group_name'] == 'Rodzaj' )
$product_com['gsize'] = $row['attribute_name'];
}
$product_combinations[] = $product_com;
}
if ( is_array( $product_combinations ) and count ( $product_combinations ) )
{
foreach ( $product_combinations as $product_combination_tmp )
{
$product_csv = false;
$cover = Product::getCover($product['id_product']);
$products['id_image'] = $cover['id_image'];
$link = new Link;
$product_csv['id'] = $product['id_product'] . $product_combination_tmp['id_product_attribute'];
$product_csv['title'] = $product['name'];
$product_csv['description'] = str_replace( array("\n", "\r"), '', strip_tags( $product['description_short'] ) );
if ( $product_combination_tmp['quantity'] > 0 )
$product_csv['availability'] = 'in stock';
else
$product_csv['availability'] = 'out of stock';
$product_csv['inventory'] = $product_combination_tmp['quantity'];
$product_csv['condition'] = $product['condition'];
$price = Product::getPriceStatic($product['id_product'], true, null, 2, null, false, false);
$price_sale = Product::getPriceStatic($product['id_product'], true, $product_combination_tmp['id_product_attribute'], 2, null, false, true);
if ( (float)$price_sale != 0 and $price_sale != $price )
$product_csv['price'] = $price_sale;
else
$product_csv['price'] = $price;
$product_csv['link'] = $product['link'];
$product_csv['image_link'] = 'https://' . $link -> getImageLink( $product['link_rewrite'], $products['id_image'], 'large_default' );
$product_csv['brand'] = $product['manufacturer_name'];
$product_csv['google_product_category'] = '';
if ( $product_combination_tmp['gsize'] != null)
$product_csv['size'] = $product_combination_tmp['gsize'];
}
if ( $product_csv['id'] )
$products_csv[] = $product_csv;
}
else
{
$price = Product::getPriceStatic($product['id_product'], true, null, 2, null, false, false);
$cover = Product::getCover($product['id_product']);
$products['id_image'] = $cover['id_image'];
$link = new Link;
$product_csv['id'] = $product['id_product'];
$product_csv['title'] = $product['name'];
$product_csv['description'] = str_replace( array("\n", "\r"), '', strip_tags( $product['description_short'] ) );
if ( $product_combination_tmp['quantity'] > 0 )
$product_csv['availability'] = 'in stock';
else
$product_csv['availability'] = 'out of stock';
$product_csv['inventory'] = $product_combination_tmp['quantity'];
$product_csv['condition'] = $product['condition'];
$price = Product::getPriceStatic($product['id_product'], true, null, 2, null, false, false);
$price_sale = Product::getPriceStatic($product['id_product'], true, $product_combination_tmp['id_product_attribute'], 2, null, false, true);
if ( (float)$price_sale != 0 and $price_sale != $price )
$product_csv['price'] = $price_sale;
else
$product_csv['price'] = $price;
$product_csv['link'] = $product['link'];
$product_csv['image_link'] = 'https://' . $link -> getImageLink( $product['link_rewrite'], $products['id_image'], 'large_default' );
$product_csv['brand'] = $product['manufacturer_name'];
$product_csv['google_product_category'] = '';
if ( $product_csv['id'] )
$products_csv[] = $product_csv;
}
}
// echo '<pre>' . print_r( $products_csv, true ); exit;
if ( $products_csv )
{
$fp = fopen( 'csv-products.csv', 'w');
fprintf( $fp, "\xEF\xBB\xBF");
foreach( $products_csv as $line )
fputcsv( $fp, $line );
fclose( $fp );
}