Prestashop 1.7在产品列表中显示横幅

时间:2018-10-17 10:16:14

标签: prestashop-1.7

谁能帮助我。 我有一个用于在产品上显示横幅(和贴纸)的模块。标语功能仅在特定产品的产品页面上显示标语,在特定产品的页面上显示贴纸,并显示产品列表缩图。 (您可以看到屏幕截图)原始页面是:http://miofantasy.it/shampoo/2192-antica-erboristeria-shampoo-girasole-250-ml.html screenshot

我想在该产品页面之外的产品列表微型上显示BANNER,例如在下一个screeshot中进行修改(stichers的工作方式) screenshot modified 原始页面是:http://miofantasy.it/

我对代码编辑一无所知,也不知道该怎么做。 任何人都可以对我说我必须修改的内容。

在模块的文件php中,我找到了以下代码,我认为可以管理产品页面上的横幅:

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');
        }
    }
}

我认为用粗体字Stickers_banner来管理横幅。

'views / templates / hook / productfooter_17.tpl'是我认为可以管理横幅布局的文件,例如:

{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}

现在我认为必须对产品列表中的展示横幅进行修改的钩子;

    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');
    }
}

也有hookdisplayStickers,我不知道这是否很重要。

我可以修改哪些内容以在产品列表中显示横幅。 请任何人可以帮助我吗? 谢谢


更新: 我有添加代码

    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();
    }
}

在hookdisplayProductListFunctionalButtons下面,我添加了用于注册钩子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;
}

但是当我安装模块时,出现错误,并且prestashop没有安装模块。我使用了hookDisplayPriceBlock,因为我发现已经写了hookDisplayProductListReviews:

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

我认为它会将prestashop 1.7重定向到hookdisplayProductListFunctionalButtons。 我在想,如果hookDisplayProductListReviews重定向到hookdisplayProductListFunctionalButtons,可以编辑hookdisplayProductListFunctionalButtons并添加用于显示横幅的代码,就像在hookDisplayFooterProduct中那样,在哪里写特定的代码来显示横幅? 怎么做? 谢谢

1 个答案:

答案 0 :(得分:0)

您必须修改标题模块才能做到这一点。

可能可以通过向此模块添加新的钩子(displayProductListFunctionalButtons或displayProductPriceBlock)来解决该问题。

它需要具有为Prestashop创建模块的能力。

更新:

Prestashop似乎已从1.7版中删除了HookdisplayProductListFunctionalButtons

因此您可以使用displayProductPriceBlock或displayProductListReviews:

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

如果要使用displayProductPriceBlock,则必须注意它具有多个位置:

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();
    }
}

提示:不要忘记应该在模块安装方法中注册新的钩子:

$this->registerHook('HookDisplayProductListReviews');