Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
Feat: Google
Browse files Browse the repository at this point in the history
Added Provider class and Google Merchant provider.
  • Loading branch information
luciobenini committed Jan 18, 2021
1 parent d895efe commit 8b85491
Show file tree
Hide file tree
Showing 10 changed files with 441 additions and 148 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ upload/

# Module data

output.xml
/output/*.xml
101 changes: 101 additions & 0 deletions classes/GoogleProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/**
* prestashop-trovaprezzi
*
* Copyright 2020 Pittica S.r.l.s.
*
* @author Lucio Benini <[email protected]>
* @copyright 2020 Pittica S.r.l.s.
* @license http://opensource.org/licenses/LGPL-3.0 The GNU Lesser General Public License, version 3.0 ( LGPL-3.0 )
*/

require_once(dirname(__FILE__) . '/Provider.php');

class GoogleProvider extends Provider
{
public function getElementRoot()
{
return 'rss';
}

public function getElementItem()
{
return 'item';
}

public function getFilename()
{
return 'google';
}

public function renderAttributes($xml)
{
$xml->writeAttribute('version', '2.0');
$xml->WriteAttribute('xmlns:g', 'http://base.google.com/ns/1.0');

return $xml;
}

public function renderItem($xml, $offer)
{
$context = Context::getContext();
$carrier = new Carrier((int)Configuration::get('PITTICA_TROVAPREZZI_CARRIER'));
$locale = Tools::getContextLocale($context);

$xml->writeElement('title', $offer->name);
$xml->writeElement('description', $offer->description);
$xml->writeElement('link', $offer->link);
$xml->writeElement('g:id', $offer->id_product . ($offer->id_product_attribute ? ('-' . $offer->id_product_attribute) : ''));
$xml->writeElement('g:image_link', $offer->image_1);
$xml->writeElement('g:price', $locale->formatPrice($offer->original_price, $context->currency->iso_code));

if ($offer->original_price != $offer->price) {
$xml->writeElement('g:sale_price', $locale->formatPrice($offer->price, $context->currency->iso_code));
}

$xml->writeElement('g:gtin', !empty($offer->ean_code) ? $offer->ean_code : $offer->part_number);
$xml->writeElement('g:brand', $offer->brand);
$xml->writeElement('g:availability', $offer->stock);
$xml->startElement('g:shipping');
$xml->writeElement('g:country', Country::getIsoById((int)Configuration::get('PS_COUNTRY_DEFAULT')));
$xml->writeElement('g:service', $carrier->name);
$xml->writeElement('g:price', $locale->formatPrice($offer->shipping_cost, $context->currency->iso_code));
$xml->endElement();

if (!empty($offer->image_2)) {
$xml->writeElement('g:additional_image_link', $offer->image_2);
}

if (!empty($offer->image_3)) {
$xml->writeElement('g:additional_image_link', $offer->image_3);
}

return $xml;
}

public function renderBody($xml)
{
$xml->startElement('channel');

$meta = Meta::getHomeMetas((int)Configuration::get('PS_LANG_DEFAULT'), 'index');

$xml->writeElement('title', Configuration::get('PS_SHOP_NAME'));
$xml->writeElement('link', Context::getContext()->link->getBaseLink());
$xml->writeElement('description', $meta['meta_description']);

$offers = TrovaprezziOffer::getOffers();

foreach ($offers as $offer) {
if ($offer->active) {
$xml->startElement($this->getElementItem());

$this->renderItem($xml, $offer);

$xml->endElement();
}
}

$xml->endElement();
}
}
80 changes: 80 additions & 0 deletions classes/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* prestashop-trovaprezzi
*
* Copyright 2020 Pittica S.r.l.s.
*
* @author Lucio Benini <[email protected]>
* @copyright 2020 Pittica S.r.l.s.
* @license http://opensource.org/licenses/LGPL-3.0 The GNU Lesser General Public License, version 3.0 ( LGPL-3.0 )
*/

require_once(dirname(__FILE__) . '/TrovaprezziOffer.php');

abstract class Provider
{
public static function getProvider($name)
{
$class = ucfirst($name) . 'Provider';

require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . $class . '.php');

return new $class;
}

public static function getProviders()
{
return array(
'trovaprezzi',
'google'
);
}

public abstract function getFilename();

public abstract function getElementRoot();

public abstract function getElementItem();

public function renderAttributes($xml)
{
return $xml;
}

public abstract function renderItem($xml, $offer);

