Hello.
I wish to take the price from cck field on the product page. $item->price = $node->field_fieldname['value']; Don't work.
Is it possible?

Comments

justclint’s picture

Hey Guys, Im trying to do kind of the same thing. Basically the site sells silver. There are various products of different silver coins. The unique thing is that all the products have the same price(sort of). They all have a market value price.

When selling silver there is something called a premium. Kind of like when you exchange money, you dont actually get the real market value, you get charged for the change making the exchange as well.

So in our case for example, on one product we might say price = market value + $0.89(the premium) per ounce. If you buy 500 ounces (quantity of 1 = 100 pieces) or more its price = market value + $0.79.

So what Ive done is created a content type called "Current Market Value" and it has a field called "field_market_price." Ive created a single node of this content type to store the current market value.

In my template.php file Ive taken that value and turned into a variable called $marketprice to be available site wide.

For my products, the sell price in one example will be $0.89.

Id like my custom price field to do something like:

$item->price = $marketprice + $item->price;

if($item->qty > 5) {
  $item->price = ($marketprice + 0.79) * $item->qty;
}

else 

if($item->qty > 10) {
  $item->price = ($marketprice + 0.69) * $item->qty;
}

The problem Im having is that the custom price field doesnt seem to allow my custom variable. Is there a way around this?

Thanks!

justclint’s picture

Title: take the price from cck field » take the price from custom sitewide variable

I just changed the title of this post.

WebNewCastle’s picture

I realize both of these posts were from a while ago, but in case its useful to either of you or someone else with a similar need ...

Both of these can be done.

Digital_fox, I've tied Custom Price functionality to CCK field values before. It is probably just a syntax issue. I could be wrong, but I think when CCK fields are printed there is usually another part to it. You can do a print_r on $node or $node->field_fieldname to be sure, but I think its usually more in the format of:

$node->field_fieldname[0]['value']

Justclint, you can do what you need, but perhaps skipping the template part. I could be wrong, but I don't know that having that defined and available for your template would necessary make it available for this module, Ubercart, the node, etc. Perhaps it does. But, instead you could just pull particular node's CCK field where it's being stored directly. Not only can Custom Price be tied to a CCK field in the node, it can be tied to a CCK field in any node. You can use node_load to load up the node in question and then have it pull from the specific CCK field. So you might have something similar to above with $reference_node-> and the rest and have $reference_node storing what you get from node_load.

There are some other options as well.

Instead of loading and referencing nodes, you could have store it in a variable (in your installation's variables table). You use variable_save and variable_get and make a custom module, or perhaps use Rules or maybe it would work to use Custom Field with your reference node to save all the values as variables.

Another approach would be to use Custom Field to grab the field(s) in the reference node - your Custom Field for a specific product class could have this code as the default. But, this one strikes me as not being the most efficient option.

AlexanderPop’s picture

I created CCK text field field_dynamic_price
than I used $item->price = $node->field_dynamic_price[0]['value'];
also tryed
$item->price = check_plain($node->field_dynamic_price[0]['value']);
just added 123 number in field_dynamic_price
and it doesn't work. when I add product to the cart prize becomes 0.00
what do I do wrong? please help

WebNewCastle’s picture

Maybe try wrapping it in "abs" or "int" instead?

AlexanderPop’s picture

#WebNewCastle
thanks for post, but could you clarify, please? thanks again

chesketh’s picture

Did you first find the nid in question and call

$node=node_load($nid);

and then call:

$item->price = check_plain($node->field_dynamic_price[0]['value']);

The hard part is pulling out the node id of the node created. I am currently pulling the last node id created by the person placing the order. This seems to work, but the new price is applied to all previous items of the same type in the cart.

AlexanderPop’s picture

great it started to work but only with static value in field_dynamic_price (like 123)
when I use php code in field_dynamic_price
somecode
it shows 0.00 again

chesketh’s picture

I've solved it, see this comment: http://drupal.org/node/559508#comment-3266432

AlexanderPop’s picture

same
when I use php tags and something between it it shows $00.0 after add to cart
if just 333 (without php tag), for example - it shows $333.00 - so works well. but I need to use dynamic data which generates in $node->field_dynamic_price[0]['value']
tryed $item->price = check_plain($node->field_dynamic_price[0]['view']); - no lack...

we use on site custom module which generate data from dynamic datafile. so if I put somewhere in content this code:
calcvalue_getvalue(13, 10, 'XAU', .1) it shows 141.98 when rendering page
so I put calcvalue_getvalue(13, 10, 'XAU', .1) code in field_dynamic_price field ("php" is input format of course) and I see number "141.98" when page is rendering, but $item->price = check_plain($node->field_dynamic_price[0]['value']); doesn't read this data...

tr’s picture

Status: Active » Closed (duplicate)

Duplicate of #559508: Reference cck field. The question is answered over there.