Discussions

How to fetch customer_billing from an order?

Hello!

After an order is completed I need to fetch the customer billing via rules to attach it to the product with an Entity Reference Field.

I tried this:
- fetching the customer billing from an oder: $variable_added = [commerce-order:commerce-customer-billing];
- loop all line items of an order
- get product
- if product has field: Entity Reference
- get Entity Reference Field as Customer Profile List
- add item to a list: fetched customer profile
- save the product

The problem ist:
the customer profile is not attached to the product after completing an order.

So... what's wrong?

thanks

Posted: Oct 25, 2011

Comments

rfay Randy Fay on October 26, 2011

Hi dasmoermel, remember that people are going to find your question when they google with your same problem. Please always report here the solution that you find for any problem, in as much depth as you know how. That's part of participating in a community.

dasmoermel on October 26, 2011

Hey rfay!

Ofcourse, you are right, sorry for that!

I needed the customer profile/billing attached to the bought Products to offer these informations in a view for the ShopAdmin. In that customer profile are 2 fields, a name-field and a telephonenumber-field.

I defined a new field for my product-type "tour", it's an Entity Reference Field->http://drupal.org/project/entityreference.

When an order is checked out, I fetch the event via rules.

Rule
Event: "Checkout completed"
Condition: "Order:type = commerce_order" -> this is the solution to "see" the customer profile from an order.
Actions:
- set the order state to completed
- loop all lineitems:
run the rule component "get Product from LineItem" Param: LineItem, Customer-Profile/Billing
decreasing stock

Rule "get Product from LineItem"
Parameter: LineItem, Customer-Profile
Provided: none
condition: "LineItem:type = article" -> to "see" the referenced Product
action:
- run the rule component "add customer profile to product" Param: Product, Customer-Profile/Billing

Rule "add customer profile to product"
Parameter: Product, Customer-Profile
Provided: none
condition: "Product:Type = Tour" -> to "see" field-commerce-prod-bookings (entity reference field)
action:
- add item to a list: product:field-commerce-prod-bookings item: customer-profile
- save product

There might be a simpler way, but it works great for me.