Add custom fields to shopping cart display - Ubercart 3
I have Drupal 7 and UC 3 running with custom fields. I want to be able to display those fields in the shopping cart but can't seems to be able to do it. That's my first module creation attempt!
I try to use hook_form_alter(&$form, &$form_state, $form_id) {} in a custom module:
function swcart_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
case 'uc_cart_view_form':
// Adding 开发者_JAVA百科Grade column
$form['items']['#columns']['grade'] = array('cell' => 'Grade', 'weight' => 3.5);
for($i=0; $i < count($form['items']); $i++) {
if(isset($form['items'][$i]['nid'])) {
// Loading the node so we can retrieve the information we need.
$product = node_load($form['items'][$i]['nid']['#value']);
// Adding the 'Grade' to the product that is in the user's cart.
$form['items'][$i]['grade']['#value'] = $product -> field_product_term_data;
}
}
break;
}
}
The column 'Grade' shows up but no data in the table. What should I do? Do I have to modify the TAPir table? hook an other uc_cart function? Thanks
This two links of Ubercart 3 API should help: hook_tapir_table_alter uc_cart_view_table
In the first link there's an example for adding column with data.
EDIT: Finished up with adding price per 1 product column by using such code. Hope this will help someone to overcome ubercart's inflexibility.
function YOUR_MODULE_NAME_form_uc_cart_view_form_alter(&$form, &$form_state) {
$form['items']['#columns']['remove']['weight'] = 6;
$form['items']['#columns']['total']['weight'] = 5;
$form['items']['#columns']['qty']['weight'] = 4;
$form['items']['#columns']['price'] = array(
'cell' => t('Price'),
'weight' => 3,
);
}
function YOUR_MODULE_NAME_tapir_table_alter(&$table, $table_id) {
if ($table_id == 'uc_cart_view_table') {
foreach (element_children($table) as $key) {
if (!empty($table['#parameters'][1][$key]['nid'])) {
$node = node_load($table['#parameters'][1][$key]['nid']['#value']);
$table[$key]['price'] = array(
'#markup' => theme('uc_product_price',array('element' => array('#value' => $node->sell_price))),
);
}
}
}
}
精彩评论