This may be a newbie question, and if this is an RTFM issue, please direct me there.

In summary, here is what I am looking to do: I have added a single checkbox field to my product content type labeled "This is a Preorder Item" (machine name field_preorder_item) which when checked will present the user with a warning when adding the product to their cart.

I can't seem to figure out how to make this work with the existing Ubercart Rules Conditions. I thought that the Data Comparison condition would to the trick, but using uc-cart-item:node:field_preorder_item as the data to compare is not working. It reports it as invalid, even thought all of the other parts of the node are presented.

Is there another way to do this using existing functionality? I can't seem to find it.

I have also tried creating a custom rule condition using HOOK_rules_condition_info():

function MODULE_rules_condition_info() {
  return array(
    'uc_preorder_cart_condition' => array(
      'label' => t('Check if the order has preorder items'), 
      'group' => t('Order'),
	    'base' => 'MODULE_uc_preorder_cart',
    ),
  );
}

function MODULE_uc_preorder_cart() {
	return true;
}

Creating a rule with this condition works like a charm. Then again, it's always returning true. I do know that the code is running, which is something. What I can't seem to figure out is how to get access to either the cart item or the product or the cart itself in order to determine if it's field_preorder_item is true.

I tried adding a parameter to the MODULE_rules_condition_info array, but that only made things worse. I don't think having a Data Selector in the mix helps.

What am I doing wrong? How can I make this work?

Thank you for your help.

Comments

joelrichard’s picture

Status: Active » Closed (works as designed)

Okay, so I'm foolish. I was poking around in uc_order.module when I should have been looking in uc_cart.module. In there, I found uc_cart_get_id() which solved my problem perfectly. Here's my resulting code stub:

function MODULE_rules_condition_info() {
  return array(
    'uc_preorder_cart_condition' => array(
      'label' => t('Check if the cart/order has preorder items'), 
      'group' => t('Order'),
	    'base' => 'MODULE_uc_preorder_cart',
    ),
  );
}

function MODULE_uc_preorder_cart() {
  global $user;
  // We can get the order id from the cart. So we'll use that.
  if (isset($_SESSION['cart_order'])) {
    $order = uc_order_load(intval($_SESSION['cart_order']));
    // Check all of the nodes for all of the products in the order
    // If any have field_preorder_item set to true, return true
  } else {
    $order = uc_order_load(uc_cart_get_id());
    // Check all of the nodes for all of the products in the cart
    // If any have field_preorder_item set to true, return true
  }
  return false;
}

Chalk this up to inexperience. :) I'm closing this ticket as "Works as Designed".

joelrichard’s picture

This just keeps getting more fun by the minute!

The code for the uc_cart_get_id() is:

function uc_cart_get_id($create = TRUE) {
  global $user;

  if ($user->uid) {
    return $user->uid;
  }
  elseif (!isset($_SESSION['uc_cart_id']) && $create) {
    $_SESSION['uc_cart_id'] = md5(uniqid(rand(), TRUE));
  }

  return isset($_SESSION['uc_cart_id']) ? $_SESSION['uc_cart_id'] : FALSE;
}

And so it's getting the wrong cart for me because it's using my user id as the cart id. I don't know why this is the case. It's moot.

The issue is that the $_SESSION['cart_order'] number is NOT set or available during this process, so I just moved my custom preorder warning rule to when the "Customer starts checkout" event where I DO have that value in the $_SESSION.

Frustrating, but I have more fish to fry. :) Sorry for the spam.

longwave’s picture

FYI, the cart ID *is* the user ID for logged in users. The cart ID is a string of random characters for anonymous users. But cart IDs and order IDs are not the same thing!