问题
I want to implement such a functionality if we have a custom option with price value 10 and its price type is percent and its base price is 0 how can i implement it.
I want Firstly from my first client chooses the Size and price is added according to size when client choose the color from color drop down then the percentage value should be calculated from the first dropdown value select
for example
if client chooses
an option A with price $20 the 20$ is added to its price and i have to calculate the price by calculatiog the seond drop down value and with the price selected from first dropdowm.
回答1:
First, let me try to rephrase your question:
In each custom option step, how do you calculate the percentage based on prices from the previous steps, rather than based on the product base price? In other words, how do you make Magento accumulate custom option prices rather than summing them?
Example product:
- Base price: $0
- Size A: +$20
- Color X: +10%
Magento's calculation: 10 % of base price 0 = 0. Total price 20.
Desired calculation: 10 % of base price AND size price = 10 % of 20. Total price 22.
Solution
To do this you have to override Magento's custom option pricing logic. Changes have to be made in the price model (for server side calculations) as well as in the block and template code (for client side Javascript calculations).
In the price model Mage_Catalog_Model_Product_Type_Price
, change the calculation in the function _applyOptionsPrice
:
//$finalPrice += $group->getOptionPrice($quoteItemOption->getValue(), $basePrice);
$finalPrice += $group->getOptionPrice($quoteItemOption->getValue(), $finalPrice);
In Mage_Catalog_Block_Product_View_Options
, the function getJsonConfig
calculates the percentage of the base price before returning it to the template. Change this to get the actual percentage value returned to the template:
//$_tmpPriceValues[$value->getId()] = Mage::helper('core')->currency($value->getPrice(true), false, false);
// Add fixed price or percentage (don't calculate percentage yet, done by JS live instead)
$_tmpPriceValues[$value->getId()]['pricing_value'] = ($value->getPriceType() != 'percent') ? Mage::helper('core')->currency($value->getPrice(true), false, false) : $value->getPrice();
$_tmpPriceValues[$value->getId()]['is_percentage'] = ($value->getPriceType() == 'percent');
Now modify the Javascript in template/catalog/product/view/options.phtml
to calculate the percentage on-the-fly, accumulatively. In the Javascript class Product.Options
, add a function for the calculation:
getPriceToAdd : function(optionvalue, oldprice) {
var value = parseFloat(optionvalue['pricing_value']);
if (optionvalue['is_percentage']) {
return Math.round(value*oldprice)/100;
} else {
return value;
}
}
and change the reloadPrice
function to use the new calculation:
//price += parseFloat(config[optionId][element.getValue()]);
price += opConfig.getPriceToAdd(config[optionId][element.getValue()], price);
...
//price += parseFloat(this.config[optionId]);
price += opConfig.getPriceToAdd(this.config[optionId], price);
...
//price += parseFloat(this.config[optionId][selectOption.value]);
price += opConfig.getPriceToAdd(this.config[optionId][selectOption.value], price);
...
//price += parseFloat(this.config[optionId]);
price += opConfig.getPriceToAdd(this.config[optionId], price);
As usual, don't edit core files directly. Use local rewrites. Please note, your new price model must be declared in the configuration for each relevant product type:
<config>
<global>
<catalog>
<product>
<type>
<simple>
<price_model>mymodule/product_type_simple_price</price_model>
</simple>
...
</type>
</product>
</catalog>
</global>
</config>
回答2:
I'm not clear about your question and i think u need configurable product option...
See this tutorial http://www.magentocommerce.com/knowledge-base/entry/tutorial-creating-a-configurable-product/
and the sample output would be like below
回答3:
With Magento update, the line to modified in _applyOptionsPrice has been updated :
from : $finalPrice += $group->getOptionPrice($quoteItemOption->getValue(), $finalPrice);
to : $finalPrice += $group->getOptionPrice($confItemOption->getValue(), $finalPrice);
来源:https://stackoverflow.com/questions/5990956/i-want-to-modify-custom-option-percent-functionality