Jump to content

Label on thumb for "Quantity Discount"


metalgdf

Recommended Posts

Hi guys,

so if I have a percentage discount PS put a label on a product thumb in product list, product page, etc.

When I configure a quantity discount PS put nothing like a label on a thumb. Good I need this label like "Quantity Discount".

I tried to use a $product variable but in a product page I have populated $product.discount_type but is empty in product list. I tried also a made a Module but I don't find the correct hook for put a label on a thumb.

This is an example of module script with hookactionProductFlagsModifier but nothing work, I tried also other hook. So someone can help me? With a simple script module, or with a correct array like  $product.discount_type ? Any idea is welcome but I don't want to use a third party module if is possible :)

Thanks

 

<?php
if (!defined('_PS_VERSION_')) {
    exit;
}

class QuantityDiscountBadge extends Module
{
    public function __construct()
    {
        $this->name = 'quantitydiscountbadge';
        $this->version = '1.0.4';
        $this->author = 'Il Tuo Nome';
        $this->need_instance = 0;
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->trans('Badge Sconto Quantità', [], 'Modules.QuantityDiscountBadge.Admin');
        $this->description = $this->trans('Mostra un badge sovrapposto sull\'immagine dei prodotti con sconto quantità.', [], 'Modules.QuantityDiscountBadge.Admin');

        $this->ps_versions_compliancy = ['min' => '8.0.0', 'max' => _PS_VERSION_];
    }

    public function install()
    {
        return parent::install()
            && $this->registerHook('actionProductFlagsModifier') // Hook per posizionare il badge sopra l'immagine
            && $this->registerHook('header'); // Per caricare il CSS
    }

    public function uninstall()
    {
        return parent::uninstall();
    }

    public function hookactionProductFlagsModifier($params)
    {
        // Verifica se il prodotto è passato come parametro
        if (!isset($params['product']) || !isset($params['product']['id_product'])) {
            return '';
        }

        $id_product = (int) $params['product']['id_product'];

        // Verifica se il prodotto ha uno sconto quantità
        if ($this->hasQuantityDiscount($id_product)) {
            return '<ul class="product-flags js-product-flags"><li class="product-flag discount">Sconto Quantità</li></ul>';
        }

        return '';
    }

    public function hookHeader()
    {
        $this->context->controller->registerStylesheet(
            'module-quantitydiscountbadge',
            'modules/' . $this->name . '/views/css/quantitydiscountbadge.css',
            ['media' => 'all', 'priority' => 150]
        );
    }

    private function hasQuantityDiscount($id_product)
    {
        $sql = new DbQuery();
        $sql->select('COUNT(*)');
        $sql->from('specific_price');
        $sql->where('id_product = ' . (int)$id_product);

        return (Db::getInstance()->getValue($sql) > 0);
    }
}

 

Link to comment
Share on other sites

  • 3 months later...
On 2/8/2025 at 11:13 PM, metalgdf said:

Hi guys,

so if I have a percentage discount PS put a label on a product thumb in product list, product page, etc.

When I configure a quantity discount PS put nothing like a label on a thumb. Good I need this label like "Quantity Discount".

I tried to use a $product variable but in a product page I have populated $product.discount_type but is empty in product list. I tried also a made a Module but I don't find the correct hook for put a label on a thumb.

This is an example of module script with hookactionProductFlagsModifier but nothing work, I tried also other hook. So someone can help me? With a simple script module, or with a correct array like  $product.discount_type ? Any idea is welcome but I don't want to use a third party module if is possible :)

Thanks

 

<?php
if (!defined('_PS_VERSION_')) {
    exit;
}

class QuantityDiscountBadge extends Module
{
    public function __construct()
    {
        $this->name = 'quantitydiscountbadge';
        $this->version = '1.0.4';
        $this->author = 'Il Tuo Nome';
        $this->need_instance = 0;
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->trans('Badge Sconto Quantità', [], 'Modules.QuantityDiscountBadge.Admin');
        $this->description = $this->trans('Mostra un badge sovrapposto sull\'immagine dei prodotti con sconto quantità.', [], 'Modules.QuantityDiscountBadge.Admin');

        $this->ps_versions_compliancy = ['min' => '8.0.0', 'max' => _PS_VERSION_];
    }

    public function install()
    {
        return parent::install()
            && $this->registerHook('actionProductFlagsModifier') // Hook per posizionare il badge sopra l'immagine
            && $this->registerHook('header'); // Per caricare il CSS
    }

    public function uninstall()
    {
        return parent::uninstall();
    }

    public function hookactionProductFlagsModifier($params)
    {
        // Verifica se il prodotto è passato come parametro
        if (!isset($params['product']) || !isset($params['product']['id_product'])) {
            return '';
        }

        $id_product = (int) $params['product']['id_product'];

        // Verifica se il prodotto ha uno sconto quantità
        if ($this->hasQuantityDiscount($id_product)) {
            return '<ul class="product-flags js-product-flags"><li class="product-flag discount">Sconto Quantità</li></ul>';
        }

        return '';
    }

    public function hookHeader()
    {
        $this->context->controller->registerStylesheet(
            'module-quantitydiscountbadge',
            'modules/' . $this->name . '/views/css/quantitydiscountbadge.css',
            ['media' => 'all', 'priority' => 150]
        );
    }

    private function hasQuantityDiscount($id_product)
    {
        $sql = new DbQuery();
        $sql->select('COUNT(*)');
        $sql->from('specific_price');
        $sql->where('id_product = ' . (int)$id_product);

        return (Db::getInstance()->getValue($sql) > 0);
    }
}

 

Instead of creating your function, you need to use the PrestaShop default function of the SpecificPrice class.

Function name: getQuantityDiscounts()

File Path: ROOT_DIR/classes/SpecificPrice.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...