Jump to content

Edit History

Simao Henriques

Simao Henriques


solved

Hi there,
Does anyone know what is the way to change a product carrier programmatically ? Here is what I tried, with no avail:
- tried using $product->setCarriers(), but it didn't seem to work, I am unsure it even exists on PS8. (maybe someone can confirm)

- tried directly doing a sql querry to ps_product_carrier, but not only did I have some trouble making it work, there was also the following problem: even if it is successful (ex: inserting manualy trough database manager or sql), when I oped that same product in edit mode, the entry I just added gets deleted (so apparently that table is being managed somewhere).

And I couldn't find the solution through searching online.

 

Any help would be apreciated, thanks.

Simão Henriques

[SOLVED]

So the problem I was having is that I was using the hook actionProductSave but that was triggering before the save and so it would get overwritten by prestashop saving process. I had to change to actionAdminProductsControllerSaveAfter so that it would trigger after the prestashop modifications.
Here is how to change carriers programatically if anyone is looking for it: (just an example bellow, not a functioning module/code)
 

<?php
class changeCarriersModule extends Module
{	
 	 // Make sure to install the correct hook
	public function install() 
    {
		// ...

        return parent::install() &&
			// ...
            $this->registerHook('actionAdminProductsControllerSaveAfter');
    }
  	
  	 public function hookActionAdminProductsControllerSaveAfter($params)
     {
        $idProduct = $params['return']->id;
        if (!$idProduct) { 
            return;
        }

        $product = new Product($idProduct);
        if (!Validate::isLoadedObject($product)) {
            return;
        }
	
        $carrierIdsArray = [2, 3, 5]; //(just an example) You can either initiate this array hardcoded or get it 
       								  // from a configuration field that you may add
		
       // Build the array with references instead of Ids
        $carrierReferenceIdsArray = [];
        foreach ($carrierIdsArray as $carrierId) {
            $carrier = new Carrier((int)$carrierId);
            if (Validate::isLoadedObject($carrier)) {
                $carrierReferenceIdsArray[] = (int)$carrier->id_reference;
            }
        }
		
        // Finally, use setCarriers() (from product.php) to set carriers
		// Make sure to pass the REFERENCE array and not the Id one
        $product->setCarriers($carrierReferenceIdsArray);
    }

 

Simao Henriques

Simao Henriques

Hi there,
Does anyone know what is the way to change a product carrier programmatically ? Here is what I tried, with no avail:
- tried using $product->setCarriers(), but it didn't seem to work, I am unsure it even exists on PS8. (maybe someone can confirm)

- tried directly doing a sql querry to ps_product_carrier, but not only did I have some trouble making it work, there was also the following problem: even if it is successful (ex: inserting manualy trough database manager or sql), when I oped that same product in edit mode, the entry I just added gets deleted (so apparently that table is being managed somewhere).

And I couldn't find the solution through searching online.

 

Any help would be apreciated, thanks.

Simão Henriques

×
×
  • Create New...