Jump to content

Fourchette de prix sur fiches produits


 Share

Recommended Posts

Bonjour à tous,

Je souhaiterai afficher sur mes fiches produits une fourchette de prix.

La fourchette sera bien sûr en fonction du prix des déclinaisons.

J'ai essayé de trouver un raccourci à inclure dans le fichier product-prices.tpl pour afficher le prix max et le prix min mais je n'ai rien trouvé...

Auriez-vous une idée ?

Merci par avance.

Capture.PNG

Share this post


Link to post
Share on other sites

C'est faisable en mettant un peu les mains dans le code, voici comment j'ai fait (avec un PrestaShop 1.6) :

Il faut modifier la fonction getProductProperties de la classe Product pour ajouter le prix des déclinaisons. Pour cela, il faut faire un override de la classe Product :

Créer un fichier Product.php et le placer dans /override/classes/

<?php

class Product extends ProductCore {

	
}

Copier la fonction getProductProperties qui se trouve dans le fichier /classes/Product.php et l'ajouter dans le fichier d'override que l'on vient de créer, ce qui donne :

<?php

class Product extends ProductCore {

	public static function getProductProperties($id_lang, $row, Context $context = null)
	{
	    Hook::exec('actionGetProductPropertiesBefore', [
	        'id_lang'   => $id_lang,
	        'product'   => &$row,
	        'context'   => $context
	    ]);

	    if (!$row['id_product']) {
	        return false;
	    }

	    if ($context == null) {
	        $context = Context::getContext();
	    }

	    $id_product_attribute = $row['id_product_attribute'] = (!empty($row['id_product_attribute']) ? (int)$row['id_product_attribute'] : null);

	    // Product::getDefaultAttribute is only called if id_product_attribute is missing from the SQL query at the origin of it:
	    // consider adding it in order to avoid unnecessary queries
	    $row['allow_oosp'] = Product::isAvailableWhenOutOfStock($row['out_of_stock']);
	    if (Combination::isFeatureActive() && $id_product_attribute === null
	        && ((isset($row['cache_default_attribute']) && ($ipa_default = $row['cache_default_attribute']) !== null)
	            || ($ipa_default = Product::getDefaultAttribute($row['id_product'], !$row['allow_oosp'])))) {
	        $id_product_attribute = $row['id_product_attribute'] = $ipa_default;
	    }
	    if (!Combination::isFeatureActive() || !isset($row['id_product_attribute'])) {
	        $id_product_attribute = $row['id_product_attribute'] = 0;
	    }

	    // Tax
	    $usetax = !Tax::excludeTaxeOption();

	    $cache_key = $row['id_product'].'-'.$id_product_attribute.'-'.$id_lang.'-'.(int)$usetax;
	    if (isset($row['id_product_pack'])) {
	        $cache_key .= '-pack'.$row['id_product_pack'];
	    }

	    if (isset(self::$producPropertiesCache[$cache_key])) {
	        return array_merge($row, self::$producPropertiesCache[$cache_key]);
	    }

	    // Datas
	    $row['category'] = Category::getLinkRewrite((int)$row['id_category_default'], (int)$id_lang);
	    $row['category_name'] = Db::getInstance()->getValue('SELECT name FROM '._DB_PREFIX_.'category_lang WHERE id_shop = '.(int)$context->shop->id.' AND id_lang = '.(int)$id_lang.' AND id_category = '.(int)$row['id_category_default']);
	    $row['link'] = $context->link->getProductLink((int)$row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);

	    $row['attribute_price'] = 0;
	    if ($id_product_attribute) {
	        $row['attribute_price'] = (float)Combination::getPrice($id_product_attribute);
	    }

	    if (isset($row['quantity_wanted'])) {
	        // 'quantity_wanted' may very well be zero even if set
	        $quantity = max((int)$row['minimal_quantity'], (int)$row['quantity_wanted']);
	    } else {
	        $quantity = (int)$row['minimal_quantity'];
	    }

	    $row['price_tax_exc'] = Product::getPriceStatic(
	        (int)$row['id_product'],
	        false,
	        $id_product_attribute,
	        (self::$_taxCalculationMethod == PS_TAX_EXC ? 2 : 6),
	        null,
	        false,
	        true,
	        $quantity
	    );

	    if (self::$_taxCalculationMethod == PS_TAX_EXC) {
	        $row['price_tax_exc'] = Tools::ps_round($row['price_tax_exc'], 2);
	        $row['price'] = Product::getPriceStatic(
	            (int)$row['id_product'],
	            true,
	            $id_product_attribute,
	            6,
	            null,
	            false,
	            true,
	            $quantity
	        );
	        $row['price_without_reduction'] = Product::getPriceStatic(
	            (int)$row['id_product'],
	            false,
	            $id_product_attribute,
	            2,
	            null,
	            false,
	            false,
	            $quantity
	        );
	    } else {
	        $row['price'] = Tools::ps_round(
	            Product::getPriceStatic(
	                (int)$row['id_product'],
	                true,
	                $id_product_attribute,
	                6,
	                null,
	                false,
	                true,
	                $quantity
	            ),
	            (int) Configuration::get('PS_PRICE_DISPLAY_PRECISION')
	        );
	        $row['price_without_reduction'] = Product::getPriceStatic(
	            (int)$row['id_product'],
	            true,
	            $id_product_attribute,
	            6,
	            null,
	            false,
	            false,
	            $quantity
	        );
	    }

	    $row['reduction'] = Product::getPriceStatic(
	        (int)$row['id_product'],
	        (bool)$usetax,
	        $id_product_attribute,
	        6,
	        null,
	        true,
	        true,
	        $quantity,
	        true,
	        null,
	        null,
	        null,
	        $specific_prices
	    );

	    $row['specific_prices'] = $specific_prices;

	    $row['quantity'] = Product::getQuantity(
	        (int)$row['id_product'],
	        0,
	        isset($row['cache_is_pack']) ? $row['cache_is_pack'] : null,
	        $context->cart
	    );

	    $row['quantity_all_versions'] = $row['quantity'];

	    if ($row['id_product_attribute']) {
	        $row['quantity'] = Product::getQuantity(
	            (int)$row['id_product'],
	            $id_product_attribute,
	            isset($row['cache_is_pack']) ? $row['cache_is_pack'] : null,
	            $context->cart
	        );

	        $row['available_date'] = Product::getAvailableDate(
	            (int)$row['id_product'],
	            $id_product_attribute
	        );
	    }

	    $row['id_image'] = Product::defineProductImage($row, $id_lang);
	    $row['features'] = Product::getFrontFeaturesStatic((int)$id_lang, $row['id_product']);

	    $row['attachments'] = array();
	    if (!isset($row['cache_has_attachments']) || $row['cache_has_attachments']) {
	        $row['attachments'] = Product::getAttachmentsStatic((int)$id_lang, $row['id_product']);
	    }

	    $row['virtual'] = ((!isset($row['is_virtual']) || $row['is_virtual']) ? 1 : 0);

	    // Pack management
	    $row['pack'] = (!isset($row['cache_is_pack']) ? Pack::isPack($row['id_product']) : (int)$row['cache_is_pack']);
	    $row['packItems'] = $row['pack'] ? Pack::getItemTable($row['id_product'], $id_lang) : array();
	    $row['nopackprice'] = $row['pack'] ? Pack::noPackPrice($row['id_product']) : 0;

	    if ($row['pack'] && !Pack::isInStock($row['id_product'], $quantity, $context->cart)) {
	        $row['quantity'] = 0;
	    }

	    $row['customization_required'] = false;
	    if (isset($row['customizable']) && $row['customizable'] && Customization::isFeatureActive()) {
	        if (count(Product::getRequiredCustomizableFieldsStatic((int)$row['id_product']))) {
	            $row['customization_required'] = true;
	        }
	    }

	    $attributes = Product::getAttributesParams($row['id_product'], $row['id_product_attribute']);

	    foreach ($attributes as $attribute) {
	        $row['attributes'][$attribute['id_attribute_group']] = $attribute;
	    }

	    $row = Product::getTaxesInformations($row, $context);

	    $row['ecotax_rate'] = (float)Tax::getProductEcotaxRate($context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});

