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

210 lines
7.2 KiB
PHP

<?php
// Załaduj konfigurację PrestaShop
require_once 'config/config.inc.php';
require_once 'init.php';
// Ustaw ścieżkę do pliku XML, który zostanie wygenerowany
$xmlFilePath = 'google_merchant_feed.xml';
// Pobierz wszystkie produkty z PrestaShop
$products = Product::getProducts(Context::getContext()->language->id, 0, 0, 'id_product', 'ASC');
// Tworzymy nowy obiekt DOMDocument do generowania pliku XML
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
// Tworzymy główny element "rss" z odpowiednimi atrybutami
$rss = $dom->createElement('rss');
$rssVersion = $dom->createAttribute('version');
$rssVersion->value = '2.0';
$rss->appendChild($rssVersion);
$xmlnsG = $dom->createAttribute('xmlns:g');
$xmlnsG->value = 'http://base.google.com/ns/1.0';
$rss->appendChild($xmlnsG);
$dom->appendChild($rss);
// Tworzymy element "channel"
$channel = $dom->createElement('channel');
$rss->appendChild($channel);
// Dodajemy tytuł kanału
$title = $dom->createElement('title', 'Wyczaruj Prezent');
$channel->appendChild($title);
// Przechodzimy przez wszystkie produkty i generujemy odpowiednie elementy dla każdego z nich
foreach ($products as $product)
{
// Sprawdzamy, czy produkt jest aktywny; jeśli nie, kontynuujemy pętlę
if (!$product['active']) {
continue;
}
// Załadowanie produktu
$productObj = new Product($product['id_product'], true, $id_lang);
// Tworzymy element "item" dla każdego produktu
$item = $dom->createElement('item');
$channel->appendChild($item);
// Dodajemy informacje o produkcie - dostosuj to odpowiednio do Twoich potrzeb
$id = $dom->createElement('g:id', $product['id_product']);
$item->appendChild($id);
$title = $dom->createElement('g:title', htmlspecialchars($product['name'], ENT_QUOTES));
$item->appendChild($title);
// Pobierz oryginalny opis produktu z PrestaShop
$productDescription = $product['description'];
// Usuń nowe linie (przejścia do nowej linii) z opisu produktu
$cleanedDescription = str_replace("\n", "", $productDescription);
$cleanedDescription = str_replace("\r", "", $cleanedDescription);
// Tworzymy element "description" dla opisu produktu z CDATA
$description = $dom->createElement('g:description');
$descriptionCdata = $dom->createCDATASection($cleanedDescription);
$description->appendChild($descriptionCdata);
$item->appendChild($description);
// Pobierz cenę brutto (z podatkami)
$priceWithTaxes = Product::getPriceStatic($product['id_product'], true);
// Dodajemy informacje o cenach i dostępności produktu
$price = $dom->createElement( 'g:price', round( $priceWithTaxes, 2 ) . ' PLN');
$item->appendChild($price);
$availability = $dom->createElement('g:availability', 'in stock');
$item->appendChild($availability);
// Dodajemy link do produktu
$link = $dom->createElement('g:link', htmlspecialchars( 'https://wyczarujprezent.pl/' . $product['link_rewrite'] . '.html', ENT_QUOTES));
$item->appendChild($link);
// Pobierz kod EAN produktu
$ean = $productObj->ean13;
$eanElement = $dom->createElement('g:gtin', $ean);
$item->appendChild($eanElement);
if ( $ean )
{
$eanElement = $dom->createElement('identifier_exists', 'yes');
$item->appendChild($eanElement);
}
else
{
$eanElement = $dom->createElement('identifier_exists', 'no');
$item->appendChild($eanElement);
}
if ( !$productObj -> hasAttributes() )
{
// Obsługa produktu bez kombinacji
$stockQuantity = StockAvailable::getQuantityAvailableByProduct($product['id_product'], 0);
$stock = $dom->createElement('g:stock', $stockQuantity);
$item->appendChild($stock);
$images = $productObj->getImages($id_lang);
if (!empty($images))
{
$image = array_shift($images);
$linkRewrite = $productObj->link_rewrite[1];
$imageLink = Context::getContext()->link->getImageLink($linkRewrite, $image['id_image'], 'large_default');
$imageElement = $dom->createElement('g:image_link', $imageLink);
$item->appendChild($imageElement);
}
}
else
{
$combinations = $productObj->getProductAttributesIds($product['id_product']);
foreach ( $combinations as $combination )
{
// Tworzenie elementu "item_group_id" dla każdej kombinacji
$itemGroupId = $dom->createElement('g:item_group_id', $product['id_product'] . '-' . $combination['id_product_attribute']);
$item->appendChild($itemGroupId);
// Pobieranie stanu magazynowego dla konkretnej kombinacji
$stockQuantity = StockAvailable::getQuantityAvailableByProduct($product['id_product'], $combination['id_product_attribute']);
$stock = $dom->createElement('g:stock', $stockQuantity);
$item->appendChild($stock);
$combinationImages = $productObj->getCombinationImages($id_lang);
if (isset($combinationImages[$combination['id_product_attribute']]))
{
$i = 0;
foreach ($combinationImages[$combination['id_product_attribute']] as $combinationImage)
{
if ( $i > 0 ) {
continue;
}
$imageLink = Context::getContext()->link->getImageLink($productObj->link_rewrite, $combinationImage['id_image'], ImageType::getFormatedName('large'));
$imageElement = $dom->createElement('g:image_link', $imageLink);
$item->appendChild($imageElement);
$i++;
}
}
else
{
$images = $productObj->getImages($id_lang);
if (!empty($images))
{
$image = array_shift($images);
$linkRewrite = $productObj->link_rewrite[1];
$imageLink = Context::getContext()->link->getImageLink($linkRewrite, $image['id_image'], 'large_default');
$imageElement = $dom->createElement('g:image_link', $imageLink);
$item->appendChild($imageElement);
}
}
// Tutaj możesz dodać dodatkowe informacje o kombinacjach, które są ważne dla Google Merchant Center
}
}
// Tworzenie elementu wysyłki
$shipping = $dom->createElement('g:shipping');
// Tworzenie i dodawanie elementu kraju
$country = $dom->createElement('g:country', 'PL');
$shipping->appendChild($country);
// Tworzenie i dodawanie elementu usługi
$service = $dom->createElement('g:service', 'Paczkomaty InPost');
$shipping->appendChild($service);
// Tworzenie i dodawanie elementu ceny wysyłki
$shippingPrice = $dom->createElement('g:price', '13.25 PLN');
$shipping->appendChild($shippingPrice);
// Dodawanie całego elementu wysyłki do elementu produktu
$item->appendChild($shipping);
// Tworzenie i dodawanie elementu stanu produktu
$condition = $dom->createElement('g:condition', 'new');
$item->appendChild($condition);
// Tworzenie i dodawanie elementu producenta produktu
$manufacturerId = $productObj->id_manufacturer;
$manufacturer = new Manufacturer($manufacturerId, Context::getContext()->language->id);
$brandName = $manufacturer->name;
$brand = $dom->createElement('g:brand', $brandName);
$item->appendChild($brand);
}
// Zapisujemy plik XML
$dom->save($xmlFilePath);
// Wyświetlamy komunikat o sukcesie lub błędzie
if (file_exists($xmlFilePath))
{
echo "Plik XML został wygenerowany i zapisany jako: " . $xmlFilePath;
}
else
{
echo "Błąd generowania pliku XML!";
}