public function renderBody($xml)
{
$offers = TrovaprezziOffer::getOffers();

foreach ($offers as $offer) {
if ($offer->active) {
$xml->startElement($this->getElementItem());

$this->renderItem($xml, $offer);

$xml->endElement();
}
}

return $xml;
}

public function generate($path)
{
$xml = new XmlWriter();
$xml->openUri($path);
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement($this->getElementRoot());

$this->renderAttributes($xml);
$this->renderBody($xml);

$xml->endElement();
$xml->endDocument();
$xml->flush();

return true;
}
}
22 changes: 0 additions & 22 deletions classes/TrovaprezziOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,4 @@ public static function truncate()
{
return Db::getInstance()->execute('TRUNCATE `' . _DB_PREFIX_ . self::TABLE_NAME . '`');
}

public function toArray()
{
return array(
'Name' => $this->name,
'Brand' => $this->brand,
'Description' => $this->description,
'OriginalPrice' => $this->original_price,
'Price' => $this->price,
'Code' => $this->id_product . ($this->id_product_attribute ? ('-' . $this->id_product_attribute) : ''),
'Link' => $this->link,
'Stock' => $this->stock,
'Categories' => $this->categories,
'Image' => $this->image_1,
'ShippingCost' => $this->shipping_cost,
'PartNumber' => $this->part_number,
'EanCode' => $this->ean_code,
'Weight' => $this->weight,
'Image2' => $this->image_2,
'Image3' => $this->image_3
);
}
}
53 changes: 53 additions & 0 deletions classes/TrovaprezziProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* prestashop-trovaprezzi
*
* Copyright 2020 Pittica S.r.l.s.
*
* @author Lucio Benini <[email protected]>
* @copyright 2020 Pittica S.r.l.s.
* @license http://opensource.org/licenses/LGPL-3.0 The GNU Lesser General Public License, version 3.0 ( LGPL-3.0 )
*/

require_once(dirname(__FILE__) . '/Provider.php');

class TrovaprezziProvider extends Provider
{
public function getElementRoot()
{
return 'Products';
}

public function getElementItem()
{
return 'Offer';
}

public function getFilename()
{
return 'trovaprezzi';
}

public function renderItem($xml, $offer)
{
$xml->writeElement('Name', $offer->name);
$xml->writeElement('Brand', $offer->brand);
$xml->writeElement('Description', $offer->description);
$xml->writeElement('OriginalPrice', $offer->original_price);
$xml->writeElement('Price', $offer->price);
$xml->writeElement('Code', $offer->id_product . ($offer->id_product_attribute ? ('-' . $offer->id_product_attribute) : ''));
$xml->writeElement('Link', $offer->link);
$xml->writeElement('Stock', $offer->stock);
$xml->writeElement('Categories', $offer->categories);
$xml->writeElement('Image', $offer->image_1);
$xml->writeElement('ShippingCost', $offer->shipping_cost);
$xml->writeElement('PartNumber', $offer->part_number);
$xml->writeElement('EanCode', $offer->ean_code);
$xml->writeElement('Weight', $offer->weight);
$xml->writeElement('Image2', $offer->image_2);
$xml->writeElement('Image3', $offer->image_3);

return $xml;
}
}
10 changes: 6 additions & 4 deletions controllers/front/download.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ class pitticatrovaprezzidownloadModuleFrontController extends ModuleFrontControl
public function initContent()
{
parent::initContent();

if (Tools::getValue('token') === $this->module->getToken()) {
if (file_exists($this->module->getFilePath())) {
$path = $this->module->getFilePath(Tools::getValue('provider', 'trovaprezzi'));

if (file_exists($path)) {
header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
header('Cache-Control: public');
header('Content-Type: text/xml');
readfile($this->module->getFilePath());
readfile($path);
}
}

die();
}
}
6 changes: 3 additions & 3 deletions controllers/front/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class pitticatrovaprezzigenerateModuleFrontController extends ModuleFrontControl
public function initContent()
{
parent::initContent();

if (Tools::getValue('token') === $this->module->getToken()) {
$this->module->generate();
$this->module->generate((int)Tools::getValue('refresh', true), Tools::getValue('provider'));
}

die();
}
}
21 changes: 21 additions & 0 deletions output/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* prestashop-trovaprezzi
*
* Copyright 2020 Pittica S.r.l.s.
*
* @author Lucio Benini <[email protected]>
* @copyright 2020 Pittica S.r.l.s.
* @license http://opensource.org/licenses/LGPL-3.0 The GNU Lesser General Public License, version 3.0 ( LGPL-3.0 )
*/

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');

header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');

header('Location: ../');
exit;
Loading

0 comments on commit 8b85491

Please sign in to comment.