Jump to content

How do I get every product combinations' ID when I create or update a product?


aleaallee

Recommended Posts

Hi, I'm not a prestashop developer but have been tasked with adding a new functionality to one of my employer's client prestashop websites, I want to get the reference from both the product and all of its possible (only if it has) combinations and then do something with them after I update or create a product. I made a module and implemented the actionProductUpdate, actionProductAttributeUpdate and actionProductAdd hooks with the following code while trying to get a product and its combinations/attributes' refferences:

 

public function hookActionProductUpdate($params)
    {
        $product_ref = $params["product"]->reference;
        $prod = $this->getProducts($product_ref);
        print_r($params["product"]);
    }

All I've managed to do is to show the print_r's content in the devtools' network tab.

Edited by aleaallee (see edit history)
Link to comment
Share on other sites

what do you want to achieve? where do you want the value to show?

If , say , hypothetically made a silly decision that you want the information to be shown on BO header (which is less likely),
then implement this module below and show the values
https://devdocs.prestashop-project.org/8/modules/concepts/hooks/list-of-hooks/displayadminafterheader/

If you want to show it in BO Product Edit page, find a hook near where you want it. Usually the hook name has 'display' -> hookDisplay....
When there is none, create your own hook (might need to modify non-module files). Kinda similar to show on storefront

 

Link to comment
Share on other sites

Hi.
It is always necessary to give as much information as possible in such a way that it makes sense.

As it was written, we don't know what the output should be, what all you need, etc.

This is how we give you the code and you immediately ask how to build your next function.

Module hook sample:

    public function hookActionObjectProductUpdateAfter($params)
    {
        $idProduct = $params['object']->id;
        $date = date('Y-m-d');
        $product = new Product($idProduct);
        $db = Db::getInstance();

        $references = array();
        $idLang = $this->context->language->id;
        $references[] = 'default product reference: '.$product->reference;

        if ($product->hasAttributes()){
            $getProductAttributes = $db->executeS('SELECT id_product_attribute, reference FROM '._DB_PREFIX_.'product_attribute WHERE id_product = '.$idProduct);
            foreach ($getProductAttributes as $attribute){
                if ($attribute['reference']){
                    $combination = new Combination($attribute['id_product_attribute']);
                    $getAttributesName = $combination->getAttributesName($idLang);
                    $combinationNames = array();
                    foreach ($getAttributesName as $attributeName){
                        $combinationNames[] = $attributeName['name'];
                    }
                    $references[] = implode(' / ', $combinationNames).': '. $attribute['reference'];
                }
            }
        }

        if ($references){
            file_put_contents(_PS_MODULE_DIR_.$this->name.'/log.txt',implode("\n", $references));
        }
    }

 

Link to comment
Share on other sites

On 1/27/2023 at 8:25 PM, s4lvozesta said:

what do you want to achieve? where do you want the value to show?

If , say , hypothetically made a silly decision that you want the information to be shown on BO header (which is less likely),
then implement this module below and show the values
https://devdocs.prestashop-project.org/8/modules/concepts/hooks/list-of-hooks/displayadminafterheader/

If you want to show it in BO Product Edit page, find a hook near where you want it. Usually the hook name has 'display' -> hookDisplay....
When there is none, create your own hook (might need to modify non-module files). Kinda similar to show on storefront

 

I want to get the product and its possible combinations references so I can send them to another API and do stuff inside that API.

Link to comment
Share on other sites

On 1/28/2023 at 7:37 AM, ps8moduly.cz said:

Hi.
It is always necessary to give as much information as possible in such a way that it makes sense.

As it was written, we don't know what the output should be, what all you need, etc.

This is how we give you the code and you immediately ask how to build your next function.

Module hook sample:

    public function hookActionObjectProductUpdateAfter($params)
    {
        $idProduct = $params['object']->id;
        $date = date('Y-m-d');
        $product = new Product($idProduct);
        $db = Db::getInstance();

        $references = array();
        $idLang = $this->context->language->id;
        $references[] = 'default product reference: '.$product->reference;

        if ($product->hasAttributes()){
            $getProductAttributes = $db->executeS('SELECT id_product_attribute, reference FROM '._DB_PREFIX_.'product_attribute WHERE id_product = '.$idProduct);
            foreach ($getProductAttributes as $attribute){
                if ($attribute['reference']){
                    $combination = new Combination($attribute['id_product_attribute']);
                    $getAttributesName = $combination->getAttributesName($idLang);
                    $combinationNames = array();
                    foreach ($getAttributesName as $attributeName){
                        $combinationNames[] = $attributeName['name'];
                    }
                    $references[] = implode(' / ', $combinationNames).': '. $attribute['reference'];
                }
            }
        }

        if ($references){
            file_put_contents(_PS_MODULE_DIR_.$this->name.'/log.txt',implode("\n", $references));
        }
    }

 

Thanks, I only want to output the product and its possible attribute references so I know it has them so I can then send them to an external API and do something with these products there.

Link to comment
Share on other sites

On 1/26/2023 at 8:36 PM, aleaallee said:

I made a module and implemented the actionProductUpdate, actionProductAttributeUpdate and actionProductAdd hooks with the following code

You have successfully done the above, so ...

 

3 hours ago, aleaallee said:

I want to get the product and its possible combinations references so I can send them to another API and do stuff inside that API.

... all you need to do is to call the API inside the hooks. For example

$curl = curl_init();
   switch ($method){
      case "POST":
         curl_setopt($curl, CURLOPT_POST, 1);
         if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         break;
      case "PUT":
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
         if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);			 					
         break;
      default:
         if ($data)
            $url = sprintf("%s?%s", $url, http_build_query($data));
   }
   // OPTIONS:
   curl_setopt($curl, CURLOPT_URL, $url);
   curl_setopt($curl, CURLOPT_HTTPHEADER, array(
      'APIKEY: 111111111111111111111',
      'Content-Type: application/json',
   ));
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
   // EXECUTE:
   $result = curl_exec($curl);
   if(!$result){die("Connection Failure");}
   curl_close($curl);
   return $result;

 

Cheers!

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...