THELIA Forum

Welcome to the THELIA support and discusssion forum

Announcement

Rejoignez la communauté sur le Discord Thelia : https://discord.gg/YgwpYEE3y3

Offline


Hi, I've created a module with a DBtable to save data related to the cart_item id.
I've set mytable id as foreing key to the id of cart_item.
Everything works well during the session; adding items to cart also save data to mytable.. but when I restart browser and reload mysite cart item still there and data on mytable was lost, the table come back cleaned.

I think I've to ovveride something but I'm stil havent found what.

Can you help me?
thanks

Last edited by meo (26-12-2017 11:25:36)

Offline


Under some circumstances, Thelia duplicates a cart when re-creating a user session (see Thelia\Action\Cart::manageCartDuplicationAtCustomerLogin()). The original cart items are deleted from the database during this process (see Thelia\Model\Cart::duplicate()). As you have a DELETE CASCADE on your FK, the related rows in your table are also deleted.

Removing the DELETE CASCADE from your FK is not the proper solution, as you have to attach your data to the duplicated cart items. The proper solution is to create a service that handles the TheliaEvents::CART_ITEM_DUPLICATE to manage cart items duplication.

For example, let's say that your table is CartRelatedData.

...
class YourEventManager implements EventSubscriberInterface
{
    public function cartDuplicateItem(CartItemDuplicationItem $event)
    {
        $list = CartRelatedDataQuery::create()->findByCartItemId($event->getOldItem()->getId());

        /** @var CartRelatedData $original */
        foreach ($list as $original) {
            // Duplicate our data...
            $copy = new CartRelatedData();

            $original->copyInto($copy);

            // ... and attach it to the new cart item
            $copy
                ->setCartItemId($event->getNewItem()->getId())
                ->save();
        }
    }

    public static function getSubscribedEvents()
    {
        return [
            ...
            TheliaEvents::CART_ITEM_DUPLICATE  => ['cartDuplicateItem', 130 ],
            ...
        ];
    }
}

Be sure to declare the YourEventManager class in the config.xml file of your module, and clear your cache.

This way, the item attached to the original cart item is deleted via the FK (this is fine), and a new item is created and attached to the duplicated cart item.


OpenStudio Toulouse

Offline


Wow! great answer... the concepts are all clear.
I'm not sure to be at this skill level, but following step by step your way I think the things could be done quite easily.

I hope I don't have to disturb you again so soon.

Grazie

Offline


It's done! now it work right.

Offline