	    Hook::exec('actionGetProductPropertiesAfter', [
	        'id_lang'   => $id_lang,
	        'product'   => &$row,
	        'context'   => $context
	    ]);

	    $combination = new Combination($id_product_attribute);

	    if (0 != $combination->unit_price_impact && 0 != $row['unit_price_ratio']) {
	        $unitPrice = ($row['price_tax_exc'] / $row['unit_price_ratio']) + $combination->unit_price_impact;
	        $row['unit_price_ratio'] = $row['price_tax_exc'] / $unitPrice;
	    }

	    $row['unit_price'] = ($row['unit_price_ratio'] != 0  ? $row['price'] / $row['unit_price_ratio'] : 0);

	    self::$producPropertiesCache[$cache_key] = $row;
	    return self::$producPropertiesCache[$cache_key];
	}
}

Dans cette fonction, ajouter le bout de code pour récupérer le prix des déclinaisons :

On récupère le prix de toutes les déclinaisons, pour chaque prix on calcul le ttc, et on regarde si le prix de cette déclinaison est le plus bas ou le plus haut

$row['lowest_price'] = 0;
$row['highest_price'] = 0;

if ($id_product_attribute)
{
	$declinaisons = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
		SELECT price, id_product_attribute
		FROM '._DB_PREFIX_.'product_attribute pa
		WHERE pa.id_product = '.(int)$row['id_product']
	);

	foreach ($declinaisons as $key => $value) {

		$tmp_price = Tools::ps_round(
            Product::getPriceStatic(
                (int)$row['id_product'],
                true,
                $value['id_product_attribute'],
                6,
                null,
                false,
                true,
                1
            ),
            (int) Configuration::get('PS_PRICE_DISPLAY_PRECISION')
        );

		if ($row['lowest_price'] == 0 || $tmp_price < $row['lowest_price'])
			$row['lowest_price'] = $tmp_price;

		if ($row['highest_price'] == 0 || $tmp_price > $row['highest_price'])
			$row['highest_price'] = $tmp_price;
	}
}

