8 replies [Last post]
mk123's picture
Offline
Joined: 12/13/2009
Juice: 41
Was this information Helpful?

Hello !

have tried the last days/weeks to do some customizing in drupal 6.x / ubercart and although I am sure it´s a great piece of code it´s getting very frustrating - if anyone can help me - please (!) do so...

I have a custom validation function which is called whenever something is put into the cart.
within I calculate the 'real price' (which is generated according to various attributes,...) and now I want to set the price of the product manually

printing $form_state['values'] results in:

Array
(
    [nid] => 82
    [qty] => 4
    [op] => In den Warenkorb legen
    [submit] => In den Warenkorb legen
    [node] => stdClass Object
        (
            [nid] => 82
            [type] => warengruppe_wunsch_pizza
            [language] => de
            [uid] => 0
            [status] => 1
            ...
            [sell_price] => 4.10000
            ...

(I am not even sure which $form field holds that value)

and I just want to set the price to the value I calculated so that it in all following displays and calculations the new price will be used
(I have to it manually because the way the price is calculated is quite complex)

I have tried everything, read various post, requests,... since now nearly 16 hours and I have achived nothing.

If anyone can point me the way or better (since I think it´s not more than 3-4 lines of code) show me how it´s done - well, you´d really help me to get over this stage of frustration

thanks,
m.

ps: I´m posting this question on another forum as well

jrowny's picture
Offline
Joined: 01/08/2009
Juice: 297
Re: using form_set_value to change a product price in UBERCART o

I have had a lot of success with the 6.x DEV version of Custom Price. I know it's only a dev release, but it has not failed me even on a production site.

Here's an example of a "pay per post" custom price code that I wrote.


$postsToPayFor = db_query("SELECT count(n.nid) as nCount
FROM {content_type_song} s LEFT JOIN {node} n on n.nid = s.nid WHERE n.uid = %d and n.type = 'song' and (s.field_paid_value is NULL or s.field_paid_value = 'Unpaid')", $user->uid);
$postsToCount = db_query("SELECT count(n.nid) as nCount
FROM {node} n WHERE n.uid = %d and n.type = 'song'", $user->uid);

$postCount = db_result($postsToPayFor);
$postCountOverall = db_result($postsToCount);

if($postCountOverall < 5){
$item->price = $postCount * 20;
}else if($postCountOverall < 11){
$item->price = $postCount * 18;
}else{
$item->price = $postCount * 15;
}

mk123's picture
Offline
Joined: 12/13/2009
Juice: 41
thanks for your reply !! if

thanks for your reply !!

if you could clarify: if I where to implement your code in my validate function
custom_add_to_cart_form_validation($form, &$form_state)
having only access to $form and $form_state - where and how could I make the changes you proposed ?

thanks again for spending your time !

regards,
m.

jrowny's picture
Offline
Joined: 01/08/2009
Juice: 297
Re: thanks for your reply !! if

You don't need to do any of that. You don't need to touch the add to cart form validation or anything.

Just install the custom price module: http://drupal.org/project/uc_custom_price

Now each product has a "custom price" field. Go to the product and edit it. You can put whatever PHP code you want in that field and when the item is added to the cart, it uses your PHP to calculate the price. Just make sure you set $item->price to the final price.

mk123's picture
Offline
Joined: 12/13/2009
Juice: 41
Re: Re: thanks for your reply !! if

thanks again for your reply - feels like I am just a few steps from my goal...

I have installed http://drupal.org/project/uc_custom_price (Ubercart Custom Price) and then http://www.ubercart.org/contrib/2381 (Attribute Tokens) and changed the .info file of the Attribute Tokens module like this: http://www.ubercart.org/issue/10825/incompatible_611#comment-38395
(still getting a strange php error: warning: Illegal offset type in ...\sites\all\modules\uc_attribute_tokens\uc_attribute_tokens.module on line 21.)

I have attached a small image with what I try to do - basically I have a few Attributes (for example in this image:
name:'Wunsch Pizza _Ananas_ Eigenschaften'
titel:'Ananas'
options:'0 mal', '1 mal'
type:select-field

name:'Wunsch Pizza _Barbeque-Sauce_ Eigenschaften'
titel:'Barbeque-Sauce'
options:'0 mal', '1 mal'
type:select-field

name:'Vollkornteig Eigenschaft'
titel:'' (-> don´t display the titel)
options:'Vollkornteig'
type:checkbox

now I just need to get the selected option of all available attributes somehow but all I get are error messages like:
Parse error: syntax error, unexpected T_STRING
Parse error: syntax error, unexpected '[', expecting ')'

I expecte the Attribute Tokens module is flawed - but is there any other way to get the name and the value of the selected attributes ?

if that answer could be clarified (how to get the selected attributes - which one/which option) I think I could solve my problem

thanks,
m.

AttachmentSize
dummy.jpg 6.99 KB
mk123's picture
Offline
Joined: 12/13/2009
Juice: 41
Re: Re: Re: thanks for your reply !! if

anyone wanting to do the same - here´s how I solved my problem:

- install the Ubercart Custom Price module
- add a code the the product (available through the custom price module) like

// various vars for calulation
$singlePartPrice = 0.80 ;
$freeParts = 3 ;

// custom code for calculation
$scriptPath = drupal_get_path('module', 'uc_displaychange').'/'.'calc_price.inc.php' ;
include ($scriptPath) ;

in the included script you can access the products data like this

print '<pre>' ;
$a = htmlentities(print_r($item, TRUE)) ;
$b = htmlentities(print_r($product, TRUE)) ;

print ($a."<br>---------<br>".$b) ;
print '</pre>' ;

later on you´d have to go through the values like this (depends on the structure of your data - see the print statement above)

if (isset($item->data[attributes])) {
foreach ($item->data[attributes] as $value) {
$key = array_search($value, $item->data[attributes]);

// skip all attributes which do not contain the string " mal" in their name
if (!is_array($value)) {
if (isset($product->attributes[$key]->options[$value]->name)) {
// .....
}
}
}
}

regards,
m.

kellogs's picture
Offline
Joined: 02/11/2010
Juice: 57
Hello, Thank you for these

Hello,

Thank you for these informations.
I was able to use uc_custom_price and uc_token.

But some problems remain and I don't know how to fix them.

Let me explain ...

I have two products with custom price code :
$scriptPath ='xxxx/calc_price.inc.php' ;
include ($scriptPath) ;

The code is:

$produit = $item->title ;
$quantite = $item->qty ;

if (isset($item->data[attributes])) {

  foreach ($item->data[attributes] as $value) {
  $key = array_search($value, $item->data[attributes]);

  // skip all attributes which do not contain the string " mal" in their name
  if (!is_array($value)) {
  if (isset($product->attributes[$key]->options[$value]->name)) {
    $attributes[]= $product->attributes[$key]->options[$value]->name ;
  }
  }
}
}

$format= $attributes[0];
$orientation = $attributes[1];
$epaisseur = $attributes[2];

This works great with on product.
But not with two products.
I always have the same title.

For example, $item->price and $item->qty output the right values.
But not $item->title. Is there a way to catch this value?

Also, I cannot catch the value of text fields in attributes.

Is there a short howto explaining how to catch the values of attributes?

Please help, I could not find information.

kellogs's picture
Offline
Joined: 02/11/2010
Juice: 57
Re: Hello, Thank you for these

i can catch $item->title, sorry for the error. I found the bug in my code.
Sorry for the confusion.

dallasclark's picture
Offline
Joined: 05/04/2010
Juice: 8
Final Price

Can you tell me where to find the setting or how to change the final price to $item->price please? I can't seem to be able to find it.

I've installed the custom price module but it has no affect on the price so far.

----------------

Dallas Clark
PHP, MySQL, JavaScript, AJAX, Web, Facebook, iPhone, and iPad developer

Australia
http://www.dallasjclark.com