prestashop-module/retailcrm/lib/RetailcrmCatalog.php

215 lines
8.5 KiB
PHP
Raw Normal View History

2016-01-13 18:43:41 +03:00
<?php
class RetailcrmCatalog
{
2018-05-28 17:09:31 +03:00
public $default_lang;
public $default_currency;
public $default_country;
2016-01-13 18:43:41 +03:00
public function __construct()
{
$this->default_lang = (int) Configuration::get('PS_LANG_DEFAULT');
$this->default_currency = (int) Configuration::get('PS_CURRENCY_DEFAULT');
$this->default_country = (int) Configuration::get('PS_COUNTRY_DEFAULT');
}
public function getData()
{
$id_lang = (int) Configuration::get('PS_LANG_DEFAULT');
$currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
$shop_url = (Configuration::get('PS_SSL_ENABLED') ? _PS_BASE_URL_SSL_ : _PS_BASE_URL_);
$protocol = (Configuration::get('PS_SSL_ENABLED')) ? "https://" : "http://";
2016-01-13 18:43:41 +03:00
$items = array();
$categories = array();
if ($currency->iso_code == 'RUB') {
$currency->iso_code = 'RUR';
}
$currencies = Currency::getCurrencies();
$types = Category::getCategories($id_lang, true, false);
2016-01-13 18:43:41 +03:00
foreach ($types AS $category) {
$categories[] = array(
'id' => $category['id_category'],
'parentId' => $category['id_parent'],
'name' => $category['name']
);
}
$link = new Link();
2016-01-13 18:43:41 +03:00
$products = Product::getProducts($id_lang, 0, 0, 'name', 'asc');
foreach ($products AS $product) {
$category = $product['id_category_default'];
if ($category == Configuration::get('PS_HOME_CATEGORY')) {
$temp_categories = Product::getProductCategories($product['id_product']);
foreach ($temp_categories AS $category) {
if ($category != Configuration::get('PS_HOME_CATEGORY'))
break;
}
if ($category == Configuration::get('PS_HOME_CATEGORY')) {
continue;
}
}
$version = substr(_PS_VERSION_, 0, 3);
if ($version == "1.3") {
$available_for_order = $product['active'] && $product['quantity'];
} else {
$available_for_order = $product['active'] && $product['available_for_order'];
2016-01-13 18:43:41 +03:00
}
$crewrite = Category::getLinkRewrite($product['id_category_default'], $id_lang);
$url = $link->getProductLink($product['id_product'], $product['link_rewrite'], $crewrite);
2016-01-13 18:43:41 +03:00
if (!empty($product['wholesale_price'])) {
$purchasePrice = round($product['wholesale_price'], 2);
} else {
$purchasePrice = null;
2016-01-13 18:43:41 +03:00
}
$price = !empty($product['rate'])
2016-01-13 18:43:41 +03:00
? round($product['price'], 2) + (round($product['price'], 2) * $product['rate'] / 100)
: round($product['price'], 2)
;
2016-01-13 18:43:41 +03:00
if (!empty($product['manufacturer_name'])) {
$vendor = $product['manufacturer_name'];
} else {
$vendor = null;
2016-01-13 18:43:41 +03:00
}
if (!empty($product['reference'])) {
$article = htmlspecialchars($product['reference']);
} else {
$article = null;
2016-01-13 18:43:41 +03:00
}
$weight = round($product['weight'], 2);
if (empty($weight)) {
$weight = null;
2016-01-13 18:43:41 +03:00
}
$width = round($product['width'], 2);
$height = round($product['height'], 2);
$depth = round($product['depth'], 2);
if (!empty($width) && !empty($height)) {
if (!empty($depth)) {
$size = implode('x', array($width, $height, $depth));
} else {
$size = implode('x', array($width, $height));
}
} else {
$size = null;
}
$productForCombination = new Product($product['id_product']);
$offers = Product::getProductAttributesIds($product['id_product']);
if(!empty($offers)) {
foreach($offers as $offer) {
$combinations = $productForCombination->getAttributeCombinationsById($offer['id_product_attribute' ], $id_lang);
if (!empty($combinations)) {
foreach ($combinations as $combination) {
$arSet = array(
'group_name' => $combination['group_name'],
'attribute' => $combination['attribute_name'],
);
$arComb[] = $arSet;
}
}
$pictures = array();
$covers = Image::getImages($id_lang, $product['id_product'], $offer['id_product_attribute']);
foreach($covers as $cover) {
$picture = $protocol . $link->getImageLink($product['link_rewrite'], $product['id_product'] . '-' . $cover['id_image'], 'large_default');
$pictures[] = $picture;
}
if ($version == "1.3") {
$quantity = $product['quantity'];
2016-01-13 18:43:41 +03:00
} else {
$quantity = (int) StockAvailable::getQuantityAvailableByProduct($product['id_product'], $offer['id_product_attribute']);
2016-01-13 18:43:41 +03:00
}
$offerPrice = Combination::getPrice($offer['id_product_attribute']);
$offerPrice = $offerPrice > 0 ? $offerPrice : $price;
$item = array(
'id' => $product['id_product'] . '#' . $offer['id_product_attribute'],
'productId' => $product['id_product'],
'productActivity' => ($available_for_order) ? 'Y' : 'N',
'name' => htmlspecialchars(strip_tags(Product::getProductName($product['id_product'], $offer['id_product_attribute']))),
'productName' => htmlspecialchars(strip_tags($product['name'])),
'categoryId' => array($category),
'picture' => $pictures,
'url' => $url,
'quantity' => $quantity > 0 ? $quantity : 0,
'purchasePrice' => $purchasePrice,
'price' => round($offerPrice, 2),
'vendor' => $vendor,
'article' => $article,
'weight' => $weight,
'size' => $size
);
if (!empty($combinations)) {
foreach ($arComb as $itemComb) {
$item[mb_strtolower($itemComb['group_name'])] = htmlspecialchars($itemComb['attribute']);
}
}
$items[] = $item;
}
} else {
$pictures = array();
$covers = Image::getImages($id_lang, $product['id_product'], null);
foreach($covers as $cover) {
$picture = $protocol . $link->getImageLink($product['link_rewrite'], $product['id_product'] . '-' . $cover['id_image'], 'large_default');
$pictures[] = $picture;
2016-01-13 18:43:41 +03:00
}
if ($version == "1.3") {
$quantity = $product['quantity'];
} else {
$quantity = (int) StockAvailable::getQuantityAvailableByProduct($product['id_product']);
}
$item = array(
'id' => $product['id_product'],
'productId' => $product['id_product'],
'productActivity' => ($available_for_order) ? 'Y' : 'N',
'name' => htmlspecialchars(strip_tags($product['name'])),
'productName' => htmlspecialchars(strip_tags($product['name'])),
'categoryId' => array($category),
'picture' => $pictures,
'url' => $url,
'quantity' => $quantity > 0 ? $quantity : 0,
'purchasePrice' => round($purchasePrice, 2),
'price' => $price,
'vendor' => $vendor,
'article' => $article,
'weight' => $weight,
'size' => $size
);
$items[] = $item;
2016-01-13 18:43:41 +03:00
}
}
return array($categories, $items);
}
}