Du coup, dans ton template, tu auras accès au prix le plus bas via $product.lowest_price et le plus haut via $product.highest_price

Tu peux par exemple afficher le prix comme ça dans ton template :

{if $product.id_product_attribute!='0'}{$product.lowest_price} € - {$product.highest_price} €{else}{$product.price}{/if}

Voici le fichier d'override complet que j'utilise, c'est à dire la copie de la fonction getProductProperties, avec le bout de code ajouté pour récupérer le prix des déclinaison, qui se trouve entre les commentaires // Recuperation des prix des declinaisons et // Fin recuperation des prix des declinaisons :

<?php

class Product extends ProductCore {

	public static function getProductProperties($id_lang, $row, Context $context = null)
	{
	    Hook::exec('actionGetProductPropertiesBefore', [
	        'id_lang'   => $id_lang,
	        'product'   => &$row,
	        'context'   => $context
	    ]);

	    if (!$row['id_product']) {
	        return false;
	    }

	    if ($context == null) {
	        $context = Context::getContext();
	    }

	    $id_product_attribute = $row['id_product_attribute'] = (!empty($row['id_product_attribute']) ? (int)$row['id_product_attribute'] : null);

	    // Recuperation des prix des declinaisons
	    $row['lowest_price'] = 0;
	    $row['highest_price'] = 0;

	    if ($id_product_attribute)
	    {
			$declinaisons = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
				SELECT price, id_product_attribute
				FROM '._DB_PREFIX_.'product_attribute pa
				WHERE pa.id_product = '.(int)$row['id_product']
			);

			foreach ($declinaisons as $key => $value) {

				$tmp_price = Tools::ps_round(
		            Product::getPriceStatic(
		                (int)$row['id_product'],
		                true,
		                $value['id_product_attribute'],
		                6,
		                null,
		                false,
		                true,
		                1
		            ),
		            (int) Configuration::get('PS_PRICE_DISPLAY_PRECISION')
		        );

				if ($row['lowest_price'] == 0 || $tmp_price < $row['lowest_price'])
					$row['lowest_price'] = $tmp_price;

				if ($row['highest_price'] == 0 || $tmp_price > $row['highest_price'])
					$row['highest_price'] = $tmp_price;
			}
	    }
	    // Fin recuperation des prix des declinaisons

