Jump to content

Combination and specific price - Add impact to discounted price


raybeenas
 Share

Recommended Posts

Hello! I'm sorry for my horrible english!

I have a problem with combination impact price and specific price. For example here:

the product is 3.150,00 €, with 30% discount become 2.205,00 €.

 

If I choiche attribute "Pianetto di lavoro inox" with value "Sì - 170,00€", price become 3.320,00€, but with discount become 2.324,00 €.

 

I wish the attribute price was applied to the sum granted ((3.150-30%)+170) and not to the gross ((3.150+170)-30%). Is it possible?

 

Thanks in advance!!!

Edited by raybeenas (see edit history)

Share this post


Link to post
Share on other sites

Ok, I resolved in part: in classes/Product.php, in function "priceCalculation", I moved this code:
 

if (is_array($result) && (!$specific_price || !$specific_price['id_product_attribute'] || $specific_price['price'] < 0)) {
            $attribute_price = Tools::convertPrice($result['attribute_price'] !== null ? (float)$result['attribute_price'] : 0, $id_currency);
            // If you want the default combination, please use NULL value instead
            if ($id_product_attribute !== false) {
                $price += $attribute_price;
            }
        }

at the end of the function, before:

$price = Tools::ps_round($price, $decimals);

In this way, when I add product to cart, price is correct. But in product page, when attribute value change, price is wrong (it is still calculated on the base price), so I have to edit the file "product.js" to resolve the problem.

 

If anyone has any suggestions expose them as well.

 

Share this post


Link to post
Share on other sites

SOLVED!!! I modified Product.php and product.js and that's all!!!

 

Product.php:

in function "priceCalculation" move this code:

 // Attribute price
        if (is_array($result) && (!$specific_price || !$specific_price['id_product_attribute'] || $specific_price['price'] < 0)) {
            $attribute_price = Tools::convertPrice($result['attribute_price'] !== null ? (float)$result['attribute_price'] : 0, $id_currency);
            // If you want the default combination, please use NULL value instead
            if ($id_product_attribute !== false) {
                $price += $attribute_price;
            }
        }
        
                        

        // Tax
        $address->id_country = $id_country;
        $address->id_state = $id_state;
        $address->postcode = $zipcode;

        $tax_manager = TaxManagerFactory::getManager($address, Product::getIdTaxRulesGroupByIdProduct((int)$id_product, $context));
        $product_tax_calculator = $tax_manager->getTaxCalculator();

// Add Tax
        if ($use_tax) {
            $price = $product_tax_calculator->addTaxes($price);
        }

before:

if ($only_reduc) {
            return Tools::ps_round($specific_price_reduction, $decimals);
        }

product.js:

 

modify function "updatePrice" in this way:

