I have a website, where users can add their products. After a user purchases a product and pays for it, I want to create some custom logic. I need to know the authors of the individual items purchased. After following this guide I have the below EventSubscriber code
public function orderCompleteHandler(WorkflowTransitionEvent $event) {
$order = $event->getEntity();
$items = $order->getItems();
foreach($items as $item){
error_log($item->getTotalPrice()->getNumber());
// Here I want to get the item's author
}
}
(Please disregard the error_log function as it is only for developing purposes)
It works as expected and lists the prices (one-by-one) of the items purchased. Now I want to list the authors of the items as well. Unfortunately there is no built in method to get the authors. I was thinking to go in the direction of getting the order item as an entity and then getting the entity author that way. Any suggestions how to do this? or any other suggested approaches?
$item->getOwner()
EntityOwnerInterface
which means they don't store the owner of the product by default. I think you'd need a custom solution - either extend the backend class with a custom one, and implement this interface, or add an entity reference to products for users, and save the owner to that entity when commerce items are created. I don't see that you can do it with the default setup. Although you may be able to find a module that provides this functionality.commerce_order_item
table in the database, or maybe thecommerce_order_item_data
table, and see if there is a column for 'Author'. Edit: Sorry, 'owner' not 'author'.