Jump to content

Get price via API for customized data


Recommended Posts

Greetings

I'm creating a module for the specific product. I'm using text customization, and want to add a product with customization into cart via my module. I've already done with product adding to a cart, and now need to calculate a price for customization. I need to ask 3d party API to calculate a price for customization. Off course I can change ps_customized_data.price directly in DB or use override, but I would like to know if there's a better way?

My current code to add item into cart:

$this->context->cart->addTextFieldToProduct(
            $product->id,
            $customizationFieldId,
            Product::CUSTOMIZE_TEXTFIELD,
            $pathData['name']
        );

        $exising_customization = Db::getInstance()->executeS('SELECT id_customization FROM ' . _DB_PREFIX_ . 'customized_data ORDER BY id_customization DESC LIMIT 0,1');
        $customization = $exising_customization[0]['id_customization'];
        $cart->updateQty(
            1,
            $product->id,
            null,
            $customization,
            'up'
        );

        die(json_encode([
            'result' => $cart->update()
        ]));

addTextFieldToProduct does not allow me to control price, and I do not see some entity to control those DB data. Also in updateQty I would like to have 'equal' operator, cause I need only 1 item per customization, but not so crucial.

Also as I've understood I can't change info inside hooks, right? $data in Hook::exec($name, $data) is not passed by pointer. I think good points to calculate price  are  actionCartUpdateQuantityBefore or actionCartSave.

Link to comment
Share on other sites

$customization = $exising_customization[0]['id_customization'];
        Db::getInstance()->execute(
            'UPDATE `' . _DB_PREFIX_ . 'customized_data`
                    SET `price` = ' . $pathData['price'] . '
                    WHERE `id_customization` = ' . (int)$customization . '
                    AND `value` = "' . $pathData['name'] . '"'
        );
        $cart->updateQty(
            1,
            $product->id,
            null,
            $customization,
            'up'
        );

 

solved via DB, looks as this code work for me, but will be happy if someone adds better solution

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...