Downloads
This code snippet is a views field template that moves the product price information into the buyitnowbutton form code, so the button displays looks like "Add to Cart - $29.95". The string [PRICE], if it appears in the Add to Cart button text (set on admin/store/settings/products/edit/general), is replaced by the price string.
In my implementation, I'm using some views (+ the insert_view module) to insert buy buttons into a free-format product page, but this code could be adapted to other presentation methods I am sure. It depends on sell-price being presented for display before buyitnowbutton, and disables display of sell-price.
To add this to one of your own views, put it in a file named view-view-fields--{view_name}--default.tpl.php, make sure the fields are in the right order, and rescan the theme template files using the button found in theme:information in the default section of the view (it can of course be renamed to affect only a single display).
I'm just getting started in Drupal, and this technique is already becoming very handy; for example, I am using it to modify a "recently added products" view so that the product images link not to the product nodes, but to special product page nodes.
<?php
// $Id: views-view-fields.tpl.php,v 1.6 2008/09/24 22:48:21 merlinofchaos Exp $
/**
* @file views-view-fields--med--default.tpl.php, hacked from
* views-view-fields.tpl.php, which you can get to by using the views theme:information
* link in a view, then click on row style output link to get the currently active code;
* You also get told the filename(s) you can use to put your new code in; you start by
* just copying the default code (if you're smart, anyway...)
*
* Name of file depends on what view you are messing with!
*
* Default simple view template to all the fields as a row.
*
* - $view: The view in use.
* - $fields: an array of $field objects. Each one contains:
* - $field->content: The output of the field.
* - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
* - $field->class: The safe class id to use.
* - $field->handler: The Views field handler object controlling this field. Do not use
* var_export to dump this object, as it can't handle the recursion.
* - $field->inline: Whether or not the field should be inline.
* - $field->inline_html: either div or span based on the above flag.
* - $field->separator: an optional separator that may appear before a field.
* - $row: The raw result object from the query, with all data it fetched.
*
* @ingroup views_templates
*
* 09/10/25 - RJW - Horribly hacked to transfer the ubercart price field value into
* the actual button, macro-replacing [PRICE]
*
*/
?>
<?php foreach ($fields as $id => $field): ?>
<?php
// depending on the class of the field being emitted, do various things.
switch ($field->class) {
case 'sell-price' :
// in this instance, we extract the url from $field->content and save it, but do not display
// the field. Note that if sell-price is not output before buyitnowbutton, this
// code will break horribly, cats and dogs will start living together, etc.
$temp_sell_price = preg_replace('/<.+>([^<]*)<.+>/',"$1",$field->content);
?>
<?php
break;
case 'buyitnowbutton' :
// hack in the url
$field->content = str_replace("[PRICE]",$temp_sell_price,$field->content);
// fall through to regular processing
default :
?>
<?php if (!empty($field->separator)): ?>
<?php print $field->separator; ?>
<?php endif; ?>
<<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
<?php if ($field->label): ?>
<label class="views-label-<?php print $field->class; ?>">
<?php print $field->label; ?>:
</label>
<?php endif; ?>
<?php
// $field->element_type is either SPAN or DIV depending upon whether or not
// the field is a 'block' element type or 'inline' element type.
?>
<<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
</<?php print $field->inline_html;?>>
<?php
}
?>
<?php endforeach; ?>
