I have a client that needs to add a "free gift" to their customers' orders for every $100 that a customer purchases.
I'm using ExpressionEngine 2.9.0 and Expresso Store 2.3.1. I've created an extension hook that executes on "store_order_recalculate_end." The hook is executing and I'm using XDebug to debug our store.
I'm trying to figure out how exactly to add an item to the order programatically. I found the function
in the file:
Our store is working as expected with and without the hook installed. I captured the $item_attributes and $form_params values when I post an item from the shopping cart using XDebug and by setting breakpoints in Sublime Text. Using those captured values, I duplicated those arrays in my extension hook, and referenced the entry_id of the free gift that we want to give to customers.
The following code in my hook creates the following arrays and then executes the addItem() function of the Order class.
$item_attributes = array( 'entry_id' => $this->freeGiftCardEntryId, 'item_qty' => 1, 'line_item_id' => rand(0, 10000), 'is_gift_to' => '', 'is_gift_from' => '', 'is_gift_special_note' => '', 'is_gift_shipping_full_name' => '', 'is_gift_shipping_address' => '', 'is_gift_shipping_city' => '', 'is_gift_shipping_state' => '', 'is_gift_shipping_province' => '', 'is_gift_shipping_country' => '', 'is_gift_shipping_zip_code' => '', ); $form_params = array( 'form_id' => 'product_form_0', 'form_class' => 'product', 'input:line_item_id' => 'Line Item ID', 'input:is_gift_to' => 'Is Gift To', 'input:is_gift_from' => 'Is Gift From', 'input:is_gift_special_note' => 'Special Note', 'input:is_gift_shipping_full_name' => 'Gift Shipping Full Name', 'input:is_gift_shipping_address' => 'Gift Shipping Address', 'input:is_gift_shipping_city' => 'Gift Shipping City', 'input:is_gift_shipping_state' => 'Gift Shipping State', 'input:is_gift_shipping_province' => 'Gift Shipping Province', 'input:is_gift_shipping_country_' => 'Gift Shipping Country', 'input:is_gift_shipping_zip_code' => 'Gift Shipping Zip/Postal Code', 'encrypted_params' => 1 ); $order->addItem($item_attributes, $form_params);
When I add an item, I can see that my hook is executing, and I can see it calling the addItem() function for every $100 spent. However, the item is never added to the cart. I believe that it "fails" on line 607 of the Order class in the addItem() function where it executes the following:
$item = $this->findExistingItem($entry_id, $modifiers);
I've compared the XDebug tracing information using a DIFF tool. Comparing a product that is added via the UI, and the product that is added programmatically, I can only see a few minor differences. From what I can tell, they are exact, except for some posted form values that aren't present on the one added programatically. However, I wouldn't call them specifically important.
My questions are, is this how you would add something programmatically? In addition, if this is how you would add it, are there other variables I need to post to identify the current user session?