2017-06-27 12:21:47 +02:00

80 lines
3.7 KiB
PHP

<?php
class Link extends LinkCore
{
/*
* module: antadisconfigurator
* date: 2017-06-27 12:10:06
* version: 0.1
*/
public function getProductLink($product, $alias = null, $category = null, $ean13 = null, $id_lang = null, $id_shop = null, $ipa = 0, $force_routes = false, $relative_protocol = false, $add_anchor = false, $id_configurator = null)
{
$dispatcher = Dispatcher::getInstance();
if (!$id_lang) {
$id_lang = Context::getContext()->language->id;
}
$url = $this->getBaseLink($id_shop, null, $relative_protocol).$this->getLangLink($id_lang, null, $id_shop);
if (!is_object($product)) {
if (is_array($product) && isset($product['id_product'])) {
$product = new Product($product['id_product'], false, $id_lang, $id_shop);
} elseif ((int)$product) {
$product = new Product((int)$product, false, $id_lang, $id_shop);
} else {
throw new PrestaShopException('Invalid product vars');
}
}
$params = array();
$params['id'] = $product->id;
$params['rewrite'] = (!$alias) ? $product->getFieldByLang('link_rewrite') : $alias;
$params['ean13'] = (!$ean13) ? $product->ean13 : $ean13;
$params['meta_keywords'] = Tools::str2url($product->getFieldByLang('meta_keywords'));
$params['meta_title'] = Tools::str2url($product->getFieldByLang('meta_title'));
if ($dispatcher->hasKeyword('product_rule', $id_lang, 'manufacturer', $id_shop)) {
$params['manufacturer'] = Tools::str2url($product->isFullyLoaded ? $product->manufacturer_name : Manufacturer::getNameById($product->id_manufacturer));
}
if ($dispatcher->hasKeyword('product_rule', $id_lang, 'supplier', $id_shop)) {
$params['supplier'] = Tools::str2url($product->isFullyLoaded ? $product->supplier_name : Supplier::getNameById($product->id_supplier));
}
if ($dispatcher->hasKeyword('product_rule', $id_lang, 'price', $id_shop)) {
$params['price'] = $product->isFullyLoaded ? $product->price : Product::getPriceStatic($product->id, false, null, 6, null, false, true, 1, false, null, null, null, $product->specificPrice);
}
if ($dispatcher->hasKeyword('product_rule', $id_lang, 'tags', $id_shop)) {
$params['tags'] = Tools::str2url($product->getTags($id_lang));
}
if ($dispatcher->hasKeyword('product_rule', $id_lang, 'category', $id_shop)) {
$params['category'] = (!is_null($product->category) && !empty($product->category)) ? Tools::str2url($product->category) : Tools::str2url($category);
}
if ($dispatcher->hasKeyword('product_rule', $id_lang, 'reference', $id_shop)) {
$params['reference'] = Tools::str2url($product->reference);
}
if ($dispatcher->hasKeyword('product_rule', $id_lang, 'categories', $id_shop)) {
$params['category'] = (!$category) ? $product->category : $category;
$cats = array();
foreach ($product->getParentCategories($id_lang) as $cat) {
if (!in_array($cat['id_category'], Link::$category_disable_rewrite)) {
$cats[] = $cat['link_rewrite'];
}
}
$params['categories'] = implode('/', $cats);
}
$anchor = $ipa ? $product->getAnchor((int)$ipa, (bool)$add_anchor) : '';
if (!empty($id_configurator)) {
$params['id_configurator'] = $id_configurator;
}
return $url.$dispatcher->createUrl('product_rule', $id_lang, $params, $force_routes, $anchor, $id_shop);
}
}