3 replies [Last post]
Danny_Joris's picture
Offline
Joined: 05/09/2009
Juice: 199
Was this information Helpful?

Hey All,

I'm trying to add a checkbox in the products page. Later at checkout I want to calculate shipping costs for when all products in the cart have that checkbox filled out.

So I'm trying to kinda copy the 'product is shippable' checkbox and it's functions here. I managed to get the checkbox into the products page, but it won't save the selection I make. I'm adjusting a test installation of UC.

I've added this in uc_product.module :

<?php
  $form
['base']['dimensions']['sizecheck'] = array(
   
'#type' => 'checkbox',
   
'#title' => t('Product is smaller than 35x23x3 cm.'),
   
'#default_value' => isset($node->sizecheck) ? $node->sizecheck : FALSE,
   
'#weight' => 9,
  ); 
?>

In uc_product.install :

<?php

//after $schema['uc_product_features'] = array(
     
'sizecheck' => array(
       
'description' => t('Boolean flag signifying that the product is small.'),
       
'type' => 'int',
       
'size' => 'tiny',
       
'unsigned' => TRUE,
       
'not null' => TRUE,
       
'default' => 0,
      ),

// wherever I saw a function with 'shippable' in it, I copied it to make it 'sizecheck' :

// Update to add the sizecheck column to the product table.

function uc_product_update_13() {
 
$ret = array();
  switch (
$GLOBALS['db_type']) {
    case
'mysql':
    case
'mysqli':
     
$ret[] = update_sql("ALTER TABLE {uc_products} ADD sizecheck tinyint(2) NOT NULL default 0");
    break;
    case
'pgsql':
     
db_add_column($ret, 'uc_products', 'sizecheck', 'tinyint', array('not null' => TRUE, 'default' => 0));
    break;
  }
  return
$ret;
}

//added this to function uc_product_update_6000() {
db_change_field($ret, 'uc_products', 'sizecheck', 'sizecheck', array('type' => 'int', 'unsigned' => TRUE, 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
?>

In uc_order.ca.inc I added this:

<?php
//in the function: function uc_order_ca_condition() {
 
$conditions['uc_order_condition_sizecheck'] = array(
   
'#title' => t('Check if all the products are categorised as small'),
   
'#category' => t('Order: Product'),
   
'#description' => t('Returns TRUE if all the products in the order are categorised as small.'),
   
'#callback' => 'uc_order_condition_sizecheck',
   
'#arguments' => array(
     
'order' => $order_arg,
    ),
  );

//and separate:
function uc_order_condition_sizecheck($order, $settings) {
  return
uc_order_sizecheck($order);
}

?>

That uc_order_sizecheck() function is located in uc_order.module:

<?php
//i tweaked it to this. In the 'shippable' function there was some other stuff going on that i didn't understand.
function uc_order_sizecheck($order) {
  if (!
is_array($order->products) || empty($order->products)) {
    return
FALSE;
  }

  foreach (

$order->products as $product) {
   
// Return FALSE if the product form specifies this as not checked.
   
if ($product->data['sizecheck'] == FALSE) {
     
$results[] = FALSE;
      continue;
    }
    else{
       
$results[] = TRUE;
        continue;
    }

  }

  return !

in_array(FALSE, $results);
}
?>

I also manually added a spot in the database for my 'sizecheck' data, right next to 'shippable'. It doesn't seem to connect to the DB yet, though.

And I don't understand how I'm suppost to know what variables i can call with that $product object. If this wasn't a function, I think i could figure out, but a function won't let me echo anything.

Any support is very appreciated !
Many thanks in advance.
Cheers,
Danny

Ps: I started to build this because I'm stuck with my other attempt as well: http://www.ubercart.org/forum/support/12504/flatrate_based_dimensions . All help is also very welcome there.

TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Danny_Joris wrote:
Danny_Joris wrote:

I've added this in uc_product.module :

Doing it that way is a Really Bad Idea.

You should be using hook_form_alter() to modify the product edit page. hook_nodeapi() is where you can add code to save any variables you create. Or for something simple like a checkbox you could just use CCK and set it as hidden so it doesn't display on the product page.

<tr>.
Danny_Joris's picture
Offline
Joined: 05/09/2009
Juice: 199
_

I'll do some research on hooks. Besides the way it is embedded in the code, can you see any mayor flaws in the code itself?

I've added a cck checkbox as well, but then again I thought it would not be a good idea to let a manually added checkbox communicate with a module or patch.

Thanks again,
Danny

Danny_Joris's picture
Offline
Joined: 05/09/2009
Juice: 199
_