I'm trying to create a rule that applies an automatic coupon discount if the custom address field "sibling" box is checked by the user (camp registration - 10% discounts for siblings). All of the default address fields are available, but not the custom ones. Is there a way to add these to the dataset in Rules?

Files: 
CommentFileSizeAuthor
#9 uc_addresses7-uc_order-and-user-address-properties-1900468-9.patch3.28 KBMegaChriz
PASSED: [[SimpleTest]]: [MySQL] 3,174 pass(es).
[ View ]
#8 uc_addresses7-uc_order-address-properties-1900468-8.patch1.68 KBMegaChriz
PASSED: [[SimpleTest]]: [MySQL] 2,895 pass(es).
[ View ]
#2 Screen Shot 2013-01-29 at 6.22.58 PM.png88.31 KBlhubbert

Comments

MegaChriz’s picture

Status:Active» Postponed (maintainer needs more info)

So your use case is to check if the customer has any address in his/her address book that has the "sibling" box checked? I'm afraid that isn't possible to do in Rules at the moment without custom code. I assume your were trying to access the addresses via the user object and the default addresses are there, because they are exposed as tokens.

If, on the other hand, you were trying to check if the customer has checked that box on the checkout page for either the delivery or billing address, then you should access the custom field via the order object instead.

For understanding your use case better, it may help to post an export of the Rule you are trying to create.

lhubbert’s picture

StatusFileSize
new88.31 KB

Hi Mega - the second is what I'm trying to do. I only want to check the billing address of the current order, which has a custom checkbox for Sibling. I believe I should do a Data Comparison, with the Data Selector "current-order:billing-address:" but there are no custom options available for the custom field (see attached). Thanks if you can help - this is the last thing I need to get in place to be ready to launch.

Lisa

MegaChriz’s picture

I have taken a look in Rules and I guess I was wrong about that tokens would be exposed in Rules in data selectors. With me, custom extra address fields didn't show up either. I have an idea about how I could expose the extra fields for the data selector, but that might quite take some time to implement if I would take it carefully (thus to be sure without breaking any other functionality). Anyway, I already found out that it would be a lot easier to do if addresses (from Ubercart Addresses) were entities. I already started to work on that, see #1831424: Turn Ubercart Addresses address into an entity and it's near finished. But it still could mean it could take a few weeks before I can came up with a decent solution for this issue (one that is tested very well).

"Execute custom PHP code"
As a workaround, you could use PHP in Rules to check if the checkbox is checked. There are two address objects attached to the order (instances of UcAddressesAddress). These can be accessed like this:

<?php
// Delivery address.
$address = $order->uc_addresses['shipping'];
// Billing address.
$address = $order->uc_addresses['billing'];
?>

On the address object you can call the method getField() to get the value of your custom field. See also the documentation about this.

I assume you created the extra field with the Extra Fields Pane module. In that case, your PHP rule would look something like this:

<?php
if ($order->uc_addresses['billing']->getField('ucxf_sibling')) {
  return
TRUE;
}
return
FALSE;
?>

If your field was created in an other way, you would still access the field the same way, but your field's name would probably not have the 'ucxf_' prefix.

So, to sum things up:

  • Extra address fields are not yet available as selectable data in Rules.
  • A workaround would be to use a PHP rule (called "Execute custom PHP code").
  • Exposing extra address fields to Rules will be easier to do when #1831424: Turn Ubercart Addresses address into an entity is completely done.

If you would like to help with coding and testing to get the extra address fields as selectable data in Rules, let me know and then I will explain my ideas about it in more detail.

lhubbert’s picture

Hi Mega,

I know I had looked at #1831424: Turn Ubercart Addresses address into an entity while I was trying to solve the problem. Glad I was on the right path with that. I definitely am using the Extra Fields module (and my field is 'ucxf_sibling' as you note).

I toyed with the PHP above but honestly that's out of my skillset to figure out. We go live in two weeks or so, I'll keep an eye on your entity project. If I have to I can use a "SIBLING" discount code, just so much less elegant than an automatic discount based on a checkbox.

Thanks for detailing the issues so clearly. I'd love to get this working but don't want to take your time to solve it.

Lisa

MegaChriz’s picture

Category:support» feature
Status:Postponed (maintainer needs more info)» Postponed

Hm, well then it's likely you have to go for the less elegant way for now. I'd like to solve this issue anyway, as having extra address fields as selectable data in Rules could be useful for other use cases also. As I look how far I am with the entities thing, it might be possible to implement a solution for this issue within two weeks, but I can't make any promises.
I will not pick this issue up until I'm sure #1831424: Turn Ubercart Addresses address into an entity is fixed, thus until then this issue is postponed.

lhubbert’s picture

Hey Mega, I saw you committed #1831424. I updated to dev, but didn't see any change. Is there a specific thing I should be doing to see custom fields available to Rules? I'd love to make my discounts work this way and have a few more days to tweak before we go live.

Thanks,
Lisa

MegaChriz’s picture

Assigned:Unassigned» MegaChriz
Status:Postponed» Active

I still have to make a fix for this issue. I have an experimental fix for this locally, maybe I can post that later today here as a patch.

Be careful with the dev, though. Since the entity integration is pretty new, it could have issues in combination with other modules that act upon entities, though I did test the implementation with Views, Panels, partly with Rules and with most common Entity API functions.

MegaChriz’s picture

Status:Active» Needs review
StatusFileSize
new1.68 KB
PASSED: [[SimpleTest]]: [MySQL] 2,895 pass(es).
[ View ]

This patch tells Entity API about the address objects that are attached to the order. After applying the patch and clearing the caches, you should be able to access extra fields via order:uc-addresses-billing-address for the billing address and order:uc-addresses-delivery-address for the delivery address.
Note that the properties order:billing-address and order:delivery-address are also available, but you won't find the extra fields there. I chose to declare two new properties instead of overwriting the two existing ones to avoid possible conflicts.

Let me know if this works for you. :)

MegaChriz’s picture

StatusFileSize
new3.28 KB
PASSED: [[SimpleTest]]: [MySQL] 3,174 pass(es).
[ View ]

In addition of the patch in #8, this patch also exposes the default addresses of the user object to Rules. I've done a few testing with this and I think this code is enough to get access to (custom) address fields in Rules (in user context and Ubercart order context).

Let me know if something still misses! Else, I assume this patch is complete.

MegaChriz’s picture

Status:Needs review» Fixed

I think the patch exposes the custom address fields in Rules correctly for the order object and for the user object. Committed #9.

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.