Discussions

Add product price to a view

Having a hard time trying to figure out how to show the product price in a view. The Add To Cart form shows fine but no product price.

I tried using this method

I got around the problem by adding a 'Content: Rendered Content (The Content of the current relationship rendered using a view mode.)' as a field in addition to the previous field, but this time formatting it as a Rendered Product and using a Custom Display Setting in the View Mode to display only the price field but this does not seem like the correct solution to me.

From here http://www.drupalcommerce.org/discussions/5338/formatting-product-view-d...

BUT even though I can get the view to look the way I want in the view preview when I save it the view shows the full node and not what was shown in the preview.

Can I use a PHP field or Global text print this

<?php
 
print render($content['product:commerce_price']);
?>
in a field? If so how do I do it.
Posted: Oct 28, 2013

Comments

analog andy on October 28, 2013

Well this is what worked for me. This view just lists Product Display nodes on the front page so there are no Contextual filters.

I created a content view for Product Display Nodes
Format Grid and Fields
Relationship: Content: Referenced products

Fields
Content: Nid (Set to Exclude From display and move it up to top)
Content: Image
Field: Product Rating
Content: Title
(Products referenced by field_product) Commerce Product: Price (Set the reference for this field) Also under Style settings > Customize field and label wrapper HTML > Create a CSS class and type in node-[nid]-product-commerce-price
Content: Product

Filter criteria
Content: Type (= Product Display)
Content: Product:delta (= 0) (Set operator to is equal to 0

You can see working on this test page http://www.gardentech.ca/add-to-cart-test

Below is my view

$view = new view();
$view->name = 'feature_products_front_page';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Feature Products Front Page';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Featured Products';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['pager']['options']['items_per_page'] = '5';
$handler->display->display_options['style_plugin'] = 'grid';
$handler->display->display_options['row_plugin'] = 'fields';
/* Header: Global: Text area */
$handler->display->display_options['header']['area']['id'] = 'area';
$handler->display->display_options['header']['area']['table'] = 'views';
$handler->display->display_options['header']['area']['field'] = 'area';
$handler->display->display_options['header']['area']['content'] = 'FEATURED PRODUCTS';
$handler->display->display_options['header']['area']['format'] = 'full_html';
/* Relationship: Content: Referenced products */
$handler->display->display_options['relationships']['field_product_product_id']['id'] = 'field_product_product_id';
$handler->display->display_options['relationships']['field_product_product_id']['table'] = 'field_data_field_product';
$handler->display->display_options['relationships']['field_product_product_id']['field'] = 'field_product_product_id';
/* Field: Content: Nid */
$handler->display->display_options['fields']['nid']['id'] = 'nid';
$handler->display->display_options['fields']['nid']['table'] = 'node';
$handler->display->display_options['fields']['nid']['field'] = 'nid';
$handler->display->display_options['fields']['nid']['label'] = '';
$handler->display->display_options['fields']['nid']['exclude'] = TRUE;
$handler->display->display_options['fields']['nid']['element_label_colon'] = FALSE;
/* Field: Content: Image */
$handler->display->display_options['fields']['uc_product_image']['id'] = 'uc_product_image';
$handler->display->display_options['fields']['uc_product_image']['table'] = 'field_data_uc_product_image';
$handler->display->display_options['fields']['uc_product_image']['field'] = 'uc_product_image';
$handler->display->display_options['fields']['uc_product_image']['label'] = '';
$handler->display->display_options['fields']['uc_product_image']['alter']['path'] = '[view_node]';
$handler->display->display_options['fields']['uc_product_image']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['uc_product_image']['click_sort_column'] = 'fid';
$handler->display->display_options['fields']['uc_product_image']['settings'] = array(
  'image_style' => 'thumbnail',
  'image_link' => 'content',
);
$handler->display->display_options['fields']['uc_product_image']['delta_limit'] = '1';
$handler->display->display_options['fields']['uc_product_image']['delta_offset'] = '0';
/* Field: Field: Product Rating */
$handler->display->display_options['fields']['field_product_rating']['id'] = 'field_product_rating';
$handler->display->display_options['fields']['field_product_rating']['table'] = 'field_data_field_product_rating';
$handler->display->display_options['fields']['field_product_rating']['field'] = 'field_product_rating';
$handler->display->display_options['fields']['field_product_rating']['label'] = '';
$handler->display->display_options['fields']['field_product_rating']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_product_rating']['click_sort_column'] = 'rating';
$handler->display->display_options['fields']['field_product_rating']['settings'] = array(
  'widget' => array(
    'fivestar_widget' => 'sites/all/modules/fivestar/widgets/square/square.css',
  ),
  'expose' => 0,
  'style' => 'average',
  'text' => 'none',
);
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
$handler->display->display_options['fields']['title']['element_label_colon'] = FALSE;
/* Field: Commerce Product: Price */
$handler->display->display_options['fields']['commerce_price']['id'] = 'commerce_price';
$handler->display->display_options['fields']['commerce_price']['table'] = 'field_data_commerce_price';
$handler->display->display_options['fields']['commerce_price']['field'] = 'commerce_price';
$handler->display->display_options['fields']['commerce_price']['relationship'] = 'field_product_product_id';
$handler->display->display_options['fields']['commerce_price']['label'] = '';
$handler->display->display_options['fields']['commerce_price']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['commerce_price']['element_wrapper_class'] = 'node-[nid]-product-commerce-price';
$handler->display->display_options['fields']['commerce_price']['click_sort_column'] = 'amount';
$handler->display->display_options['fields']['commerce_price']['settings'] = array(
  'calculation' => '0',
);
/* Field: Content: Product */
$handler->display->display_options['fields']['field_product']['id'] = 'field_product';
$handler->display->display_options['fields']['field_product']['table'] = 'field_data_field_product';
$handler->display->display_options['fields']['field_product']['field'] = 'field_product';
$handler->display->display_options['fields']['field_product']['label'] = '';
$handler->display->display_options['fields']['field_product']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_product']['settings'] = array(
  'show_quantity' => 0,
  'default_quantity' => '1',
  'combine' => 1,
  'show_single_product_attributes' => 0,
  'line_item_type' => 0,
);
$handler->display->display_options['fields']['field_product']['delta_offset'] = '0';
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'product_display' => 'product_display',
);
$handler->display->display_options['filters']['type']['group'] = 1;
/* Filter criterion: Content: Product (field_product:delta) */
$handler->display->display_options['filters']['delta']['id'] = 'delta';
$handler->display->display_options['filters']['delta']['table'] = 'field_data_field_product';
$handler->display->display_options['filters']['delta']['field'] = 'delta';
$handler->display->display_options['filters']['delta']['value']['value'] = '0';

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block');

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->display->display_options['path'] = 'add-to-cart-test';