2017-08-29 15:16:24 +02:00

206 lines
7.6 KiB
PHP

<?php
class ProductController extends ProductControllerCore
{
/*
* module: antadisconfigurator
* date: 2017-06-27 12:10:07
* version: 0.1
*/
protected $optValues = array();
/*
* module: antadisconfigurator
* date: 2017-06-27 12:10:07
* version: 0.1
*/
protected $impactPrice;
/*
* module: antadisconfigurator
* date: 2017-06-27 12:10:07
* version: 0.1
*/
protected $productPriceWithImpact;
/*
* module: antadisconfigurator
* date: 2017-06-27 12:10:07
* version: 0.1
*/
public function init()
{
parent::init();
if (Tools::getValue('ajax') && Tools::getValue('action') == 'getProductInfo') {
$this->ajaxProcessGetProductInfo();
}
if ($this->ajax && Tools::getValue('impactprice', 0)) {
// Product Info
if (Validate::isLoadedObject($this->product)) {
$qty = Tools::getValue('qty', 1);
$productPrice = $this->product->getPrice(true, null, 6, null, false, true, $qty);
} else {
return;
}
// Options selected
$detectOptGroup = 'optgroup-';
$postValues = Tools::getAllValues();
$this->optValues = array();
$impact = 0;
if (count($postValues) > 0) {
foreach($postValues as $p => $v) {
if (!empty($v)) {
if (substr($p, 0, strlen($detectOptGroup)) === $detectOptGroup) {
$ids = explode('-', substr($p, strlen($detectOptGroup)));
$id_configurator_opt_group = $ids[0];
$id_configurator_opt = $v;
if (isset($id[1])) {
$id_configurator_opt = $ids[1];
}
$this->optValues[] = array(
'id_configurator_opt_group' => substr($p, strlen($detectOptGroup)),
'id_configurator_opt' => $id_configurator_opt
);
}
}
}
$impact = $this->product->getConfiguratorImpactPrice($this->optValues);
}
$this->impactPrice = $impact;
// We want total price
$productPrice = $productPrice * $qty;
$this->productPriceWithImpact = $productPrice + $impact;
}
}
/*
* module: antadisconfigurator
* date: 2017-06-27 12:10:07
* version: 0.1
*/
public function postProcess()
{
if ($this->ajax && Tools::getValue('impactprice', 0)) {
$qty = Tools::getValue('qty', 1);
$return = array(
'priceUnitDisplay' => Tools::displayPrice($this->product->getPrice(true, null, 6, null, false, true, $qty)),
'priceDisplay' => Tools::displayPrice($this->productPriceWithImpact),
'price' => $this->productPriceWithImpact,
);
die(Tools::jsonEncode($return));
}
}
public function setMedia()
{
parent::setMedia();
$this->addJS(_THEME_JS_DIR_.'tools/flexslider.js');
$this->addJS(_THEME_JS_DIR_.'tools/lightGallery/lightgallery.min.js');
$this->addJS(_THEME_JS_DIR_.'tools/lightGallery/lg-zoom.min.js');
$this->addJS(_THEME_JS_DIR_.'tools/lightGallery/lg-thumbnail.min.js');
$this->addCSS(_THEME_CSS_DIR_.'tools/flexslider.css');
$this->addCSS(_THEME_CSS_DIR_.'addresses.css');
$this->addCSS(_THEME_CSS_DIR_.'tools/lightgallery.css');
}
public function initContent()
{
parent::initContent();
$impact = 0;
if (($id_configurator = Tools::getValue('id_configurator', 0)) != 0) {
// Product Info
if (Validate::isLoadedObject($this->product)) {
$qty = Tools::getValue('qty', 1);
$productPrice = $this->product->getPrice(true, null, 6, null, false, true, $qty);
}
$impact = Product::getConfiguratorSelectedPriceTotal($this->product->id, $id_configurator);
}
$this->context->smarty->assign('impactPrice', $impact);
}
public function ajaxProcessGetProductInfo()
{
$context = Context::getContext();
$product = new Product(Tools::getValue('id_product'),true,$context->cookie->id_lang);
if($product)
{
$idCombination = null;
$attributes = null;
$qty = Tools::getValue('qty', 1);
$product->quantity = $qty;
if(Tools::getValue('idCombination'))
{
$idCombination = Tools::getValue('idCombination');
$attributes = $product->getAttributeCombinationsById($idCombination,$context->cookie->id_lang);
}
$priceDisplay = Group::getPriceDisplayMethod($context->customer->id_default_group);
if (!$priceDisplay || $priceDisplay == 2)
{
$productPriceFloat = $product->getPrice(true, $idCombination);
$productPrice = Tools::displayPrice($productPriceFloat);
$productPriceWithoutReductionFloat = $product->getPriceWithoutReduct(false, $idCombination);
$productPriceWithoutReduction = Tools::displayPrice($productPriceWithoutReductionFloat);
$totalPrice = Tools::displayPrice($product->getPrice(true, $idCombination) * $qty);
}
else if( $priceDisplay == 1)
{
$productPriceFloat = $product->getPrice(false, $idCombination);
$productPrice = Tools::displayPrice($productPriceFloat);
$productPriceWithoutReductionFloat = $product->getPriceWithoutReduct(true, $idCombination);
$productPriceWithoutReduction = Tools::displayPrice($productPriceWithoutReductionFloat);
$totalPrice = Tools::displayPrice($product->getPrice(false, $idCombination) * $qty);
}
if($idCombination !== null){
$combinationImages = $product->getCombinationImages($this->context->language->id);
if($combinationImages)
{
$img = reset($combinationImages[$idCombination]);
$product->img = $context->link->getImageLink($product->link_rewrite, $img["id_image"], 'home_default');
}
else
{
$product->img = $context->link->getImageLink($product->link_rewrite, $product->getCoverWs(), 'home_default');
}
} else {
$product->img = $context->link->getImageLink($product->link_rewrite, $product->getCoverWs(), 'home_default');
}
$product->attributes = $attributes;
$product->qty = $qty;
$product->productPrice = $productPrice;
$product->productPriceWithoutReduction = $productPriceWithoutReduction;
$product->totalPrice = $totalPrice;
$product->reduction = 0;
if($productPrice != $productPriceWithoutReduction) {
$product->reduction = -1 * round(100 - ($productPriceFloat * 100 / $productPriceWithoutReductionFloat));
}
$to_return = array(
'product' => $product,
'found' => true
);
}
else
{
$to_return = array('found' => false);
}
echo Tools::jsonEncode($to_return);
}
}