Hack: adding the price to the buyitnowbutton

Contrib type: 
Code/CSS Snippet
Status: 
Initial upload
Moderation: 
Awaiting moderation
Latest version: 
0.9b

Downloads


Compatibility: 
Ubercart 2.x

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; ?>

blasthaus's picture
Offline
Joined: 01/10/2010
Juice: 17
hack works

just a note of thanks, i found this helpful but i can't believe this is the only way we can do this, someone please tell me I'm wrong. i feel guilty using this technique, but it totally gets the job done and can save someone like me a lot of potentially wasted hours. for your special product page a links, are you just using the same kind of str_replace type of approach?
thx-wil

MadOverlord's picture
Offline
Joined: 10/10/2009
Juice: 129
Re: hack works

Yep. I use this kind of technique in several places on www.animeigo.com

For example, on the homepage, the url that points to the full-sized image for the product package is munged so that it points to the product page instead, and also fills in title and alt fields. It's just an extended version of the original hack...

<?php
// $Id: views-view-fields.tpl.php,v 1.6 2008/09/24 22:48:21 merlinofchaos Exp $
/**
* @file views-view-fields--recent-products--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/21 - RJW - Horribly hacked to transfer a URL from one field to another,
* and to transfer sell price to buy-it-now button
*
*/
?>

<?php foreach ($fields as $id => $field): ?>

<?php

// depending on the class of the field being emitted, do various things.

switch ($field->class) {
 
case
'title' :

    $temp_title = $field->content;
    break;
   
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);
 
  break;

case 'field-page-nid' :

  // in this instance, we extract the url from $field->content and save it, but do not display
  // the field.  Note that if field-page-nid is not output before field-image-cache-fid, this
  // code will break horribly, cats and dogs will start living together, etc.
 
 
$field_page_nid_url = preg_replace('/([^"]+)"([^"]+)".*/',"$2",$field->content);
 
  break;

case 'field-image-cache-fid' :

  // hack in the url
 
 
$field->content = preg_replace('/([^"]+")([^"]+)(".*)/',"$1$field_page_nid_url$3",$field->content);
 
$field->content = str_replace('alt=""','alt="' . $temp_title . '"',$field->content);
 
$field->content = str_replace('title=""','title="' . $temp_title . '"',$field->content);
 
 
// fall through, be careful
 
case 'buyitnowbutton' :

  // hack in the url, have to do extra check because we might get a fallthrough
 
 
if ($field->class == 'buyitnowbutton') {
   
$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; ?>

When you get to be my age, you don't write code anymore... you just remember it and type it in again! (grin)