workflow_ng custom PHP condition to check product attribute option?

Posts: 55
Joined: 10/08/2007
Getting busy with the Ubercode.

Hi Smiling

I'm trying to create a workflow_ng configuration that triggers on 'Customer completes checkout' -- and checks whether any products in an order have a particular attribute option selected. My goal is to send a different email with instructions to the user based on which of three options they select.

Currently the only way I know how to do this is create a SKU for each attribute combination on the product that contains my desired option - and use workflow_ng to check the SKU. The problem here is there are like 24 pages of combinations just for 3 options (because I have other attributes with many options, if you see what I mean), and while it would only take a few minutes per product to paste the SKUs into all of those attribute combinations, they get reset whenever I change any attribute settings. kind of not an ideal way to do it.

I was hoping to use the 'Execute custom PHP code' condition, and add a snippet which would be able to check the specific attribute option I want to use for the condition - either by the option name or $key (eg, '24').

So this is my support request in a nutshell -- what would a PHP snippet look like to check a particular attribute option? I'm taking a wild guess based on the uc_attribute_options function (but a slightly less wild guess is I'm totally wrong about this):

<?php
// set condition to FALSE by default
$output = FALSE;

// set condition to TRUE if any product in order has selected my desired option
$myoption = '24';
while (
$attribute->options as $key => $data) {
    if (
in_array($key, '24')) {
     
$output = TRUE;
    }
}

// if output is FALSE, my action will not meet the condition - if TRUE it will
return $output;
?>

Am I totally way off here? Is there a better way to check this?

Thanks in advance for your advice on this one!
Scott

PS, I noticed there's not much in the workflow snippets page on ubercart... maybe this could go in there once we get a working snippet? Smiling

PPS, For the email, I planned to use the workflow_ng action 'Send a mail to an arbitrary mail address', and add the [order:order-email] token as the Recipient - I haven't been able to test this but I'm assuming this should work?

Posts: 55
Joined: 10/08/2007
Getting busy with the Ubercode.

For anyone wondering... this is the module that allows selecting the SKU:
http://www.ubercart.org/forum/support/4589/workflow_trigger_a_particular...
...very useful, just doesn't solve my particular issue above Sticking out tongue
So any advice about the custom php condition would be totally appreciated - thanks!

Posts: 5625
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

I'm pretty sure the problem here is that you aren't checking any product's attributes in particular. Also, you don't really need to use the $output variable... you can simply return TRUE; right there in the loop and it will be a teeny tiny bit quicker (like... microseconds or something Sticking out tongue). Anyways... how about something like:

<?php
 
// Loop through all the products on the order.
 
foreach ($order->products as $product) {
   
// Look in an array of options for the right one.
   
if (in_array('some_option', array_values($product->data['attributes']))) {
      return
TRUE;
    }
  }

 
// Return FALSE to show that no matches were found.
 
return FALSE;
?>

You can see what a products array for an order looks like by adding the following snippet to a configuration:

<?php
  drupal_set_message
('<pre>'. print_r($order, TRUE) .'</pre>');
?>

On the Livetest, it looks like this:

    [products] => Array
        (
            [0] => stdClass Object
                (
                    [order_product_id] => 1261
                    [order_id] => 635
                    [nid] => 6
                    [title] => cha0s Jams - Recursion Emerge
                    [manufacturer] =>
                    [model] => CHA0S-MP3
                    [qty] => 1
                    [cost] => 0.00
                    [price] => 14.95
                    [weight] => 1
                    [data] => Array
                        (
                            [restrict_qty] => 1
                            [attributes] => Array
                                (
                                    [Purchase type] => MP3
                                )

                            [model] => CHA0S-MP3
                            [shippable] => 1
                            [module] => uc_product
                        )

                )

        )

You can see how we have to access the attributes from this... Purchase type is an attribute, MP3 is an option. Basically, you'd replace 'some_option' from my code above with what your option is.