	    // Product::getDefaultAttribute is only called if id_product_attribute is missing from the SQL query at the origin of it:
	    // consider adding it in order to avoid unnecessary queries
	    $row['allow_oosp'] = Product::isAvailableWhenOutOfStock($row['out_of_stock']);
	    if (Combination::isFeatureActive() && $id_product_attribute === null
	        && ((isset($row['cache_default_attribute']) && ($ipa_default = $row['cache_default_attribute']) !== null)
	            || ($ipa_default = Product::getDefaultAttribute($row['id_product'], !$row['allow_oosp'])))) {
	        $id_product_attribute = $row['id_product_attribute'] = $ipa_default;
	    }
	    if (!Combination::isFeatureActive() || !isset($row['id_product_attribute'])) {
	        $id_product_attribute = $row['id_product_attribute'] = 0;
	    }

	    // Tax
	    $usetax = !Tax::excludeTaxeOption();

	    $cache_key = $row['id_product'].'-'.$id_product_attribute.'-'.$id_lang.'-'.(int)$usetax;
	    if (isset($row['id_product_pack'])) {
	        $cache_key .= '-pack'.$row['id_product_pack'];
	    }

	    if (isset(self::$producPropertiesCache[$cache_key])) {
	        return array_merge($row, self::$producPropertiesCache[$cache_key]);
	    }

	    // Datas
	    $row['category'] = Category::getLinkRewrite((int)$row['id_category_default'], (int)$id_lang);
	    $row['category_name'] = Db::getInstance()->getValue('SELECT name FROM '._DB_PREFIX_.'category_lang WHERE id_shop = '.(int)$context->shop->id.' AND id_lang = '.(int)$id_lang.' AND id_category = '.(int)$row['id_category_default']);
	    $row['link'] = $context->link->getProductLink((int)$row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);

	    $row['attribute_price'] = 0;
	    if ($id_product_attribute) {
	        $row['attribute_price'] = (float)Combination::getPrice($id_product_attribute);
	    }

	    if (isset($row['quantity_wanted'])) {
	        // 'quantity_wanted' may very well be zero even if set
	        $quantity = max((int)$row['minimal_quantity'], (int)$row['quantity_wanted']);
	    } else {
	        $quantity = (int)$row['minimal_quantity'];
	    }

	    $row['price_tax_exc'] = Product::getPriceStatic(
	        (int)$row['id_product'],
	        false,
	        $id_product_attribute,
	        (self::$_taxCalculationMethod == PS_TAX_EXC ? 2 : 6),
	        null,
	        false,
	        true,
	        $quantity
	    );

	    if (self::$_taxCalculationMethod == PS_TAX_EXC) {
	        $row['price_tax_exc'] = Tools::ps_round($row['price_tax_exc'], 2);
	        $row['price'] = Product::getPriceStatic(
	            (int)$row['id_product'],
	            true,
	            $id_product_attribute,
	            6,
	            null,
	            false,
	            true,
	            $quantity
	        );
	        $row['price_without_reduction'] = Product::getPriceStatic(
	            (int)$row['id_product'],
	            false,
	            $id_product_attribute,
	            2,
	            null,
	            false,
	            false,
	            $quantity
	        );
	    } else {
	        $row['price'] = Tools::ps_round(
	            Product::getPriceStatic(
	                (int)$row['id_product'],
	                true,
	                $id_product_attribute,
	                6,
	                null,
	                false,
	                true,
	                $quantity
	            ),
	            (int) Configuration::get('PS_PRICE_DISPLAY_PRECISION')
	        );
	        $row['price_without_reduction'] = Product::getPriceStatic(
	            (int)$row['id_product'],
	            true,
	            $id_product_attribute,
	            6,
	            null,
	            false,
	            false,
	            $quantity
	        );
	    }

