Jump to content

Updating wholesale price.


Recommended Posts

Good Morning,
I'm in dire need of your help.
I'm coding a Module where i compute my personal restock orders. Everything works fine so far but when i wanna update the wholesale price of the Product via

                $product = new Product($productid); // Load the product
                $product->wholesale_price = (float) $wholesale; // Set the new wholesale price
                $product->update();

it kinda overrides it with the old wholesale price instantly because if i use exit; or die(); after it it works just fine but if not the old Wholesaleprice is still active and in the db.
If you have any clue or tip why this happens, please help me out here. I'm desperate!
Leo

Link to comment
Share on other sites

Hello leoCodesAShop,

have you considered using a SQL request for this task?

$wholesale = (float) $wholesale;
$productid = (int) $productid;

$sqlProduct = 'UPDATE ' . _DB_PREFIX_ . 'product 
               SET wholesale_price = ' . $wholesale . ' 
               WHERE id_product = ' . $productid;
Db::getInstance()->execute($sqlProduct);

$sqlProductShop = 'UPDATE ' . _DB_PREFIX_ . 'product_shop 
                   SET wholesale_price = ' . $wholesale . ' 
                   WHERE id_product = ' . $productid;
Db::getInstance()->execute($sqlProductShop);

best regards,

Link to comment
Share on other sites

Hello baba code,
thank you so much for the reply.
I did try it with the SQL request before, just to be sure i tried it again but it only works if I put a exit; or die(); behind it so the problem persists.
I feel like i tried almost everything but cant seem to find whats causing the problem.
But thank you for at least trying. If you have any ideas i would be grateful if you could share them with me.
Leo

Link to comment
Share on other sites

It's like a Form for a custom Product Tab, so the code is full of static shit and to large but i can show you the structure im working with
 

    public function buildForm(FormBuilderInterface $builder, array $options)

    {

        parent::buildForm($builder, $options);

        $productid = $options['productid']; //get product id from options

        $employee_id = $options['employee_id']; //get product id from options


        //settings Row

        $builder->add('row_set', FormType::class, [

                'label' => 'Produkt Einstellungen',

                'label_tag_name' => 'h2',

                'required' => false,

                'attr' => ['class' => 'row', 'style' => 'margin: 1px;'],

            ]);

// some more rows ...


//building the rows
        $builder->get('row_set')

            ->add('test', TextType::class, [

                'label' => 'test:',

                'label_attr' => ['style' => 'display: block; margin-bottom: 5px;'],

                'label_tag_name' => 'h3',

                'required' => false,

                'attr' => ['style' => 'width: 200px; margin-right: 10px;','disabled' => 'true']

            ])

// some more fields in the row...

        // eventlistener for submit 

        $builder->addEventListener(FormEvents::POST_SUBMIT, function (PostSubmitEvent $event) use ($productid, $employee_id) {

            $form = $event->getForm();


##################    EINKAUFSHISTORIE     ############################

            // Get the data from the forms
            $pieces = $form->get('row_inv_in')->get('pieces')->getData();
//some more ...

            // Check if everythings there
            if (($productinvoices) && !$pieces) {
                // Add a validation error to the 'pieces' field
                $form->get('row_inv_in')->get('pieces')->addError(new FormError('Fehlende Stückzahl.'));
            }elseif (($pieces) && !$productinvoices) {
                // Add a validation error to the 'productinvoices' field
                $form->get('row_inv_in')->get('productinvoices')->addError(new FormError('Fehlende Summe.'));
            }
//handle submit
            elseif ($epochtime instanceof \DateTimeInterface && $pieces && $productinvoices) {

                $epochtime = $epochtime->getTimestamp();
                $wholesale = $productinvoices / $pieces;
                $insertData = [
                    'id_product' => pSQL($productid),
                    'epochtime' => $epochtime,
//...
                ];

                Db::getInstance()->insert('custom_table', $insertData);

// my version to handle shop wholesale
                // $product = new Product($productid); // Load the product
                // $product->wholesale_price = (float) $wholesale; // Set the new wholesale price
                // $product->update();
				//die($wholesale);

               
                $sqlProduct = 'UPDATE ' . _DB_PREFIX_ . 'product
                            SET wholesale_price = ' . $wholesale . '
                            WHERE id_product = ' . $productid;
                Db::getInstance()->execute($sqlProduct);

                $sqlProductShop = 'UPDATE ' . _DB_PREFIX_ . 'product_shop
                                SET wholesale_price = ' . $wholesale . '
                                WHERE id_product = ' . $productid;
                Db::getInstance()->execute($sqlProductShop);

                //die($wholesale);

            }

i cut out some part to make the core code more understandable.
I'm pretty sure the code is fine and it just gets overwritten from a random hook which gets activated but i cant seem to find it.
exit; is the only solution which kinda worked but not in a satisfying way.

Link to comment
Share on other sites

 

Should help

https://www.h-hennes.fr/blog/2019/08/05/prestashop-1-7-ajouter-des-champs-dans-un-formulaire-dadministration/

Try to use hook

 public function hookActionAfterCreateProductFormHandler(array $params)
    {
        $this->updateData($params['id'],$params['form_data']);
    }
 
   
    public function hookActionAfterUpdateProductFormHandler(array $params)
    {
        $this->updateData($params['id'],$params['form_data']);
    }
  • Thanks 1
Link to comment
Share on other sites

Hey again,
thank you for the hint. Even tho it didn't work instantly like in the tutorial it lead me on the right track.
i basically i simply used this hook and then got my custom field over params (had to find it first but in there is basically all the information ever xD) and then just updated it like i was doing before.
 

    public function hookActionAfterUpdateProductFormHandler(array $params)
{
        $productid = $params['id'];
        $formData = $params['form_data'];
        $wholesale = $formData['custom_tab']['row_inv_in']['wholesale'];

        if(isset($wholesale)){
            $product = new Product($productid); // Load the product
            $product->wholesale_price = (float) $wholesale; // Set the new wholesale price
            $product->update();
        }
}

So everything works now finally and I'm soo thankful for taking the time out of your life to help me find a 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...