Jump to content

Quantity discounts - change product price on product page when selected quantity


hakeryk2

Recommended Posts

Hello! :)

I have Prestashop 1.6.1.18 and I heard that when You have set quantity discounts then price in front office product page should change when customer will set quantity before adding product to cart.

Example: Set 4€ discount when 4 or more in cart. On product page I have 1 pcsand regular price 49€. When I change with this + button to 4 pcs price on product page should be 45€ but it is still 49. 

Proper price is shown when added to cart in cart page but not on product page. I only see this table on the bottom.

Any idea what is wrong? Console browser is not saying anything. Where in product.js is code responsible for this? I can't find code responsible for this action in this file. 

Actually - I can  try do this in jquery but I am asking if this is built in prestashop to not make effort :)

Link to comment
Share on other sites

35 minutes ago, hakeryk2 said:

Hello! :)

I have Prestashop 1.6.1.18 and I heard that when You have set quantity discounts then price in front office product page should change when customer will set quantity before adding product to cart.

Example: Set 4€ discount when 4 or more in cart. On product page I have 1 pcsand regular price 49€. When I change with this + button to 4 pcs price on product page should be 45€ but it is still 49. 

Proper price is shown when added to cart in cart page but not on product page. I only see this table on the bottom.

Any idea what is wrong? Console browser is not saying anything. Where in product.js is code responsible for this? I can't find code responsible for this action in this file. 

Actually - I can  try do this in jquery but I am asking if this is built in prestashop to not make effort :)

 

Yes, default theme have this functionality. Maybe it's a problem with your theme?

 

Link to comment
Share on other sites

Just now, joseantgv said:

 

Yes, default theme have this functionality. Maybe it's a problem with your theme?

 

It is edited just a little bit so this is why I asked about where are lines responsible for this behavior :) I will go and check. I saw displayDiscounts(), updateDiscountTable() functions in product.js but I didn't see any function responsible for changing price. I compared mine product.js to the same file from original default-bootstrap 1.6.1.18 product.js file and I don't see any difference.

Which functions are reposible for this change?

Link to comment
Share on other sites

Ok, I didn' mentioned that I want this for product that have combinations and multiple conditions, 2 for all for combinations and 1 for specific attribute.

I found out something weird. I downloaded 1.6.1.18 package and there was in function in front/ProductController.php assignPriceAndTax() and in original 1.6.1.18 file it looks like this. Look for line

 

protected function assignPriceAndTax()
    {
        $id_customer = (isset($this->context->customer) ? (int)$this->context->customer->id : 0);
        $id_group = (int)Group::getCurrent()->id;
        $id_country = $id_customer ? (int)Customer::getCurrentCountry($id_customer) : (int)Tools::getCountry();

        $group_reduction = GroupReduction::getValueForProduct($this->product->id, $id_group);
        if ($group_reduction === false) {
            $group_reduction = Group::getReduction((int)$this->context->cookie->id_customer) / 100;
        }

        // Tax
        $tax = (float)$this->product->getTaxesRate(new Address((int)$this->context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}));
        $this->context->smarty->assign('tax_rate', $tax);

        $product_price_with_tax = Product::getPriceStatic($this->product->id, true, null, 6);
        if (Product::$_taxCalculationMethod == PS_TAX_INC) {
            $product_price_with_tax = Tools::ps_round($product_price_with_tax, 2);
        }
        $product_price_without_eco_tax = (float)$product_price_with_tax - $this->product->ecotax;

        $ecotax_rate = (float)Tax::getProductEcotaxRate($this->context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
        if (Product::$_taxCalculationMethod == PS_TAX_INC && (int)Configuration::get('PS_TAX')) {
            $ecotax_tax_amount = Tools::ps_round($this->product->ecotax * (1 + $ecotax_rate / 100), 2);
        }
	else {
	    $ecotax_tax_amount = Tools::ps_round($this->product->ecotax, 2);
	}

        $id_currency = (int)$this->context->cookie->id_currency;
        $id_product = (int)$this->product->id;
        $id_shop = $this->context->shop->id;

        $quantity_discounts = SpecificPrice::getQuantityDiscounts($id_product, $id_shop, $id_currency, $id_country, $id_group, null, true, (int)$this->context->customer->id);
        foreach ($quantity_discounts as &$quantity_discount) {
            if (!isset($quantity_discount['base_price'])) {
                $quantity_discount['base_price'] = 0;
            }
            if ($quantity_discount['id_product_attribute']) {
                $quantity_discount['base_price'] = $this->product->getPrice(Product::$_taxCalculationMethod == PS_TAX_INC, $quantity_discount['id_product_attribute']);

                $combination = new Combination((int)$quantity_discount['id_product_attribute']);
                $attributes = $combination->getAttributesName((int)$this->context->language->id);
                foreach ($attributes as $attribute) {
                    $quantity_discount['attributes'] = $attribute['name'].' - ';
                }
                $quantity_discount['attributes'] = rtrim($quantity_discount['attributes'], ' - ');
            }
            if ((int)$quantity_discount['id_currency'] == 0 && $quantity_discount['reduction_type'] == 'amount') {
                $quantity_discount['reduction'] = Tools::convertPriceFull($quantity_discount['reduction'], null, Context::getContext()->currency);
            }
        }

        $address = new Address($this->context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
        $this->context->smarty->assign(array(
            'quantity_discounts' => $this->formatQuantityDiscounts($quantity_discounts, null, (float)$tax, $ecotax_tax_amount),
            'ecotax_tax_inc' => $ecotax_tax_amount,
            'ecotax_tax_exc' => Tools::ps_round($this->product->ecotax, 2),
            'ecotaxTax_rate' => $ecotax_rate,
            'productPriceWithoutEcoTax' => (float)$product_price_without_eco_tax,
            'group_reduction' => $group_reduction,
            'no_tax' => Tax::excludeTaxeOption() || !$this->product->getTaxesRate($address),
            'ecotax' => (!count($this->errors) && $this->product->ecotax > 0 ? Tools::convertPrice((float)$this->product->ecotax) : 0),
            'tax_enabled' => Configuration::get('PS_TAX') && !Configuration::get('AEUC_LABEL_TAX_INC_EXC'),
            'customer_group_without_tax' => Group::getPriceDisplayMethod($this->context->customer->id_default_group),
        ));
    }

There is a code

if (!isset($quantity_discount['base_price'])) {
	$quantity_discount['base_price'] = 0;
}


Why? I changed this to 

$quantity_discount['base_price'] = $this->product->getPrice(Product::$_taxCalculationMethod == PS_TAX_INC, $quantity_discount['id_product_attribute']);

due to some github solution and now everything works ok.

And if someone is looking for code responsible for this in product.js this is in findSpecificPrice() function.

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...