Hi,
I want to display add to cart button on page.tpl.php file.
I am trying like this

echo drupal_render($content['field_product']);;
but, it was showing nothing.
Is there any settings or configuration?
Thanks.

Comments

rszrama’s picture

Status: Active » Fixed

I'm not sure you're supposed to be printing variables out like that in your custom templates. There's got to be some better way to render that, maybe even just drupal_render($content['field_name']);. Once you figure out the best way to do that (I'd check #drupal-support in IRC), then it should just be a matter of rendering your node's product reference field.

gamepaused’s picture

I think the way these variables are being printed come from the contemplate module.
Some are spat out like that, while others have a 'safe value' option:

print $node->field_headline['und'][0]['safe_value']

Could this be just:

drupal_render($content['field_headline']); then?

maxchock’s picture

Oh i see. Sorry, I'm just a very beginner of doing custom tpl.php. I actually learn those from Contemplate. So which variable to drupal_render() for "Add to cart" button?

Thanks.

rszrama’s picture

It just depends on what the name of your product reference field is.

maxchock’s picture

sorry, I'm really a newbie here. I run a dpm($content) and these are the field printed:

... (Array, 13 elements)
links (Array, 6 elements)
product:commerce_price (Array, 16 elements)
product:field_normal_price (Array, 16 elements)
product:field_photo (Array, 18 elements)
product:field_highlights (Array, 2 elements)
product:field_conditions (Array, 2 elements)
product:field_seller (Array, 2 elements)
product:field_address (Array, 2 elements)
product:field_date (Array, 16 elements)
product:field_min_purchase (Array, 16 elements)
product:field_max_purchase (Array, 16 elements)
product:field_lock (Array, 2 elements)
product:field_testing (Array, 2 elements)

Where can I find the "product reference field"?

Thanks.

rszrama’s picture

Those look like fields attached to a product, not the node. How exactly are you making your product display?

maxchock’s picture

I make the product display as follow the instruction here http://www.drupalcommerce.org/node/293.

I have a content type "Product display" and inside "Product display" there is a field "Product reference" to point to the product SKU.

rszrama’s picture

Ok, you can find the name of your product reference field on your node type's manage fields tab. It's whatever you put in as the name (it'll have a field_ prefix).

maxchock’s picture

I found out the product reference field is "field_product_ref", so I just use drupal_render($content['field_product_ref']) to display the button?

this is the dpm($node) output:

field_product_ref (Array, 1 element)
      und (Array, 1 element)
             0 (Array, 1 element)
                    product_id (String, 1 characters ) 2

I tried drupal_render($node['field_product_ref']['und'][0]['product_id']); but it give me
"Fatal error: Cannot use object of type stdClass as array in /home/buddyjoy/public_html/sites/all/themes/escapes2/templates/node--product_display.tpl.php on line 17" error.

I enable the contemplate again to get the value which is

$node->field_product_ref['und'][0]['product_id']

I use

drupal_render($node->field_product_ref['und'][0]['product_id'])

I also set the content type "Product display" > Manage display > Teaser to display the "Product reference" field using "Add to cart form" but it still doesn't display anything..

Am I using the wrong function to display the button?

Thanks.

rszrama’s picture

I honestly don't know... you're asking a hardcore module dev questions better suited to hardcore theme devs. : P

fwiw, the Add to Cart button you're trying to print is just another field, so this can be printed out the same way you print out any other field. With that in mind, you should really try asking for help in #drupal-support on IRC or somewhere where themers are available to answer questions like these. Just make sure in your node type's "Manage display" tab that it's set to use the Add to Cart form display formatter.

gamepaused’s picture

Contemplate is great for finding the values to spit out.
After looking at this thread yesterday, I looked into it and swapped out all contemplate chunks with the proper method.

drupal_render only seems to work on parts of content that are standard to a content type (body, title etc).

You can use this to swap contemplate values -

$node->field_product_ref['und'][0]['product_id'] would become:

print render($content['field_product_ref'][0]);?> assuming 0 is the ID

maxchock’s picture

tried, no luck on displaying the "Add to cart" button by this field.

adamgerthel’s picture

Have you tried <?php print render($content['field_product_ref']); ?>

Btw, you don't need contemplate to find the field names. They are visible on "manage fields". Don't forget that when printing fields this way, they will still use the format settings from "manage display".

Also, does your product have a price? Are you 100% sure that there's no css that's hiding the form?

pardalman’s picture

subs

adamgerthel’s picture

You're right. Something is wrong. We recently changed to using fieldgroups, and just printing $content in our node--product.tpl.php so we haven't spotted this before. But something is amiss. Looking at our repository history for this particular project, I extracted what our .tpl.php looked prior to just printing $content. I tried applying this version again, and lo-and-behold it doesn't work anymore. Could something have happened from beta 3 to beta 4?

This is our old version of product--node.tpl.php that doesn't work anymore. It used to work. "field_product_article" is the product reference field.

<?php
<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

  <?php print $user_picture; ?>
  <?php print render($title_prefix); ?>
  <?php print render($title_suffix); ?>

  <?php if ($display_submitted): ?>
    <div class="submitted">
      <?php print $submitted; ?>
    </div>
  <?php endif; ?>

  <div class="content"<?php print $content_attributes; ?>>
    <div class="product-top">
      <div id="product-image">
        <?php print render($content['product:field_prod_ent_image']); ?>
        <?php print render($content['field_product_thumbs']); ?>
       </div>
    </div>
    <div id="product-info">
      <?php print render($content['field_product_family']); ?>
      <?php print render($content['product:field_prod_ent_color']); ?>
      <?php print render($content['product:field_prod_ent_material']); ?>
      <div class="dimensions"> 
        <?php print render($content['product:field_prod_ent_width']); ?>
        <?php print render($content['product:field_prod_ent_height']); ?>
        <?php print render($content['product:field_prod_ent_depth']); ?>
        <?php print render($content['product:field_prod_ent_diameter']); ?>
        <?php print render($content['product:field_prod_ent_height_seat']); ?>
        <?php print render($content['product:field_prod_ent_depth_seat']); ?>
        <?php print render($content['product:field_prod_ent_shape']); ?>
      </div>
      <?php print render($content['product:field_prod_ent_descr']); ?>
      <?php print render($content['product:commerce_price']); ?>
      <?php print render($content['product:commerce_stock']); ?>
      <?php print render($content['field_product_article']); ?>
    </div>
    <div id="product-extras">
      <div id="product-facts">
        <h2>Produktinfo</h2>
        <?php print render($content['field_product_supplier']); ?>
        <?php print render($content['field_product_designer']); ?>
        <?php print render($content['body']); ?>
      </div>
      <div id="product-images">
        <h2>Galleri</h2>
        <?php print render($content['field_product_image']); ?>
        <?php print render($content['field_product_images']); ?>
      </div>
    </div>
    <div id="product-related">
      <?php print render($content['field_product_related']); ?>
    </div>
   </div>
  </div>
    <?php
      // We hide the comments and links now so that we can render them later.
      hide($content['comments']);
      hide($content['links']);
      print render($content);
    ?>
  </div>

  <?php print render($content['links']); ?>
  <?php print render($content['comments']); ?>

</div>
maxchock’s picture

Thanks AdamGerthel!! That's the fields to show up the Add to cart button!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Sampath PerOxide’s picture

Title: Showing "Add to cart" button on a custom tpl.php » Showing "Add to cart" button on page.tpl.php
Issue summary: View changes