The product myBundle has myColorOption with these items:
- Green $50
- Blue $100
- Red $100
- Black $150
Magento 1.4.2.0 per default will present the customer with a select dropdown with options like this:
- Green +$50
- Blue +$100
- Red +$100
- Black +$150
The change I am looking for is when a default item has been selected by admin. When it is, each displayed price should be relative to that of the default option. If admin sets Blue (price $100) as the default item for the option, the dropdown should now read:
- Green -$50
- Blue
- Red
- Black +$50
To clarify: I only want the displayed price in the dropdown to change, the actual price added to cart and used for other calculations remains the same.
Update: Here's the code I have so far, the problem is in the commented lines. I need help getting the correct models etc.
<?php
// From file app/code/core/Mage/Bundle/Block/Catalog/Product/View/Type/Bundle/Option.php
// copied to app/code/local/Mage/...
public function getSelectionTitlePrice($_selection, $includeContainer = true)
{
$defaultPrice = 0.00;
$_product = $this->getProduct();
/*
$_mbmo = new Mage_Bundle_Model_Option();
$_mbmo->load($_selection->getProductId());
$_default = $_mbmo->getDefaultSelection();
$defaultPrice = $_product->getPriceModel()->getSelectionPreFinalPrice($_product, $_default, 1);
*/
$price = $_product->getPriceModel()->getSelectionPreFinalPrice($_product, $_selection, 1);
if ($price == $defaultPrice)
{
return $_selection->getName();
}
else
{
$sign = ($price < $defaultPrice) ? '-' : '+';
$diff = ($price < $defaultPrice) ? $defaultPrice - $price : $price - $defaultPrice;
return $_selection->getName() . ' ' .
($includeContainer ? '<span class="price-notice">':'') . $sign .
$this->formatPriceString($diff, $includeContainer) . ($includeContainer ? '</spa开发者_StackOverflow社区n>':'');
}
}
Use this code
$defaultPrice = $_product->getPriceModel()->getSelectionPreFinalPrice($_product, $_default,1);
Replace above line with below
$defaultPrice=$this->getOption()->getDefaultSelection()->getSelectionPriceValue();
Just to add something to this answer - I was finding that the code would fail if it tried to call getDefaultSelection()->getPrice() against an option that had no default value. Was able to fix this by adding the following code:
$_mbmo = new Mage_Bundle_Model_Option();
$_mbmo->load($_selection->getProductId());
$_default = $_mbmo->getDefaultSelection();
if (gettype($this->getOption()->getDefaultSelection())==object){
$defaultPrice=$this->getOption()->getDefaultSelection()->getPrice();
}
Basically just does a check that something is returned from calling getDefaultSelection() on $this and then proceeds with setting default price, otherwise it just carries on with the rest of the code.
精彩评论