	    $row['reduction'] = Product::getPriceStatic(
	        (int)$row['id_product'],
	        (bool)$usetax,
	        $id_product_attribute,
	        6,
	        null,
	        true,
	        true,
	        $quantity,
	        true,
	        null,
	        null,
	        null,
	        $specific_prices
	    );

	    $row['specific_prices'] = $specific_prices;

	    $row['quantity'] = Product::getQuantity(
	        (int)$row['id_product'],
	        0,
	        isset($row['cache_is_pack']) ? $row['cache_is_pack'] : null,
	        $context->cart
	    );

	    $row['quantity_all_versions'] = $row['quantity'];

	    if ($row['id_product_attribute']) {
	        $row['quantity'] = Product::getQuantity(
	            (int)$row['id_product'],
	            $id_product_attribute,
	            isset($row['cache_is_pack']) ? $row['cache_is_pack'] : null,
	            $context->cart
	        );

	        $row['available_date'] = Product::getAvailableDate(
	            (int)$row['id_product'],
	            $id_product_attribute
	        );
	    }

	    $row['id_image'] = Product::defineProductImage($row, $id_lang);
	    $row['features'] = Product::getFrontFeaturesStatic((int)$id_lang, $row['id_product']);

	    $row['attachments'] = array();
	    if (!isset($row['cache_has_attachments']) || $row['cache_has_attachments']) {
	        $row['attachments'] = Product::getAttachmentsStatic((int)$id_lang, $row['id_product']);
	    }

	    $row['virtual'] = ((!isset($row['is_virtual']) || $row['is_virtual']) ? 1 : 0);

	    // Pack management
	    $row['pack'] = (!isset($row['cache_is_pack']) ? Pack::isPack($row['id_product']) : (int)$row['cache_is_pack']);
	    $row['packItems'] = $row['pack'] ? Pack::getItemTable($row['id_product'], $id_lang) : array();
	    $row['nopackprice'] = $row['pack'] ? Pack::noPackPrice($row['id_product']) : 0;

	    if ($row['pack'] && !Pack::isInStock($row['id_product'], $quantity, $context->cart)) {
	        $row['quantity'] = 0;
	    }

	    $row['customization_required'] = false;
	    if (isset($row['customizable']) && $row['customizable'] && Customization::isFeatureActive()) {
	        if (count(Product::getRequiredCustomizableFieldsStatic((int)$row['id_product']))) {
	            $row['customization_required'] = true;
	        }
	    }

	    $attributes = Product::getAttributesParams($row['id_product'], $row['id_product_attribute']);

	    foreach ($attributes as $attribute) {
	        $row['attributes'][$attribute['id_attribute_group']] = $attribute;
	    }

	    $row = Product::getTaxesInformations($row, $context);

	    $row['ecotax_rate'] = (float)Tax::getProductEcotaxRate($context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});

	    Hook::exec('actionGetProductPropertiesAfter', [
	        'id_lang'   => $id_lang,
	        'product'   => &$row,
	        'context'   => $context
	    ]);

	    $combination = new Combination($id_product_attribute);

	    if (0 != $combination->unit_price_impact && 0 != $row['unit_price_ratio']) {
	        $unitPrice = ($row['price_tax_exc'] / $row['unit_price_ratio']) + $combination->unit_price_impact;
	        $row['unit_price_ratio'] = $row['price_tax_exc'] / $unitPrice;
	    }

	    $row['unit_price'] = ($row['unit_price_ratio'] != 0  ? $row['price'] / $row['unit_price_ratio'] : 0);

	    self::$producPropertiesCache[$cache_key] = $row;
	    return self::$producPropertiesCache[$cache_key];
	}
}

Attention à bien copier la fonction getProductProperties de ta version de PrestaShop, qui ne sera pas forcément identique en fonction de la version de PrestaShop que tu utilises.

Pour que les modifications soient prises en compte, penser à vider le cache via l'administration du site

J'espère que c'est clair 😀

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More