2

I've set up a fabric shop, which requires free sample purchasing. I've implemented this without issue using on the fly products for each fabric. However, the client now requires that after x amount of samples have been added (across all products) each additional sample should be charged.

I've spent several days trawling the developer docs & internet trying to find a solution, but so far it has eluded me. Can you please confirm the correct procedure for modifying a dynamic products price which is already in the cart. I've looked at the cartthrob_update_cart_end hook and $this->core->cart - but can't seem to figure it out.

Thank you.

1

Sounds like you are building a custom extension. A start might look something like this.

Would have to know a little bit more about your products to give you a better example.

public function cartthrob_update_cart_end()
{
$items = ee()->cartthrob->cart->items_array();

foreach($items as $key => $value)
{

    if($value['title'] == "Some on the Fly Product")
    {
        // do something
    }

}

$this->EE->cartthrob->cart->save();

} // END cartthrob_update_cart_end
| improve this answer | |
  • I have a similar issue, but the // do something may need a little more explanation in order to actually amend price. For example, my "do something" has this $value['price'] = "1"; $items[$key] = $value; So the items prices are changed in the array, but this does not change the price in the cart. So do you or anyone know how to actually change pricing in this hook? Thanks – Laurence Cope May 14 '14 at 7:54
  • Could you start a new question and post your sample code along with what you'd like to accomplish? That would be probably be easier than trying to describe it in the comments. – mjr May 14 '14 at 13:11
  • I could do, but my code is the similar to above, but I cant find any way to change an items details (your // do something section). So I think expanding your "do something" section to show how to modify product data would be good, the original question asked it. – Laurence Cope May 14 '14 at 17:16
  • I figured it out! Will reply and expand upon your answer. – Laurence Cope May 14 '14 at 17:20
1

Using mjr's code as a basis, to actually edit the cart items data you will need to use the add_item() method which will implement an update() if the item exists in the cart already. You don't need the save() method either. So to change a product price to 10 for example:

public function cartthrob_update_cart_end()
{
    $items = ee()->cartthrob->cart->items_array();

    foreach($items as $item)
    {
        if($item['title'] == "Some on the Fly Product")
        {
            $item['price'] = 10; 
            ee()->cartthrob->cart->add_item($item);
        }
    }
} // END cartthrob_update_cart_end
| improve this answer | |
  • great! glad you got it working – mjr May 14 '14 at 18:50

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.