function updatePrice()
{
    // Get combination prices
    var combID = $('#idCombination').val();
    var combination = combinationsFromController[combID];
    if (typeof combination == 'undefined')
        return;

    // Set product (not the combination) base price
    var basePriceWithoutTax = +productPriceTaxExcluded;
    var basePriceWithTax = +productPriceTaxIncluded;
    var priceWithGroupReductionWithoutTax = 0;

    priceWithGroupReductionWithoutTax = basePriceWithoutTax * (1 - groupReduction);

    // Apply combination price impact (only if there is no specific price)
    // 0 by default, +x if price is inscreased, -x if price is decreased
    

    var priceWithDiscountsWithoutTax = basePriceWithoutTax;
    var priceWithDiscountsWithTax = basePriceWithTax;

    if (default_eco_tax)
    {
        // combination.ecotax doesn't modify the price but only the display
        priceWithDiscountsWithoutTax = priceWithDiscountsWithoutTax + default_eco_tax * (1 + ecotaxTax_rate / 100);
        priceWithDiscountsWithTax = priceWithDiscountsWithTax + default_eco_tax * (1 + ecotaxTax_rate / 100);
        basePriceWithTax = basePriceWithTax + default_eco_tax * (1 + ecotaxTax_rate / 100);
        basePriceWithoutTax = basePriceWithoutTax + default_eco_tax * (1 + ecotaxTax_rate / 100);
    }

    // Apply specific price (discount)
    // We only apply percentage discount and discount amount given before tax
    // Specific price give after tax will be handled after taxes are added
    if (combination.specific_price && combination.specific_price.reduction > 0)
    {
        if (combination.specific_price.reduction_type == 'amount')
        {
            if (typeof combination.specific_price.reduction_tax !== 'undefined' && combination.specific_price.reduction_tax === "0")
            {
                var reduction = combination.specific_price.reduction;
                if (combination.specific_price.id_currency == 0)
                    reduction = reduction * currencyRate * (1 - groupReduction);
                priceWithDiscountsWithoutTax -= reduction;
                priceWithDiscountsWithTax -= reduction * (taxRate/100 + 1);
            }
        }
        else if (combination.specific_price.reduction_type == 'percentage')
        {
            console.log(combination);
            priceWithDiscountsWithoutTax = priceWithDiscountsWithoutTax * (1 - +combination.specific_price.reduction);
            priceWithDiscountsWithTax = priceWithDiscountsWithTax * (1 - +combination.specific_price.reduction);
        }
    }
    priceWithDiscountsWithoutTax = priceWithDiscountsWithoutTax + +combination.price;
    priceWithDiscountsWithTax = priceWithDiscountsWithTax + +combination.price * (taxRate/100 + 1);

    // Apply Tax if necessary
    if (noTaxForThisProduct || customerGroupWithoutTax)
    {
        basePriceDisplay = basePriceWithoutTax;
        priceWithDiscountsDisplay = priceWithDiscountsWithoutTax;
    }
    else
    {
        basePriceDisplay = basePriceWithTax;
        priceWithDiscountsDisplay = priceWithDiscountsWithTax;
    }

    // If the specific price was given after tax, we apply it now
    if (combination.specific_price && combination.specific_price.reduction > 0)
    {
        if (combination.specific_price.reduction_type == 'amount')
        {
            if (typeof combination.specific_price.reduction_tax === 'undefined'
                || (typeof combination.specific_price.reduction_tax !== 'undefined' && combination.specific_price.reduction_tax === '1'))
            {
                var reduction = combination.specific_price.reduction;

                if (typeof specific_currency !== 'undefined' && specific_currency && parseInt(combination.specific_price.id_currency) && combination.specific_price.id_currency != currency.id)
                    reduction = reduction / currencyRate;
                else if(!specific_currency)
                    reduction = reduction * currencyRate;

                if (typeof groupReduction !== 'undefined' && groupReduction > 0)
                    reduction *= 1 - parseFloat(groupReduction);

                priceWithDiscountsDisplay -= reduction;
                // We recalculate the price without tax in order to keep the data consistency
                priceWithDiscountsWithoutTax = priceWithDiscountsDisplay - reduction * ( 1/(1+taxRate/100) );
            }
        }
    }
    
    console.log("prezzo scontato: "+priceWithDiscountsWithoutTax);

    if (priceWithDiscountsDisplay < 0)
    {
        priceWithDiscountsDisplay = 0;
    }

    // Compute discount value and percentage
    // Done just before display update so we have final prices
    if (basePriceDisplay != priceWithDiscountsDisplay)
    {
        var discountValue = basePriceDisplay - priceWithDiscountsDisplay;
        var discountPercentage = (1-(priceWithDiscountsDisplay/basePriceDisplay))*100;
    }

    var unit_impact = +combination.unit_impact;
    if (productUnitPriceRatio > 0 || unit_impact)
    {
        if (unit_impact)
        {
            baseUnitPrice = productBasePriceTaxExcl / productUnitPriceRatio;
            unit_price = baseUnitPrice + unit_impact;

            if (!noTaxForThisProduct || !customerGroupWithoutTax)
                unit_price = unit_price * (taxRate/100 + 1);
        }
        else
            unit_price = priceWithDiscountsDisplay / productUnitPriceRatio;
    }

    /*  Update the page content, no price calculation happens after */

    // Hide everything then show what needs to be shown
    $('#reduction_percent').hide();
    $('#reduction_amount').hide();
    $('#old_price, #old_price_display, #old_price_display_taxes').hide();
    $('.price-ecotax').hide();
    $('.unit-price').hide();

    if (priceWithDiscountsDisplay > 0)
    {
        $('#our_price_display').text(formatCurrency(priceWithDiscountsDisplay, currencyFormat, currencySign, currencyBlank)).trigger('change');
    }
    else
    {
        $('#our_price_display').text(formatCurrency(0, currencyFormat, currencySign, currencyBlank)).trigger('change');
    }

    // If the calculated price (after all discounts) is different than the base price
    // we show the old price striked through

    if (priceWithDiscountsDisplay.toFixed(2) != basePriceDisplay.toFixed(2))
    {
        $('#old_price_display span.price').text(formatCurrency(basePriceDisplay, currencyFormat, currencySign, currencyBlank));
        $('#old_price, #old_price_display, #old_price_display_taxes').removeClass('hidden').show();

        // Then if it's not only a group reduction we display the discount in red box
        if (priceWithDiscountsWithoutTax != priceWithGroupReductionWithoutTax)
        {
            if (combination.specific_price.reduction_type == 'amount')
            {
                $('#reduction_amount_display').html('-' + formatCurrency(discountValue, currencyFormat, currencySign, currencyBlank));
                $('#reduction_amount').show();
            }
            else
            {
                var toFix = 2;
                if ((parseFloat(discountPercentage).toFixed(2) - parseFloat(discountPercentage).toFixed(0)) == 0)
                    toFix = 0;
                $('#reduction_percent_display').html('-' + parseFloat(discountPercentage).toFixed(toFix) + '%');
                $('#reduction_percent').show();
            }
        }
    }

    // Green Tax (Eco tax)
    // Update display of Green Tax
    if (default_eco_tax)
    {
        ecotax = default_eco_tax;

        // If the default product ecotax is overridden by the combination
        if (combination.ecotax)
            ecotax = +combination.ecotax;

        if (!noTaxForThisProduct)
            ecotax = ecotax * (1 + ecotaxTax_rate/100)

        $('#ecotax_price_display').text(formatCurrency(ecotax * currencyRate, currencyFormat, currencySign, currencyBlank));
        $('.price-ecotax').show();
    }

    // Unit price are the price per piece, per Kg, per m²
    // It doesn't modify the price, it's only for display
    if (productUnitPriceRatio > 0)
    {
        $('#unit_price_display').text(formatCurrency(unit_price * currencyRate, currencyFormat, currencySign, currencyBlank));
        $('.unit-price').show();
    }

    if (noTaxForThisProduct || customerGroupWithoutTax)
        updateDiscountTable(priceWithDiscountsWithoutTax);
    else
        updateDiscountTable(priceWithDiscountsWithTax);
}

Yeah!!!

 

 

 

 

  • Like 1

Share this post


Link to post
Share on other sites

  • 3 weeks later...
  • 4 weeks later...
  • 11 months later...

this solution work for me.

 

maybe your issue is due that you don't modify the right product.js

 

have you try to edit the product.js in your theme folder?

 

 

 

Matteo

Share this post


Link to post
Share on other sites

  • 3 months later...

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