6 replies [Last post]
jordana@drupal.org's picture
Offline
Joined: 02/13/2009
Juice: 18
Was this information Helpful?

Hi all,
I have a cck computed field which has computed the number of days form a to and from date field.
Now I want the quantity field of a product to get the value of this computed field.
This is for a rental company where the quantity field should actually have the number of days rented.
(I created a reservation content type where I added the cck date fields and the computed field and linked that thru node chechout module to a "rental" product type)
I tried adding this with the custom price module.
Here is my code, where fiedl_rentdays is the cck computed field:

$node = node_load($item->nid);
$rentaldays = $node->field_rentdays;

if ($rentaldays > 1){
$item->qty = $rentaldays;
}

Is the field I'm referencing wrong?
Should I just write a module to do this? Can someone please help/advise me on how to proceed (with details).
Please help.
Thanks in advance.

jordana@drupal.org's picture
Offline
Joined: 02/13/2009
Juice: 18
solved it

I figured this out with some help in irc (thanks longwave) and peeping in the uc_node_checkout code on how to reference the corresponding node.
Hope this helps someone.

/**
* Implementation of hook_cart_item().
*/

function uc_date_qty_cart_item($op, &$item) {
  if ($op == 'load') {
        // I used Node Checkout and computed field is stored in corresponding node, so
// Load up the corresponding node so we can get the computed field data from it
   if (isset($item->data['node_checkout_nid']) && $node = node_load($item->data['node_checkout_nid'])) {
        $item->checkout_node = $node;
      }
// field name of computed field is field_rentdays - Get value
$dayqty = abs($node->field_rentdays[0]['value']);

if ($dayqty > 1){
$item->qty = $dayqty;
}
$item->qty = $dayqty;
}
}

eporama's picture
Offline
Joined: 01/26/2010
Juice: 2
well, it helped me

Jordana, just wanted to give some thanks for putting this up. Helped me out greatly with a similar task.

Near the end of your example, you have:

if ($dayqty > 1){
$item->qty = $dayqty;
}
$item->qty = $dayqty;

which seems to be redundant. I'm sure there were lots of other things going on, but just wanted to make sure I wasn't missing something important.

_E

jordana@drupal.org's picture
Offline
Joined: 02/13/2009
Juice: 18
Re: well, it helped me

Well - I kinda needed more custom coding, which is why I left that as a place holder.
So it probably is redundant.

I needed to also have a special price for each full week of rental - So the above code changed into something to the lines of:
if ($dayqty > 6) { (code to give the special pricing) }

which is why I put an if statement there.

I'm assuming you took out the redundant code and it works?

I'm sorry for the late reply, but was locked out of my account =)

I'm glad this post helped some people.

GreyHawk's picture
Offline
Joined: 03/17/2009
Juice: 174
Jordana -- thanks! That worked great for me, too.

I used the central bit of code (excluding the Function wrapper) and put it into the Custom Price calculation on my product; it passed the right value and worked like a charm.

Not sure if the repetition of the item qty setting is necessary, but I kept it -- I'm still getting up to speed on coding here.

PaniX's picture
Offline
Joined: 01/27/2010
Juice: 95
Re: Changing the quantity field to the value of a cck computed f

Hi i think this thread would be the solution to the problem in my scenario. I am trying to figure a way to create a reservation / rental feature for some products. Basically the rental is restricted to only selecting one day, which gives some ease to my problem. I am working on modifying right now the hotel booking module base code to adapt it to my needs, and i am looking for some sort of method to affect the availability. The hotel booking module seems to have the quantity functionality disabled. The method stated above basically connects a cck field with the quantity field of the product if i understand? and you achieve this by pasting this bit of code to the field of the "custom price module"?

jordana@drupal.org's picture
Offline
Joined: 02/13/2009
Juice: 18
Re: Re: Changing the quantity field to the value of a cck comput

First off - let me give you the setup of the content type.
There is a date field with a start rental date and an end rental date.
I then have a computed field that takes these 2 dates and extracts the number of days.

What the code does is basically this:
It takes the number of days of the computed field and inputs that into the quantity field.
This makes the quantity of the checkout item reflect the actual number of days that the customer would like to rent the product.

It seems simple, but I had a heck of a time implementing it as my drupal and ubercart coding skills were very very limited at that time =)

I did all this by making a custom module, not by using the custom price module. So the code above went into my custom module named "uc_date_qty".

I hope this helps you out a bit.
Let me know if you need more of my code, but am not sure it'll really work with the hotel booking system. You might have to change it a bit, because I'm sure they use different fields.