* @copyright 2012-2018 Patryk Marek - PrestaDev.pl * @license License is for use in domain / or one multistore enviroment (do not modify or reuse this code or part of it) * @link http://prestadev.pl * @package PD Free delivery left amount for Pro PS 1.6x && 1.7.x * @version 1.1.1 * @date 18-12-2014 */ if (!defined('_PS_VERSION_')) { exit; } use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever; use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter; use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever; use PrestaShop\PrestaShop\Core\Module\WidgetInterface; use PrestaShop\PrestaShop\Core\Product\ProductListingPresenter; class PdFreeDeliveryLeftAmountPro extends Module { private $html = ''; public function __construct() { $this->name = 'pdfreedeliveryleftamountpro'; $this->tab = 'front_office_features'; $this->version = '1.1.6'; $this->author = 'PrestaDev.pl'; $this->secure_key = Tools::encrypt($this->name); $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Free delivery left amount pro'); $this->description = $this->l('Display free delivery left amount in shopping cart summary, product page and in any place choosen by using custom hook'); $this->ps_version_16 = (version_compare(substr(_PS_VERSION_, 0, 3), '1.6', '=')) ? true : false; $this->ps_version_17 = (version_compare(substr(_PS_VERSION_, 0, 3), '1.7', '=')) ? true : false; $this->show_custom = Configuration::get('PD_FDLAP_SHOW_CUSTOM'); $this->show_shopping_cart_footer = Configuration::get('PD_FDLAP_SHOW_SCF'); $this->show_shopping_product_page = Configuration::get('PD_FDLAP_SHOW_PP'); } public function install() { Configuration::updateValue('PD_FDLAP_SHOW_CUSTOM', 0); Configuration::updateValue('PD_FDLAP_SHOW_SCF', 1); Configuration::updateValue('PD_FDLAP_SHOW_PP', 1); if (!parent::install() || !$this->registerHook('header') || !$this->registerHook('extraRight') || !$this->registerHook('displayCustomFreeDeliveryLeftAmount') || !$this->registerHook('shoppingCart') || !$this->registerHook('displayProductButtons') ) { return false; } return true; } public function uninstall() { // Uninstall if (!parent::uninstall()) { return false; } else { return true; } } public function postProcess() { if (Tools::isSubmit('btnSubmit')) { Configuration::updateValue('PD_FDLAP_SHOW_CUSTOM', Tools::getValue('PD_FDLAP_SHOW_CUSTOM')); Configuration::updateValue('PD_FDLAP_SHOW_SCF', Tools::getValue('PD_FDLAP_SHOW_SCF')); Configuration::updateValue('PD_FDLAP_SHOW_PP', Tools::getValue('PD_FDLAP_SHOW_PP')); $this->html .= $this->displayConfirmation($this->l('Settings updated')); } } public function getContent() { if (Tools::isSubmit('btnSubmit')) { $this->postProcess(); } $this->html .= '
'; $this->html .= '

'.$this->displayName.' (v'.$this->version.')

'.$this->description.'

