Magento

Buy me a beer or a book?

amazon PayPal - The safer, easier way to donate online!

Magento 1.5+ Core Override to make Custom Options drop down display full prices, not incremental relational prices

{jcomments on}Recently I have been working a lot with Magento at work and as such have had a lot of exposure to unanswered threads and blog posts which are too old to help in any way, or the solution isn't complete. So in my wisdom I thought I would give a stab at helping out some others in-kind.

Rather than relying heavily on the Magento Development Community for overbloated plugins to solve basic core changes I decided from the outset I would try and use core-overrides where possible (note, this is NOT hacking the core, it's overriding it). So, when I came across the issue of the custom options dropdowns displaying an incremental price when I wanted the full price displayed I immediately turned to Google. It seems there are a few others out there who are actually after the same change or a similiar one, however, what I discovered was there is no template file for this particular html output. Instead you need to amend "app/code/core/Mage/Catalog/Block/Product/View/Options/Type/Select.php" to make the full price display instead of an incremental one.

 

In order to override Magento core there is two directions you can take, the first (and the one which I will be using here) is simply overriding by creating an identical file and path as the file inside app/code/core/ that you wish to override, or if it's a more complex change then creating a plugin is definately a much cleaner and much more advised way of approaching the task. First of all you will need to create said file within the "local" directory as displayed in the below image:

 

Magento Core Override Custom Option Select

 

In this file you will need to add a line of code in near line 50, this line of code gets the base price for you to manipulate:

 

Magento Core Overrides - Custom Options - Select.php - Line 50 (new line)
1
$basePrice = $this->getProduct()->getPrice();

 

Then at the lines of code near 67 - 82 you will need to replace the foreach loop with the following code, this will make the price used to be the full price rather than an incremental value which is relational to the base price:

 

Magento Core Overrides - Custom Options - Select.php - Lines 67-82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
foreach ($_option->getValues() as $_value) {
 
	//change to display full price, not price difference.
	$itemPrice = ($basePrice + $this->helper('core')->currencyByStore(
$_value
->getPrice(true), $store, false));
  $priceStr = $this->_formatPrice(array( 'is_percent' => ($_value->getPriceType() == 'percent') ? true : false, 'pricing_value' => $itemPrice ), false);   $select->addOption( $_value->getOptionTypeId(), $_value->getTitle() . ' ' . $priceStr . '', array('price' => $itemPrice) ); }

 

Assuming that there is no mistakes in the location, or Magentos latest version has moved either of these files you should now be viewing a full price on your custom options dropdown, but wait, there is still a pesky "+" symbol making the prices look incremental instead. I soon found this to be easily resolveable by amending a close-by file, the parent Abstract: "app/code/core/Mage/Catalog/Block/Product/View/Options/Abstract.php". As before with select.php you will need to create an overriding version of this file within the "local" directory inside "app/code/" so you should then have something looking like this:

 

Magento Core Override Custom Option Select Abstract

 

In Abstract.php you simply override the method _formatPrice near lines 112-142 with the following so that "+" is no longer present:

 

 

Magento Core Overrides - Custom Options - Abstract.php - Lines 112-142
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
protected function _formatPrice($value, $flag=true)
{
	if ($value['pricing_value'] == 0) {
		return '';
	}
		$taxHelper = Mage::helper('tax');
	$store = $this->getProduct()->getStore();
 
	$priceStr = $sign;
	$_priceInclTax = $this->getPrice($value['pricing_value'], true);
	$_priceExclTax = $this->getPrice($value['pricing_value']);
	if ($taxHelper->displayPriceIncludingTax()) {
		$priceStr .= $this->helper('core')->currencyByStore($_priceInclTax,
$store, true, $flag);
} elseif ($taxHelper->displayPriceExcludingTax()) { $priceStr .= $this->helper('core')->currencyByStore($_priceExclTax,
$store, true, $flag);
} elseif ($taxHelper->displayBothPrices()) { $priceStr .= $this->helper('core')->currencyByStore($_priceExclTax,
$store, true, $flag);
if ($_priceInclTax != $_priceExclTax) { $priceStr .= ' ('.$sign.$this->helper('core') ->currencyByStore($_priceInclTax, $store, true,
$flag).' '.$this->__('Incl. Tax').')';
} } if ($flag) { $priceStr = '<span class="price-notice">'.$priceStr.'</span>'; } return $priceStr; }

 

There we have it, a succesful core override to make your custom option dropdown show a final price rather than an incremental difference based on the core price of the product. As always, don't try this on a live site, always make sure you test on development version first! Any comments are always welcome, as I am new to this and may have overlooked something horrid!

 

Free Downloads