VirtueMart Dev/Coding Central: VM1 (old version) > Development Projects, Modifications, Hacks & Tweaks. VM1.1
Price Updater
Macchiato:
The Price Updater will automatically add the product price and all attribute prices together and display the total amount. The Price Updater was tested in Joomla 1.5.23 with VirtueMart 1.1.9, I have used the Ja Larix template, but the Price Updater should work in any other version and template, cause it's an standalone script and it's not version dependent.
Follow the steps to implement the Price Updater:
--- STEP 1 ---
Make sure you have the decimal symbol set to period/dot (.) and the thousands separator set to comma (,)
You can change these by going to the backend of Joomla, then select Components --> VirtueMart --> Store --> Edit Store. Below "Currency Display Style" change the decimal symbol to period/dot (.) and the thousands separator to comma (,)
--- STEP 2 ---
Open: /components/com_virtuemart/themes/default/templates/common/price.tpl.php
and replace:
--- Code: ---<?php
if( !empty( $price_info["product_price_id"] )) { ?>
<span class="productPrice">
<?php echo $CURRENCY_DISPLAY->getFullValue($base_price) ?>
<?php echo $text_including_tax ?>
</span>
<?php
}
echo $price_table;
?>
--- End code ---
with:
--- Code: ---<?php
if( !empty( $price_info["product_price_id"] )) { ?>
<span class="productPrice">
<p id="priceUpdater-Price"><?php echo $CURRENCY_DISPLAY->getFullValue($base_price) ?><?php echo $text_including_tax ?></p>
</span>
<?php } echo $price_table; ?>
--- End code ---
--- STEP 3 ---
Open: /components/com_virtuemart/themes/"your template name"/templates/product_details/flypage.tpl.php
and replace:
--- Code: --- <tr>
<td colspan="3"><?php echo $product_type ?></td>
</tr>
--- End code ---
with:
--- Code: --- <tr>
<td colspan="3"><?php echo $product_type ?></td>
</tr>
<tr>
<td></td>
<td align="right" style="border:solid 1px #cccccc;border-right:none;">
<p>Product Price:</p>
<p>Attributes Price:</p>
<p style="font-weight:bold;font-size:110%;background-color:#efefef;">Total:</p>
</td>
<td align="left" style="border:solid 1px #cccccc;border-left:none;">
<p id="priceUpdater-duplicatePrice">$0.00</p>
<p id="priceUpdater-attributesPrice">$0.00</p>
<p id="priceUpdater-totalPrice" style="font-weight:bold;font-size:110%;background-color:#efefef;">$0.00</p>
</td>
</tr>
--- End code ---
If you do not use Ja Larix, insert the following code anywhere you like in flypage.tpl.php:
--- Code: --- <tr>
<td></td>
<td align="right" style="border:solid 1px #cccccc;border-right:none;">
<p>Product Price:</p>
<p>Attributes Price:</p>
<p style="font-weight:bold;font-size:110%;background-color:#efefef;">Total:</p>
</td>
<td align="left" style="border:solid 1px #cccccc;border-left:none;">
<p id="priceUpdater-duplicatePrice">$0.00</p>
<p id="priceUpdater-attributesPrice">$0.00</p>
<p id="priceUpdater-totalPrice" style="font-weight:bold;font-size:110%;background-color:#efefef;">$0.00</p>
</td>
</tr>
--- End code ---
--- STEP 4 ---
Open: /components/com_virtuemart/themes/"your template name"/templates/product_details/includes/addtocart_form.tpl.php
to make this step more easier, replace the whole page with this code:
--- Code: ---<?php if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); ?>
<div class="vmCartContainer">
<?php
mm_showMyFileName(__FILE__);
// This function lists all product children ( = Items)
// or, when not children are defined, the product_id
// SO LEAVE THIS IN HERE!
list($html,$children) = $ps_product_attribute->list_attribute( ( $product_parent_id > 0 ) ? $product_parent_id : $product_id );
if ($children != "multi") {
if( CHECK_STOCK == '1' && ( $product_in_stock < 1 ) ) {
$notify = true;
} else {
$notify = false;
}
?>
<!-- Add to Cart Form -->
<form action="<?php echo $mm_action_url ?>index.php" method="post" name="addtocart" id="<?php echo uniqid('addtocart_') ?>" class="addtocart_form" <?php if( $this->get_cfg( 'useAjaxCartActions', 1 ) && !$notify ) { echo 'onsubmit="handleAddToCart( this.id );return false;"'; } ?>>
<!-- Add to Cart and Attribute List Items -->
<fieldset id="priceUpdater">
<?php
if ($children == "drop") {
echo $ps_product_attribute->show_quantity_box($product_id,$product_id);
}
if ($children == "radio") {
echo $ps_product_attribute->show_radio_quantity_box();
}
$button_lbl = $VM_LANG->_('PHPSHOP_CART_ADD_TO');
$button_cls = 'addtocart_button';
if( CHECK_STOCK == '1' && ( $product_in_stock < 1) ) {
$button_lbl = $VM_LANG->_('VM_CART_NOTIFY');
$button_cls = 'notify_button';
}
?>
<input type="submit" class="<?php echo $button_cls ?>" value="<?php echo $button_lbl ?>" title="<?php echo $button_lbl ?>" />
<!-- Attribute List Items -->
<?php
}
echo $html;
if (USE_AS_CATALOGUE != '1' && $product_price != "" && !stristr( $product_price, $VM_LANG->_('PHPSHOP_PRODUCT_CALL') )) {
?>
<?php if ($children != "multi") { ?>
<!-- End Attribute List Items -->
</fieldset><!-- End Add to Cart and Attribute List Items -->
<?php } ?>
<input type="hidden" name="flypage" value="shop.<?php echo $flypage ?>" />
<input type="hidden" name="page" value="shop.cart" />
<input type="hidden" name="manufacturer_id" value="<?php echo $manufacturer_id ?>" />
<input type="hidden" name="category_id" value="<?php echo $category_id ?>" />
<input type="hidden" name="func" value="cartAdd" />
<input type="hidden" name="option" value="<?php echo $option ?>" />
<input type="hidden" name="Itemid" value="<?php echo $Itemid ?>" />
<input type="hidden" name="set_price[]" value="" />
<input type="hidden" name="adjust_price[]" value="" />
<input type="hidden" name="master_product[]" value="" />
<?php } if ($children != "multi") { ?>
</form><!-- End Add to Cart Form -->
<?php
}
if($children == "radio") { ?>
<script language="JavaScript" type="text/javascript">//<![CDATA[
function alterQuantity(myForm) {
for (i=0;i<myForm.selItem.length;i++){
setQuantity = myForm.elements['quantity'];
selected = myForm.elements['selItem'];
j = selected[i].id.substr(7);
k= document.getElementById('quantity' + j);
if (selected[i].checked==true){
k.value = myForm.quantity_adjust.value; }
else {
k.value = 0;
}
}
}
//]]>
</script>
<?php } ?>
</div>
--- End code ---
For those of you who use a custom code: you only have to add <fieldset id="priceUpdater"> to the page (don't forget to close </fieldset>) Please look at the code above to see where you can place it.
--- STEP 5 ---
For the final step, you copy the following Javascript code and place it in a external Javascript document, let's call the document "price-updater.js".
Place "price-updater.js" in your template's script directory (/templates/"your template name"/scripts)
--- Code: ---/*------------------------------------------------------------------------
## Price Updater v1 by Macchiato
-------------------------------------------------------------------------*/
function getAmountAndPutInElement(id) {
var checkForThisID = document.getElementById(id);
var getAmountSelects = checkForThisID.getElementsByTagName("select"),
L = getAmountSelects.length,
i;
for (i = 0; i < L; i++) {
getAmountSelects[i].onchange = calcTotal;
}
function calcTotal() {
var totalEl = document.getElementById("priceUpdater-attributesPrice"),
totalVal = 0,
getAmountSelects = checkForThisID.getElementsByTagName("select");
for (var x=0, y=getAmountSelects.length; x<y; x++) {
var index = getAmountSelects[x].selectedIndex;
if (index) {
var value = getAmountSelects[x].options[index].text;
value = value.substring(value.search(/[(]\+[$]/)+3, value.length-1);
value = value.replace(/,/g, '');
totalVal += Number(value);
}
}
totalEl.innerHTML = addCommasandsign(totalVal.toFixed(2));
addAmountsAndPutInElement("priceUpdater-Price");
}
}
getAmountAndPutInElement("priceUpdater");
function getTextAndPutInElement(id) {
var source = document.getElementById(id);
var target = document.getElementById("priceUpdater-duplicatePrice");
target.innerHTML = source.innerHTML;
}
getTextAndPutInElement("priceUpdater-Price");
function addCommasandsign(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return '$' + x1 + x2;
}
function addAmountsAndPutInElement(id) {
var firstval=document.getElementById(id).innerHTML;
var secval=document.getElementById('priceUpdater-attributesPrice').innerHTML;
firstval=firstval.replace('$','');
firstval=firstval.replace(',','');
secval=secval.replace('$','');
secval=secval.replace(',','');
var totalOne=parseFloat(firstval) + parseFloat(secval);
var totalTwo=addCommasandsign(totalOne.toFixed(2));
document.getElementById('priceUpdater-totalPrice').innerHTML=totalTwo;
}
addAmountsAndPutInElement("priceUpdater-Price");
--- End code ---
Now place the following line at the bottom of /components/com_virtuemart/themes/"your template name"/templates/product_details/flypage.tpl.php :
<script type="text/javascript" src="templates/"your template name"/scripts/price-updater.js"></script>
That's it :)
P.S. Please post if it works for you, and what Joomla/VirtueMart version and template you are using.
tutnet:
Hi,
I have this working in J1.5.23 with VM 1.1.6 - great mod!
There is however one glitch - it only works with $ as the currency.
I have tried swapping in € and € into price-updater.js without any luck.
Any ideas how to make this work with Euros?
Thans
strdesign:
Hi Macchiato; do you have a demo for your Price Updater add-on?
thanks,
Lewis
Macchiato:
--- Quote from: tutnet on September 28, 2011, 00:27:10 AM ---Hi,
I have this working in J1.5.23 with VM 1.1.6 - great mod!
There is however one glitch - it only works with $ as the currency.
I have tried swapping in € and € into price-updater.js without any luck.
Any ideas how to make this work with Euros?
Thans
--- End quote ---
Hi Tutnet,
Have you also changed the $ sign to € in flypage.tpl.php?
I haven't tried this with the euro sign yet, but I'm pretty sure you only have to change all the $ signs in the above php and javascript files.
You also need to change the $ sign to € in the VirtueMart configuration and you need to use a dot (0.00) instead of a comma (0,00) for the price format.
Hope this helps and thanks for trying my mod :)
Macchiato:
--- Quote from: strdesign on September 30, 2011, 07:57:06 AM ---Hi Macchiato; do you have a demo for your Price Updater add-on?
thanks,
Lewis
--- End quote ---
No not yet, will try to make one in the (near) future... if I have more free time ;)
Navigation
[0] Message Index
[#] Next page
Go to full version