Created
July 12, 2022 15:21
-
-
Save cbarley10/e12b0d5f3c32443a287a65df9f64df5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def save_order(self, order): | |
# Check to see if there are any items in the order | |
# We checked earlier but we need to check again for Spree v2.0 | |
if ( | |
not order["customer"][ProfileConstants.EMAIL_KEY] | |
or not is_valid_email(order["customer"][ProfileConstants.EMAIL_KEY]) | |
or not order["items"] | |
): | |
return | |
customer_properties_dict = copy.deepcopy(order["customer"]) | |
customer_properties = CustomerProperties( | |
email=customer_properties_dict.pop(ProfileConstants.EMAIL_KEY) | |
) | |
customer_properties.set_custom_properties(customer_properties_dict) | |
if order["state"] in ("complete",): | |
placed_order_event = OrderPlacedEvent( | |
service=self.service, | |
unique_key=order["id"], | |
customer_properties=customer_properties, | |
event_data=order["properties"], | |
time=order["completed"] or order["updated"], | |
) | |
self._publish_event(placed_order_event) | |
if order["shipment_state"] == "shipped" and order["shipped"]: | |
order = self.normalize_tracking_numbers(order) | |
fulfilled_order_event = OrderFulfilledEvent( | |
service=self.service, | |
unique_key=order["id"], | |
customer_properties=customer_properties, | |
event_data=order["properties"], | |
time=order["shipped"], | |
) | |
self._publish_event(fulfilled_order_event) | |
if self.new_customers_group: | |
customer_properties.set_custom_properties( | |
{ | |
CustomerGroup.SUBSCRIBE_EVENT: { | |
CustomerGroupKeys.GROUP_ID: self.new_customers_group.id, | |
CustomerGroupKeys.ADD_WITH_SIDE_EFFECTS: self.new_customers_queue_autoresponders, | |
CustomerGroupKeys.SUBMISSION_SOURCE: SubscriptionSource.SUBSCRIBED_VIA_INTEGRATION_SPREE, | |
}, | |
} | |
) | |
started_checkout_event = CheckoutStartedEvent( | |
service=self.service, | |
unique_key=order["id"], | |
customer_properties=customer_properties, | |
event_data=order["properties"], | |
time=order["created"], | |
) | |
self._publish_event(started_checkout_event) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment