Dani
Dani

Reputation: 1

Prestashop 1.7 show banner in product list

can anyone help to me. I have a module for show banners (and stickers) on product. Banner function show a banner only on product page of specific product, and stickers on page of specific product, and product list miniature. (you can see screenshot) original page is: http://miofantasy.it/shampoo/2192-antica-erboristeria-shampoo-girasole-250-ml.html screenshot

I want show the BANNER on product list miniature, beyond that product page, like in next screeshot modified (how already stichers does) screenshot modified original page is: http://miofantasy.it/

I'm don't know about code edit, and i don't know how to do it. Anyone can said to me what i must modify.

Inside a file php of module, I have found this code, that i think manage the banner on product page:

public function hookDisplayProductButtons()
{
    //Its an alternative, use only if productfooter hook is not available.
    // get rid of 1 > 2 condition to activate
    if (Tools::version_compare(_PS_VERSION_, '1.7.0.0', '>=') && 1 > 2) {
        return $this-> hookDisplayFooterProduct();
    }
}

public function hookDisplayFooterProduct()
{
    $type = (int)Configuration::get('sticker_type_val');
    $type = empty($type) ? 1 : $type;
    if ($type > 0) {
        $object = new Stickers();
        $rules = new Rules();
        $id = Tools::getValue('id_product');
        $product = new Product((int)$id, true, $this->context->language->id);
        $category_data = $product->getCategories();
        $stickers_pro = $object->getProductStickers($id);
        **$stickers_banner = $object->getProductBanner($id);**
        $_price = Tools::ps_round($product->price);
        //For Stickers Rules if any matches - Tags
        $tags_exist = Tag::getProductTags((int)$id);
        $new_stickers_colllection = $rules->keyNewExists();
        $is_discounted = (int)$product->isDiscounted($product->id);
        if (!empty($tags_exist)) {
            $stickers_colllection = $rules->keyTagExists($tags_exist);
            if (!empty($stickers_colllection)) {
                foreach ($stickers_colllection as $stick) {
                    array_push($stickers_pro, $object->getSticker($stick));
                }
            }
        }
        //Check for reference match
        if (!empty($product->reference)) {
            $stickers_colllection = $rules->keyRefExists($product->reference);
            if (!empty($stickers_colllection)) {
                foreach ($stickers_colllection as $stick) {
                    array_push($stickers_pro, $object->getSticker($stick));
                }
            }
        }
        //Check for Price match
        if ($_price > 0) {
            $stickers_colllection = $rules->keyPriceExists($_price);
            if (!empty($stickers_colllection)) {
                foreach ($stickers_colllection as $stick) {
                    array_push($stickers_pro, $object->getSticker($stick));
                }
            }
            $_stickers_colllection = $rules->keyPriceGreaterExists($_price);
            if (!empty($_stickers_colllection)) {
                foreach ($_stickers_colllection as $_stick) {
                    array_push($stickers_pro, $object->getSticker($_stick));
                }
            }
        }
        //Check for new Products match
        if (!empty($new_stickers_colllection) && (int)$product->new > 0) {
            foreach ($new_stickers_colllection as $stick) {
                array_push($stickers_pro, $object->getSticker($stick));
            }
        }
        //Check for Dsicounted Product rules
        if ($is_discounted > 0) {
            $stickers_colllection = $rules->keySaleExists();
            foreach ($stickers_colllection as $stick) {
                array_push($stickers_pro, $object->getSticker($stick));
            }
        }
        //Finally check for category rule existance
        $rule_category = $rules->getAllApplicable('category');
        if (count($rule_category) > 0)
        {
            $category_applicable = array();
            foreach ($category_data as $key)
            {
                $return = $rules->getIsCategoryStickerApplicable($key);
                if (!empty($return))
                {
                    $return = array_shift($return);
                    array_push($category_applicable, $return);
                }
            }
            if (count($category_applicable) > 0)
            {
                foreach ($category_applicable as $stick) {
                    array_push($stickers_pro, $object->getSticker($stick));
                }
            }
        }
        //Now check for brands rule existance
        $rule_brands = $rules->getAllApplicable('brand');
        if (count($rule_brands) > 0)
        {
            $stickers_colllection = $rules->keyBrandsExists($rule_brands, (int)$product->id_manufacturer);
            foreach ($stickers_colllection as $stick) {
                array_push($stickers_pro, $object->getSticker($stick));
            }
        }
        //Now check for supplier rule existance
        $rule_supplier = $rules->getAllApplicable('supplier');
        if (count($rule_supplier) > 0 && (int)$product->id_supplier > 0)
        {
            $stickers_colllection = $rules->keySupplierExists($rule_supplier, (int)$product->id_supplier);
            foreach ($stickers_colllection as $stick) {
                array_push($stickers_pro, $object->getSticker($stick));
            }
            //echo '<pre>'; print_r($stickers_colllection); exit;
        }
        $base_image = __PS_BASE_URI__.'img/';
        $position = Configuration::get('sticker_pos');
        $size = Configuration::get('sticker_size');
        $this->context->smarty->assign('base_image', $base_image);
        $this->context->smarty->assign('size', $size);
        $this->context->smarty->assign('position', $position);
        $this->context->smarty->assign('id', $id);
        $this->context->smarty->assign('stickers', $stickers_pro);
        $this->context->smarty->assign('module_dir', _PS_MODULE_DIR_);
        **$this->context->smarty->assign('stickers_banner', $stickers_banner);**
        $force_ssl = (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE'));
        $this->context->smarty->assign(array(
            'base_dir' => _PS_BASE_URL_.__PS_BASE_URI__,
            'base_dir_ssl' => _PS_BASE_URL_SSL_.__PS_BASE_URI__,
            'force_ssl' => $force_ssl
            )
        );
        if (Tools::version_compare(_PS_VERSION_, '1.7.0.0', '>=') == true) {
            return $this->display(__FILE__, 'views/templates/hook/productfooter_17.tpl');
        } else {
            return $this->display(__FILE__, 'views/templates/hook/productfooter.tpl');
        }
    }
}

In bold character stickers_banner that i think manage the banner.

'views/templates/hook/productfooter_17.tpl' is the file where I find the file that i think manage banner layout, like this:

{if !empty($stickers_banner.title)}
{if empty($stickers)}<script type="text/javascript" src="{if $force_ssl == 
1}{$base_dir_ssl|escape:'htmlall':'UTF-8'}{else} 
{$base_dir|escape:'htmlall':'UTF-8'}{/if}js/jquery/jquery-1.11.0.min.js"> 
</script>{/if}
{literal}
<script>
$('.product-price').after({/literal}'<div style="padding:10px 6px; margin- 
bottom:10px; text-align:center;background: 
{$stickers_banner.bg_color|escape:'htmlall':'UTF-8'};color: 
{$stickers_banner.color|escape:'htmlall':'UTF-8'};border:1px solid 
{$stickers_banner.border_color|escape:'htmlall':'UTF-8'};font-family: 
{$stickers_banner.font|escape:'htmlall':'UTF-8'};font-size: 
{$stickers_banner.font_size|escape:'htmlall':'UTF-8'}px;font-weight: 
{$stickers_banner.font_weight|escape:'htmlall':'UTF-8'};"> 
{$stickers_banner.title|escape:'htmlall':'UTF-8'}</div>'{literal});
</script>
{/literal}
{/if}

Now the hook that i think to must modify for show banner in product list;

    public function hookdisplayProductListFunctionalButtons($params)
{
    $id = (int)$params['product']['id_product'];
    $id = ($id <= 0) ? Tools::getValue('id_product') : $id;
    $product = new Product((int)$id, true, $this->context->language->id);
    $category_data = $product->getCategories();
    $type = Configuration::get('sticker_type_val');
    $type = empty($type) ? 1 : $type;
    $stickers_pro = array();
    $object = new Stickers();
    $rules = new Rules();
    $_price = Tools::ps_round($product->price);
    //$page_name = Dispatcher::getInstance()->getController();
    if ($type == 1) {
        $new_stickers_colllection = $rules->keyNewExists();
        $is_discounted = (int)$product->isDiscounted($product->id);
        //For Stickers Rules if any matches
        $tags_exist = Tag::getProductTags((int)$id);
        if (!empty($tags_exist)) {
            $stickers_colllection = $rules->keyTagExists($tags_exist);
            if (!empty($stickers_colllection)) {
                foreach ($stickers_colllection as $stick) {
                    array_push($stickers_pro, $object->getSticker($stick));
                }
            }
        }
        //Check for reference match
        if (!empty($product->reference)) {
            $stickers_colllection = $rules->keyRefExists($product->reference);
            if (!empty($stickers_colllection)) {
                foreach ($stickers_colllection as $stick) {
                    array_push($stickers_pro, $object->getSticker($stick));
                }
            }
        }
        //Check for Price match
        if ($_price > 0) {
            $stickers_colllection = $rules->keyPriceExists($_price);
            if (!empty($stickers_colllection)) {
                foreach ($stickers_colllection as $stick) {
                    array_push($stickers_pro, $object->getSticker($stick));
                }
            }
            $_stickers_colllection = $rules->keyPriceGreaterExists($_price);
            if (!empty($_stickers_colllection)) {
                foreach ($_stickers_colllection as $_stick) {
                    array_push($stickers_pro, $object->getSticker($_stick));
                }
            }
        }
        //Check for new Products match
        if (!empty($new_stickers_colllection) && (int)$product->new > 0) {
            foreach ($new_stickers_colllection as $stick) {
                array_push($stickers_pro, $object->getSticker($stick));
            }
        }
        //Check for Discounted Product rules
        if ($is_discounted > 0) {
            $stickers_colllection = $rules->keySaleExists();
            foreach ($stickers_colllection as $stick) {
                array_push($stickers_pro, $object->getSticker($stick));
            }
        }
        //Finally check for category rule existance
        $rule_category = $rules->getAllApplicable('category');
        if (count($rule_category) > 0)
        {
            $category_applicable = array();
            foreach ($category_data as $key)
            {
                $return = $rules->getIsCategoryStickerApplicable($key);
                if (!empty($return))
                {
                    $return = array_shift($return);
                    array_push($category_applicable, $return);
                }
            }
            if (count($category_applicable) > 0)
            {
                foreach ($category_applicable as $stick) {
                    array_push($stickers_pro, $object->getSticker($stick));
                }
            }
        }
        //Now check for brands rule existance
        $rule_brands = $rules->getAllApplicable('brand');
        if (count($rule_brands) > 0)
        {
            $stickers_colllection = $rules->keyBrandsExists($rule_brands, (int)$product->id_manufacturer);
            foreach ($stickers_colllection as $stick) {
                array_push($stickers_pro, $object->getSticker($stick));
            }
        }
        //Now check for supplier rule existance
        $rule_supplier = $rules->getAllApplicable('supplier');
        if (count($rule_supplier) > 0 && (int)$product->id_supplier > 0)
        {
            $stickers_colllection = $rules->keySupplierExists($rule_supplier, (int)$product->id_supplier);
            foreach ($stickers_colllection as $stick) {
                array_push($stickers_pro, $object->getSticker($stick));
            }
            //echo '<pre>'; print_r($stickers_colllection); exit;
        }
    }
    $position = Configuration::get('sticker_pos');
    $size = Configuration::get('sticker_size');
    $base_image = __PS_BASE_URI__.'img/';
    $this->context->smarty->assign('base_image', $base_image);
    $this->context->smarty->assign('name', $params['product']['name']);
    $this->context->smarty->assign('size', $size);
    $this->context->smarty->assign('position', $position);
    $this->context->smarty->assign('module_dir', _PS_MODULE_DIR_);
    $this->context->smarty->assign('id', $id);
    if ($type == 1) {
        $stickercollection_ = $object->getProductStickers($params['product']['id_product']);
        foreach ($stickercollection_ as $stick) {
            array_push($stickers_pro, $stick);
        }
    }
    $this->context->smarty->assign('stickers', $stickers_pro);
    $force_ssl = (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE'));
    $this->context->smarty->assign(array(
        'base_dir' => _PS_BASE_URL_.__PS_BASE_URI__,
        'base_dir_ssl' => _PS_BASE_URL_SSL_.__PS_BASE_URI__,
        'force_ssl' => $force_ssl
        )
    );
    if (Tools::version_compare(_PS_VERSION_, '1.7.0.0', '>=') == true) {
        return $this->display(__FILE__, 'views/templates/hook/listing_17.tpl');
    } else {
        return $this->display(__FILE__, 'views/templates/hook/listing.tpl');
    }
}

There is also hookdisplayStickers, i don't know if is impotant for this.

What can I modify for show the banner in product list. Please anyone can help me? Thanks


UPDATE: I have add code

    public function hookDisplayProductPriceBlock($params)
{
    if ($params['type'] == 'before_price') { 
    // old_price or unit_price or weight
        $id = (int)$params['product']['id_product'];
        $_POST('id_product') = $id;
        return $this->hookDisplayFooterProduct();
    }
}

below hookdisplayProductListFunctionalButtons, and I have added code for register hook displayProductPriceBlock

public function install()
{
    if (!$this->existsTab($this->tab_class)) {
        if (!$this->addTab($this->tab_class, 0)) {
            return false;
        }
    }
    mkdir(_PS_IMG_DIR_.'stickers', 0777, true);
    if (!parent::install()
        || !$this->installDb()
        || !$this->registerHook('displayCatalogListing')
        || !$this->registerHook('displayProductListFunctionalButtons')
        || !$this->registerHook('displayProductPriceBlock')         
        || !$this->registerHook('displayAdminProductsExtra')
        || !$this->registerHook('actionProductUpdate')
        || !$this->registerHook('displayBackOfficeHeader')
        || !$this->registerHook('displayProductListReviews')
        || !$this->registerHook('displayProductButtons')
        || !$this->registerHook('displayFooterProduct')) {
        return false;
    }
    return true;
}

But when I install module, I have error and prestashop not install module. I have used hookDisplayPriceBlock because I have found already writed hookDisplayProductListReviews :

    public function hookDisplayProductListReviews($params)
{
    if (Tools::version_compare(_PS_VERSION_, '1.7.0.0', '>=')) {
        return $this->hookdisplayProductListFunctionalButtons($params);
    }
}

that i think it redirect prestashop 1.7 to hookdisplayProductListFunctionalButtons. I was thinking, if hookDisplayProductListReviews redirect to hookdisplayProductListFunctionalButtons, is possible to edit hookdisplayProductListFunctionalButtons and add the code for show the banner, like in hookDisplayFooterProduct, where is written specific code for show the Banner? How to do? Thanks

Upvotes: -1

Views: 1105

Answers (1)

Mahdi Shad
Mahdi Shad

Reputation: 1457

You have to modify your banner module to do it.

Probably the problem will be solved by adding new hook (displayProductListFunctionalButtons or displayProductPriceBlock) to this module.

It requires the ability to cretae the module for Prestashop.

UPDATE :

Prestashop seems to have removed HookdisplayProductListFunctionalButtons from version 1.7

So you can use displayProductPriceBlock or displayProductListReviews :

public function HookDisplayProductListReviews($params)
{
    $id = (int)$params['product']['id_product'];
    $_POST('id_product') = $id;
    return $this->hookDisplayFooterProduct();
}

If you want to use displayProductPriceBlock, you have to pay attention that it has several positions:

public function displayProductPriceBlock($params)
{
    if($params['type'] == 'before_price') { // old_price or unit_price or weight
        $id = (int)$params['product']['id_product'];
        $_POST('id_product') = $id;
        return $this->hookDisplayFooterProduct();
    }
}

Tip: Do not forget that new hooks should be registered in the module installation method:

$this->registerHook('HookDisplayProductListReviews');

Upvotes: 0

Related Questions