Jump to content

Get product id in hookValidateOrder


Recommended Posts

Hello, I try to write my own module which get some values and insert them into my module database table. My problem is that I can't get a product id.

This is my hook which gets every other param and insert it into my table but product_id:

public function hookActionValidateOrder($params){

		$order = $params['order'];
		$order_id = (int)$order->id;
		$ref = $order->reference;
		$id_shop = (int)Context::getContext()->shop->id;
		$language = (int)Context::getContext()->language->id;
		$id_customer = (int)Context::getContext()->customer->id;
		$date_add = $order->date_add;
		
		$products = $params['cart']->getProducts(true);
		
		Db::getInstance()->insert('first_module', array(
		'id_shop'      => $id_shop
		));
		
		Db::getInstance()->insert('first_module_lang', array(
		'id_lang'      => $language,
		'reference' => $ref,
		'id_order' => $order_id,
		'id_customer' => $id_customer,
		'id_product' => $products['id_product'],
		'date_add' => $date_add
		));		
	}

When order is submitted it display this notice:

at ErrorHandler->handleError('8', 'Undefined index: id_product', 'C:\xampp\htdocs\presta\modules\ps_first_module\ps_first_module.php', '428', array('params' => array('cart' => object(Cart), 'order' => object(Order), 'customer' => object(Customer), 'currency' => object(Currency), 'orderStatus' => object(OrderState), 'cookie' => object(Cookie), 'altern' => '2'), 'order' => object(Order), 'order_id' => '67', 'ref' => 'GTTPTRGFZ', 'id_shop' => '1', 'language' => '1', 'id_customer' => '4', 'date_add' => '2018-04-24 15:09:13', 'products' => array(array('id_product_attribute' => '13', 'id_product' => '3', 'cart_quantity' => '1', 'id_shop' => '1', 'id_customization' => null, 'name' => 'The best is yet to come' Framed poster', 'is_virtual' => '0', 'description_short' => '<p><span style="font-size:10pt;font-family:Arial;font-weight:normal;font-style:normal;color:#000000;">Printed on rigid matt paper and smooth surface.</span></p>', 'available_now' => '', 'available_later' => '', 'id_category_default' => '9', 'id_supplier' => '0', 'id_manufacturer' => '2', 'manufacturer_name' => 'Graphic Corner', 'on_sale' => '0', 'ecotax' => '0.000000', 'additional_shipping_cost' => '0.00', 'available_for_order' => '1', 'show_price' => '1', 'price' => '29', 'active' => '1', 'unity' => '', 'unit_price_ratio' => '0.000000', 'quantity_available' => '870', 'width' => '0.000000', 'height' => '0.000000', 'depth' => '0.000000', 'out_of_stock' => '2', 'weight' => '0', 'available_date' => '0000-00-00', 'date_add' => '2018-03-09 12:28:00', 'date_upd' => '2018-03-09 12:28:00', 'quantity' => '1', 'link_rewrite' => 'the-best-is-yet-to-come-framed-poster', 'category' => 'art', 'unique_id' => '0000000003000000001370', 'id_address_delivery' => '7', 'advanced_stock_management' => '0', 'supplier_reference' => null, 'customization_quantity' => null, 'price_attribute' => '0.000000', 'ecotax_attr' => '0.000000', 'reference' => 'demo_6', 'weight_attribute' => '0', 'ean13' => '', 'isbn' => '', 'upc' => '', 'minimal_quantity' => '1', 'wholesale_price' => '0.000000', 'id_image' => '3-3', 'legend' => 'The best is yet to come' Framed poster', 'reduction_type' => '0', 'is_gift' => false, 'reduction' => '0', 'price_without_reduction' => '35.67', 'specific_prices' => array(), 'stock_quantity' => '870', 'price_with_reduction' => '35.67', 'price_with_reduction_without_tax' => '29', 'total' => '29', 'total_wt' => '35.67', 'price_wt' => '35.67', 'reduction_applies' => false, 'quantity_discount_applies' => false, 'allow_oosp' => '0', 'features' => array(), 'attributes' => 'Dimension : 40x60cm', 'attributes_small' => '40x60cm', 'rate' => '23', 'tax_name' => 'PTU PL 23%')))) in ps_first_module.php line 428

 

Link to comment
Share on other sites

instead of using the cart object, trying using the order object

$products = $params['cart']->getProducts(true);

 

$products = $order->getProducts();

foreach ($products as $product) {
    $id_product = $product['product_id'];
}

an order can have more than one product, so make sure you are iterating through the list of products returned.

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