Open alanmels opened 1 year ago
@alanmels thank you for posting this comment. As a maintainer for both Entity Plus and Ubercart I'm very familiar with the functions you are using here.
In a nutshell: you are trying to use entity_plus_view
on the wrong type of entity, and on entities that have not been saved and don't have IDs yet. entity_plus_view
is intended to be used with saved entities that possess IDs.
I see several issues with your code above. You are using a few Ubercart functions as if they were a full API, but unfortunately those functions are used as part of a whole process. You are bypassing several steps in the process.
uc_cart_item
entity, which is saved and has its own cart_item_id
. So, the first problem I see in your code is that you are passing nodes to a function that takes saved uc_cart_item
entities.uc_order_product_view_multiple()
is supposed to be used at a specific moment of the checkout process: when a customer moves from the Shopping cart form, into the Checkout formuc_order
entity has been saved before invoking that function. This means that the order at this moment contains an array of either uc_cart_item
entities WITH order_product_id
s (which are added when you save the order), OR uc_order_product
entities with order_product_id
s. (This BTW is one weakness of Ubercart architecture - the fact that the function uc_order_product_view_multiple()
can receive either uc_cart_item
entities, or uc_order_product
entities.)So, my suggestion to you is that you take a close look at the whole process. It's possible to do all this programmatically, without doing all the UI forms. You would first need to add a node product to the shopping cart, then get the cart into the order and save it, then pass those items to uc_order_product_view_multiple()
(more or less - other details may be missing in this description).
As a side note: I don't think it's a good idea to change an Entity Plus API function (entity_plus_view
) to accommodate a problem that can be preventing by correctly using the Ubercart processes.
I can help further with this if you need feedback or a second opinion 😄
I want to clarify/expand my comment - the nodes you are passing to uc_order_product_view_multiple()
ARE indeed saved and contain a nid. The issue is that uc_order_product_view_multiple()
expects entities with order_product_id
s set, which are obtained as Ubercart loads the nodes into the cart (creating uc_cart_item
entities) and then loads those into an order and saves the order.
@argiepiano I do appreciate much your insights. I've got the whole process working in regular conditions, however I'm trying to change the product items on the cart/checkout
page depending on the user-chosen cycle recurring cycle, and because I need to reflect the change in individual item prices and the subtotal, I have to use an ajax_callback
where all the regular workflow gets awry. Let me try few more things and come back to the issue.
First, let me paste just the relevant part of a long custom function:
At this stage, the
$new_items
array contains twoNode
type objects as dpm() shows:Further processing through the
uc_order_product_view_multiple()
function turns theNode
objects toUcOrderProduct
objects.The problem starts when the
entity_plus_view()
looses one of the array members and returns incomplete result. Unfortunately, the function was returning just one item:until I changed the entity_plus_view() which looks like:
to:
Turns out if
$entity->order_product_id
is not assigned yet, then theentity_plus_view()
returns just one item (probably the last one overwrites the first one).I'm now trying to figure out if there is a way for the newly formatted
Node
objects (order products) to already haveorder_product_id
before passing touc_order_product_view_multiple()
function, but I thought to report this anyway, so you, guys, could consider changing theentity_plus_view()
function slightly to make it foolproof against similar cases whenUcOrderProduct
entities with noorder_product_id
are served to it.