Skip to content

Instantly share code, notes, and snippets.

@cbarley10
Created July 12, 2022 15:21
Show Gist options
  • Save cbarley10/2c5e559e2c4c4be469cd9268b47ffbc5 to your computer and use it in GitHub Desktop.
Save cbarley10/2c5e559e2c4c4be469cd9268b47ffbc5 to your computer and use it in GitHub Desktop.
def save_order_items(self, order):
if order["state"] not in ("complete",):
return
timestamp = (order["completed"] or order["updated"]) + timedelta(seconds=1)
for item in order["items"]:
quantity = item["Quantity"]
if not quantity:
continue
properties = copy.deepcopy(item)
properties["Quantity"] = 1
if properties.get(EventConstants.VALUE) is not None:
properties[EventConstants.VALUE] = properties[
EventConstants.VALUE
] / float(quantity)
if item[EventConstants.EXTRA].get("Options"):
properties.update(
dict(
("Variant Option: %s" % k, v)
for k, v in six.iteritems(item[EventConstants.EXTRA]["Options"])
)
)
for i in range(int(quantity)):
ordered_product_event = ProductOrderedEvent(
service=self.service,
unique_key="{}:{}:{}".format(order["id"], item["ProductId"], i),
customer_properties=CustomerProperties(
email=order["customer"][ProfileConstants.EMAIL_KEY]
),
event_data=properties,
time=timestamp,
)
self._publish_event(ordered_product_event)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment