0

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

addItem($item_attributes, $form_params)

in the file:

/system/expressionengine/third_party/store/src/Store/Model/Order.php

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?

0

I seem to remember running into a similar problem. Unfortunately, I don't remember exactly the steps I took, but I just looked at my final code, and it looks like this:

$order->addItem(array('entry_id' => $this->entry_id, 'update_qty' => 1));

I do remember update_qty working while item_qty didn't. However, this is also running inside the store_order_recalculate_start hook instead of the store_order_recalculate_end hook.

| improve this answer | |
  • Thanks Bryan, using the store_order_recalculate_start hook worked just fine. At least you've answered my question on whether or not this is how to properly add an item programatically. Unfortunately, for what I'm trying to do, there is room for error if the order calculations aren't completed before I try to add a free product. – Javier Nov 7 '14 at 18:08
1

If I use either my or Bryan's solution to add an item programmatically, I can add an item in the store_order_recalculate_start hook. To make it work in the store_order_recalculate_end hook, I have to call

$order->recalculate();

in the line after I add my item for the item to actually show up in the cart. I don't like re-calling function, but it makes sense and appears to be necessary for this.

| improve this answer | |
0

You don't happen to have a copy of this function anywhere in a repo by any chance? I have a client who wants exactly this and I am at this point of adding to cart.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.