'; $this->html .= $this->displayForm(); $this->html .= '
'; return $this->html; } protected function initForm() { $helper = new HelperForm(); $helper->module = $this; $helper->name_controller = 'pdfreedeliveryleftamountpro'; $helper->identifier = $this->identifier; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->languages = $this->context->controller->_languages; $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; $helper->default_form_language = $this->context->controller->default_form_language; $helper->allow_employee_form_lang = $this->context->controller->allow_employee_form_lang; $helper->show_toolbar = false; return $helper; } public function displayForm() { $switch = version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'; $button_class = version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'btn btn-default pull-right' : 'button'; $this->fields_form[0]['form'] = array( 'legend' => array( 'title' => $this->l('Module configuration'), 'icon' => 'icon-cogs' ), 'input' => array( array( 'type' => $switch, 'label' => $this->l('Show custom hook'), 'name' => 'PD_FDLAP_SHOW_CUSTOM', 'is_bool' => true, 'class' => 't', 'desc' => $this->l('Showing informations regarding free delivery left amount on custom hook, please add in any tpl file line of code: {hook h=\'displayCustomFreeDeliveryLeftAmount\'}'), 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->l('Disabled') ) ), ), array( 'type' => $switch, 'label' => $this->l('Show on shopping cart footer hook'), 'name' => 'PD_FDLAP_SHOW_SCF', 'is_bool' => true, 'class' => 't', 'desc' => $this->l('Showing informations regarding free delivery left amount on shopping cart footer hook'), 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->l('Disabled') ) ), ), array( 'type' => $switch, 'label' => $this->l('Show on product page'), 'name' => 'PD_FDLAP_SHOW_PP', 'is_bool' => true, 'class' => 't', 'desc' => $this->l('Showing informations regarding free delivery left amount on product page hook'), 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->l('Disabled') ) ), ), ), 'submit' => array( 'name' => 'btnSubmit', 'title' => $this->l('Save settings'), 'class' => $button_class ) ); $this->fields_value['PD_FDLAP_SHOW_CUSTOM'] = Configuration::get('PD_FDLAP_SHOW_CUSTOM'); $this->fields_value['PD_FDLAP_SHOW_SCF'] = Configuration::get('PD_FDLAP_SHOW_SCF'); $this->fields_value['PD_FDLAP_SHOW_PP'] = Configuration::get('PD_FDLAP_SHOW_PP'); $helper = $this->initForm(); $helper->submit_action = 'btnSubmit'; $helper->fields_value = isset($this->fields_value) ? $this->fields_value : array(); $this->html .= $helper->generateForm($this->fields_form); } public function doFreeDliveryCheck($cart) { $id_carrier = (int)$cart->id_carrier; $module_instance = Module::getInstanceByName('pdfreedeliverypro'); if ($module_instance instanceOf Module) { if ($module_instance->active) { return $module_instance->doFreeDeliveryCheckByParams($id_carrier, $cart, true); } else { return false; } } else { return false; } } public function calculateFreeDeliveryLeftAmount($ajax = 0) { $free_ship_from = 0; $delta = 0; $cart = Context::getContext()->cart; $freedeliverymodule_config_obj = $this->doFreeDliveryCheck($cart); if ($freedeliverymodule_config_obj != false && is_object($freedeliverymodule_config_obj)) { $free_ship_from = $freedeliverymodule_config_obj->free_from_price; } else { $free_ship_from = (float)Configuration::get('PS_SHIPPING_FREE_PRICE'); } $id_currency = (int)$cart->id_currency; if (!empty($id_currency)) { $free_ship_from = Tools::convertPrice($free_ship_from, $id_currency, true); } $id_customer = (int)$this->context->customer->id; if ($id_customer) { $curent_customer_default_group = Customer::getDefaultGroupId($id_customer); $price_display_method = Group::getPriceDisplayMethod($curent_customer_default_group); } else { $price_display_method = Group::getDefaultPriceDisplayMethod(); } if ($price_display_method == 0) { $tax = true; } else { $tax = false; } $order_total = $cart->getOrderTotal($tax, Cart::BOTH_WITHOUT_SHIPPING, null, null, false); $delta = $free_ship_from - $order_total; if ($delta < 0) { $delta = 0; } $currency = new Currency((int)$cart->id_currency); return array( 'free_ship_from' => $free_ship_from, 'free_ship_from_display' => Tools::displayPrice($free_ship_from), 'delta' => $delta, 'delta_display' => Tools::displayPrice($delta), 'currency_sign' => $currency->sign ); } public function hookShoppingCart($params) { if (!$this->show_shopping_cart_footer) { return; } $data = $this->calculateFreeDeliveryLeftAmount(); $context = Context::getContext(); $this->context->smarty->assign(array( 'free_ship_from' => $data['free_ship_from'], 'delta' => $data['delta'], 'ps_version_16' => $this->ps_version_16, 'ps_version_17' => $this->ps_version_17 )); $id_products = [ 196449541, 196449971, 20597818, 20602396, 20602127, 20605963, 20606101, 20597990, 20601374, 20600556, 20605703, 20606050 ]; // Podmień to na fak // Podmień to na faktyczne ID produktu $randomKeys = array_rand($id_products, 4); $randomValues = array_map(function($key) use ($id_products) { return $id_products[$key]; }, $randomKeys); $id_lang = Context::getContext()->language->id; foreach ( $randomValues as $id_product ) { $product = new Product($id_product, true, $id_lang); if (Validate::isLoadedObject($product)) { $assembler = new ProductAssembler(Context::getContext()); $presenterFactory = new ProductPresenterFactory(Context::getContext()); $presentationSettings = $presenterFactory->getPresentationSettings(); $presenter = new PrestaShop\PrestaShop\Core\Product\ProductListingPresenter( new PrestaShop\PrestaShop\Adapter\Image\ImageRetriever( Context::getContext()->link ), Context::getContext()->link, new PrestaShop\PrestaShop\Adapter\Product\PriceFormatter(), new PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever(), Context::getContext()->getTranslator() ); $productArray = $presenter->present( $presentationSettings, $assembler->assembleProduct($product->getFields()), Context::getContext()->language ); $products_array[] = $productArray; } } $this->context->smarty->assign('products', $products_array); if ($this->ps_version_17) { return $this->display(__FILE__, 'views/hooks/shopping_cart_17.tpl'); } else { return $this->display(__FILE__, 'views/hooks/shopping_cart.tpl'); } } public function getFrontendProductInformation($productIds) { foreach($productIds as $rs){ $products[] = new Product($rs, false, $this->context->language->id); } $assembler = new ProductAssembler($this->context); $presenterFactory = new ProductPresenterFactory($this->context); $presentationSettings = $presenterFactory->getPresentationSettings(); $presenter = new ProductListingPresenter( new ImageRetriever( $this->context->link ), $this->context->link, new PriceFormatter(), new ProductColorsRetriever(), $this->context->getTranslator() ); $products_for_template = []; foreach ($products as $rawProduct) { $rawProduct = (array) $rawProduct; $products_for_template[] = $presenter->present( $presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language ); } return $products_for_template; } public function hookdisplayProductButtons($params) { if (!$this->show_shopping_product_page) { return; } $data = $this->calculateFreeDeliveryLeftAmount(); $this->context->smarty->assign(array( 'free_ship_from' => $data['free_ship_from'], 'delta' => $data['delta'], 'ps_version_16' => $this->ps_version_16, 'ps_version_17' => $this->ps_version_17 )); if ($this->ps_version_17) { return $this->display(__FILE__, 'views/hooks/displayproductbuttons_17.tpl'); } else { return $this->display(__FILE__, 'views/hooks/displayproductbuttons.tpl'); } } public function hookDisplayCustomFreeDeliveryLeftAmount($params) { if (!$this->show_custom) { return; } $data = $this->calculateFreeDeliveryLeftAmount(false); $this->context->smarty->assign(array( 'free_ship_from' => $data['free_ship_from'], 'delta' => $data['delta'], 'ps_version_16' => $this->ps_version_16, 'ps_version_17' => $this->ps_version_17 )); if ($this->ps_version_17) { return $this->display(__FILE__, 'views/hooks/customhook_17.tpl'); } else { return $this->display(__FILE__, 'views/hooks/customhook.tpl'); } } public function hookHeader() { if ($this->ps_version_16) { $this->context->controller->addJS($this->_path . '/views/js/scripts.js'); $this->context->controller->addCSS($this->_path . '/views/css/styles.css'); } else { Media::addJsDef(array( 'pdfreedeliveryleftamountpro_secure_key' => $this->secure_key, 'pdfreedeliveryleftamountpro_ajax_link' => $this->_path.'/ajax.php', )); $this->context->controller->registerStylesheet( 'modules-pdfreedeliveryleftamountpro-front', 'modules/'.$this->name.'/views/css/styles.css', array('media' => 'all', 'priority' => 150) ); $this->context->controller->registerJavascript('modules-pdfreedeliveryleftamountpro-front', 'modules/'.$this->name.'/views/js/scripts_17.js', array('position' => 'bottom', 'priority' => 1)); } } }