Files
wyczarujprezent.pl/xml/google-ads-feed.php
2024-10-28 22:14:22 +01:00

145 lines
5.6 KiB
PHP

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once('../config/config.inc.php');
require_once('../app/config/parameters.php');
require_once('../config/defines.inc.php');
require_once('../init.php');
class SimpleXMLElementExtended extends SimpleXMLElement
{
private function addCDataToNode(SimpleXMLElement $node, $value = '')
{
if ($domElement = dom_import_simplexml($node)) {
$domOwner = $domElement->ownerDocument;
$domElement->appendChild($domOwner->createCDATASection($value));
}
}
public function addChildWithCData($name = '', $value = '')
{
$newChild = parent::addChild($name);
if ($value) $this->addCDataToNode($newChild, $value);
return $newChild;
}
public function addCData($value = '')
{
$this->addCDataToNode($this, $value);
}
}
$iteration = filter_input(INPUT_GET, 'iteration', FILTER_VALIDATE_INT, ["options" => ["default" => 0, "min_range" => 0]]);
$context = Context::getContext();
const ID_LANG = 1; // PL
const LIMIT = 500;
$start = $iteration * LIMIT;
$file = 'google-ads-feed-' . $iteration . '.xml';
if (file_exists($file)) {
unlink($file);
}
$products_partial = Product::getProducts(ID_LANG, $start, LIMIT, 'id_product', 'DESC', false, true, $context);
$products = Product::getProductsProperties(ID_LANG, $products_partial);
function cleanHtml($html) {
$html = preg_replace('/<img[^>]+>/i', '', $html);
$html = strip_tags($html, '<p><br><ul><li><ol><strong><em>');
return trim($html);
}
function createProductXML($product, $combinations, SimpleXMLElementExtended $xml, Link $link)
{
$cover = Product::getCover($product['id_product']);
$product['id_image'] = $cover['id_image'];
$imagePath = $link->getImageLink($product['link_rewrite'], $product['id_image'], 'large_default');
$all_product_subs = Product::getProductCategoriesFull($product['id_product'], ID_LANG, 0, 2);
$all_product_subs_path = [];
foreach ($all_product_subs as $subcat) {
if ($subcat['id_category'] != $product['id_category_default']) {
$all_product_subs_path[] = strip_tags(Tools::getPath('', $subcat['id_category'], ''));
}
}
$productXml = $xml->addChild('entry');
$productXml->addChild('g:id', $product['id_product'], 'http://base.google.com/ns/1.0');
$productXml->addChildWithCData('g:title', $product['name'], 'http://base.google.com/ns/1.0');
$opis = $productXml->addChild('g:description');
$opis_czysty = cleanHtml($product['description']);
$opis->addCData($opis_czysty, 'http://base.google.com/ns/1.0');
$productXml->addChild('g:link', $product['link'], 'http://base.google.com/ns/1.0');
$productXml->addChild('g:image_link', 'https://' . $imagePath, 'http://base.google.com/ns/1.0');
$productXml->addChild('g:condition', $product['condition'], 'http://base.google.com/ns/1.0');
$productXml->addChild('g:availability', $product['quantity'] > 0 ? 'in stock' : 'out of stock', 'http://base.google.com/ns/1.0');
$price = Product::getPriceStatic($product['id_product'], true, null, 2, null, false, false);
$productXml->addChild('g:price', number_format($price, 2, '.', '') . ' PLN', 'http://base.google.com/ns/1.0');
$price_sale = Product::getPriceStatic($product['id_product'], true, null, 2, null, false, true);
if ($price_sale != $price) {
$productXml->addChild('g:sale_price', number_format($price_sale, 2, '.', '') . ' PLN', 'http://base.google.com/ns/1.0');
}
$productXml->addChild('g:quantity', $product['quantity'], 'http://base.google.com/ns/1.0');
if (!empty($product['ean13'])) {
$productXml->addChild('g:gtin', $product['ean13'], 'http://base.google.com/ns/1.0');
}
$productXml->addChild('g:brand', $product['manufacturer_name'], 'http://base.google.com/ns/1.0');
foreach ($all_product_subs_path as $category) {
$productXml->addChild('g:product_type', $category, 'http://base.google.com/ns/1.0');
}
foreach ($combinations as $combination) {
if (!empty($combination['gsize'])) {
$productXml->addChild('g:size', $combination['gsize'], 'http://base.google.com/ns/1.0');
}
}
}
$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'));
$link = new Link();
if ( count( $products ) )
{
foreach ($products as $product) {
$product_tmp = new Product($product['id_product']);
$combinations = $product_tmp->getAttributeCombinations(ID_LANG, true);
$product_combinations = [];
foreach ($combinations as $com) {
$id_product_attribute = $com['id_product_attribute'];
if (!isset($product_combinations[$id_product_attribute])) {
$product_combinations[$id_product_attribute] = [
'id_product_attribute' => $id_product_attribute,
'quantity' => Product::getQuantity($product['id_product'], $id_product_attribute),
'reference' => $com['reference'],
'gsize' => null
];
}
if (in_array($com['group_name'], ['Rozmiar', 'Rodzaj', 'Size'])) {
$product_combinations[$id_product_attribute]['gsize'] = $com['attribute_name'];
}
}
createProductXML($product, $product_combinations, $xml, $link);
}
$xml->asXML($file);
echo "Limit: " . LIMIT . "<br>";
echo "Start: " . $start . "<br>";
echo "File: https://wyczarujprezent.pl/xml/" . $file . "<br>";
}
else
{
echo 'No products found';
}