Jump to content

SOLVED I can't get the product reference in the products with combinations


Recommended Posts

hi how are things. I have a ps module (designed by me) to update the stock of the physical store every time an online sale is made. So what I do is search for the product reference in the database of the physical store to update the stock. It works fine for me for simple products, but for those with combinations it doesn't get me the reference. This is the hook code that is launched every time there is a sale in ps:
 

    public function hookActionPaymentConfirmation($params)
    {


        $db = \Db::getInstance();
        //$products = $params['cart']->getProducts(true);//en los nuevos ps ya no va y hay que hacerlo con las dos ordenes siguientes
        $order = new Order($params['id_order']);
    $products = $order->getCartProducts();
        foreach ($products as $product)
        {
            $id_product = $product['id_product'];
            $cantidad = $product['cart_quantity'];
            $referencia = $product['reference'];
            $referencia_a_mostrar = $product['reference_to_display'];
            $unidades = $db->getValue('SELECT unidades FROM productos WHERE codigo_de_barras = "'.$referencia.'"');
            $unidadesRestantes=$unidades-$cantidad;
            $db->Execute('UPDATE productos SET unidades="'.$unidadesRestantes.'" WHERE codigo_de_barras = "'.$referencia.'"');
            mail("[email protected]", "yay", $id_product." ".$referencia." ".$cantidad." ".$referencia_a_mostrar);
    }
        
    
    }

As you can see, I send an email to my account with the ID, the reference and the amount. Well, in the reference it does not print anything. How can I get the reference of a product with combinations? My version of prestashop is 1.7.8.6.

Best regards!

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

You should use the $product['product_attribute_id'] the select the combination like:

    $sql = 'select * from ' . _DB_PREFIX_ . 'product_attribute where id_product_attribute = ' . (string) $id_product_attribute . ';';
    $rec = DB::getInstance()->getRow($sql);
    $ref = $rec["reference"];

rg,

Leo

Link to comment
Share on other sites

14 minutes ago, dostoyevski said:

perfect, it worked for me. One more thing, is there a way to know programmatically if a product is a combination product? 

A lot of thanks

 

$product = new Product($id_product);
$has_combinations = $product->hasCombinations(); //returns bool true or false

 

Edit: Obviously if you're using that within your foreach loop you'll have to use a different variable name than "$product"

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

16 minutes ago, dostoyevski said:

perfect, it worked for me. One more thing, is there a way to know programmatically if a product is a combination product? 

A lot of thanks

Alternatively, In the order array, $product['product_attribute_id'] will not be set. So test for

if (isset($product['product_attribute_id']) && !is_null($product['product_attribute_id']) && ($product['product_attribute_id'] !== 0)) {
	// combination processing
} else {
	// simple product processing
}

Rg,

Same Leo as above...

Link to comment
Share on other sites

  • dostoyevski changed the title to SOLVED I can't get the product reference in the products with combinations

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