IdeaR Posted May 12, 2016 Share Posted May 12, 2016 (edited) Hi, i need update 'invoice_date' value in 'prefix_orders' table after create a new order. I need use 'date_add' value for this, but in this moment not exits this registry in the data base I create a module with this code: public function hookActionValidateOrder($params) { $order = $params['order']; $sql = "UPDATE "._DB_PREFIX_."orders SET invoice_date = '".$order->date_add."' WHERE id_order = ".(int)$order->id; if (!Db::getInstance()->execute($sql)) die('error'); } This dont run because not exits this register in data base, this create later. Somebody know how i can do it? Regards PD: Sorry for my bad english Edited May 12, 2016 by IdeaR (see edit history) Link to comment Share on other sites More sharing options...
IdeaR Posted May 12, 2016 Author Share Posted May 12, 2016 (edited) Did you use $this->registerHook('actionValidateOrder') somewhere in the module's code in order to register your module for the hook? Hi, thanks for answer, yes, i have too public function install() { if (Shop::isFeatureActive()) Shop::setContext(Shop::CONTEXT_ALL); if (!parent::install() || !$this->registerHook('actionValidateOrder')){ return false; } return true; } Hook tigger ok, problem is when hook run order isnt register in data base, this is later Edited May 12, 2016 by IdeaR (see edit history) Link to comment Share on other sites More sharing options...
IdeaR Posted May 12, 2016 Author Share Posted May 12, 2016 Oh, wait maybe I understood this incorrectly. So, the hook actually gets called, but the entry in the database does not exist, yet, at the time of running the hook? Is that what you mean? What does the Order object look like? Can you try the following code and show the output: public function hookActionValidateOrder($params) { $order = $params['order']; ddd($order); $sql = "UPDATE "._DB_PREFIX_."orders SET invoice_date = '".$order->date_add."' WHERE id_order = ".(int)$order->id; if (!Db::getInstance()->execute($sql)) die('error'); } This is true, thats correct Output for your code is: Order Object ( [id_address_delivery] => 1 [id_address_invoice] => 1 [id_shop_group] => 1 [id_shop] => 1 [id_cart] => 46 [id_currency] => 1 [id_lang] => 1 [id_customer] => 1 [id_carrier] => 19 [current_state] => [secure_key] => 1d3b144465dd6c28dffc95b6366a21d8 [payment] => Transferencia bancaria [module] => bankwire [conversion_rate] => 1.000000 [recyclable] => 0 [gift] => 0 [gift_message] => [mobile_theme] => 0 [shipping_number] => [total_discounts] => 0 [total_discounts_tax_incl] => 0 [total_discounts_tax_excl] => 0 [total_paid] => 38.36 [total_paid_tax_incl] => 38.36 [total_paid_tax_excl] => 31.7 [total_paid_real] => 38.36 [total_products] => 31.7 [total_products_wt] => 38.36 [total_shipping] => 0 [total_shipping_tax_incl] => 0 [total_shipping_tax_excl] => 0 [carrier_tax_rate] => 0 [total_wrapping] => 0 [total_wrapping_tax_incl] => 0 [total_wrapping_tax_excl] => 0 [invoice_number] => [delivery_number] => [invoice_date] => 0000-00-00 00:00:00 [delivery_date] => 0000-00-00 00:00:00 [valid] => [date_add] => 2016-05-12 17:54:39 [date_upd] => 2016-05-12 17:54:40 [reference] => VCBCNTSZF [round_mode] => 2 [round_type] => 1 [webserviceParameters:protected] => Array ( [objectMethods] => Array ( [add] => addWs ) [objectNodeName] => order [objectsNodeName] => orders [fields] => Array ( [id_address_delivery] => Array ( [xlink_resource] => addresses ) [id_address_invoice] => Array ( [xlink_resource] => addresses ) [id_cart] => Array ( [xlink_resource] => carts ) [id_currency] => Array ( [xlink_resource] => currencies ) [id_lang] => Array ( [xlink_resource] => languages ) [id_customer] => Array ( [xlink_resource] => customers ) [id_carrier] => Array ( [xlink_resource] => carriers ) [current_state] => Array ( [xlink_resource] => order_states [setter] => setWsCurrentState ) [module] => Array ( [required] => 1 ) [invoice_number] => Array ( ) [invoice_date] => Array ( ) [delivery_number] => Array ( ) [delivery_date] => Array ( ) [valid] => Array ( ) [date_add] => Array ( ) [date_upd] => Array ( ) [shipping_number] => Array ( [getter] => getWsShippingNumber [setter] => setWsShippingNumber ) ) [associations] => Array ( [order_rows] => Array ( [resource] => order_row [setter] => [virtual_entity] => 1 [fields] => Array ( [id] => Array ( ) [product_id] => Array ( [required] => 1 ) [product_attribute_id] => Array ( [required] => 1 ) [product_quantity] => Array ( [required] => 1 ) [product_name] => Array ( [setter] => ) [product_reference] => Array ( [setter] => ) [product_ean13] => Array ( [setter] => ) [product_upc] => Array ( [setter] => ) [product_price] => Array ( [setter] => ) [unit_price_tax_incl] => Array ( [setter] => ) [unit_price_tax_excl] => Array ( [setter] => ) ) ) ) ) [_taxCalculationMethod:protected] => 0 [cacheCustomer:protected] => [id] => 25 [id_shop_list] => [get_shop_from_context:protected] => 1 [table:protected] => orders [identifier:protected] => id_order [fieldsRequired:protected] => Array ( [0] => id_address_delivery [1] => id_address_invoice [2] => id_cart [3] => id_currency [4] => id_lang [5] => id_customer [6] => id_carrier [7] => payment [8] => module [9] => total_paid [10] => total_paid_real [11] => total_products [12] => total_products_wt [13] => conversion_rate ) [fieldsSize:protected] => Array ( ) [fieldsValidate:protected] => Array ( [id_address_delivery] => isUnsignedId [id_address_invoice] => isUnsignedId [id_cart] => isUnsignedId [id_currency] => isUnsignedId [id_shop_group] => isUnsignedId [id_shop] => isUnsignedId [id_lang] => isUnsignedId [id_customer] => isUnsignedId [id_carrier] => isUnsignedId [current_state] => isUnsignedId [secure_key] => isMd5 [payment] => isGenericName [module] => isModuleName [recyclable] => isBool [gift] => isBool [gift_message] => isMessage [mobile_theme] => isBool [total_discounts] => isPrice [total_discounts_tax_incl] => isPrice [total_discounts_tax_excl] => isPrice [total_paid] => isPrice [total_paid_tax_incl] => isPrice [total_paid_tax_excl] => isPrice [total_paid_real] => isPrice [total_products] => isPrice [total_products_wt] => isPrice [total_shipping] => isPrice [total_shipping_tax_incl] => isPrice [total_shipping_tax_excl] => isPrice [carrier_tax_rate] => isFloat [total_wrapping] => isPrice [total_wrapping_tax_incl] => isPrice [total_wrapping_tax_excl] => isPrice [round_mode] => isUnsignedId [round_type] => isUnsignedId [shipping_number] => isTrackingNumber [conversion_rate] => isFloat [date_add] => isDate [date_upd] => isDate ) [fieldsRequiredLang:protected] => Array ( ) [fieldsSizeLang:protected] => Array ( ) [fieldsValidateLang:protected] => Array ( ) [tables:protected] => Array ( ) [image_dir:protected] => [image_format:protected] => jpg [def:protected] => Array ( [table] => orders [primary] => id_order [fields] => Array ( [id_address_delivery] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_address_invoice] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_cart] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_currency] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_shop_group] => Array ( [type] => 1 [validate] => isUnsignedId ) [id_shop] => Array ( [type] => 1 [validate] => isUnsignedId ) [id_lang] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_customer] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_carrier] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [current_state] => Array ( [type] => 1 [validate] => isUnsignedId ) [secure_key] => Array ( [type] => 3 [validate] => isMd5 ) [payment] => Array ( [type] => 3 [validate] => isGenericName [required] => 1 ) [module] => Array ( [type] => 3 [validate] => isModuleName [required] => 1 ) [recyclable] => Array ( [type] => 2 [validate] => isBool ) [gift] => Array ( [type] => 2 [validate] => isBool ) [gift_message] => Array ( [type] => 3 [validate] => isMessage ) [mobile_theme] => Array ( [type] => 2 [validate] => isBool ) [total_discounts] => Array ( [type] => 4 [validate] => isPrice ) [total_discounts_tax_incl] => Array ( [type] => 4 [validate] => isPrice ) [total_discounts_tax_excl] => Array ( [type] => 4 [validate] => isPrice ) [total_paid] => Array ( [type] => 4 [validate] => isPrice [required] => 1 ) [total_paid_tax_incl] => Array ( [type] => 4 [validate] => isPrice ) [total_paid_tax_excl] => Array ( [type] => 4 [validate] => isPrice ) [total_paid_real] => Array ( [type] => 4 [validate] => isPrice [required] => 1 ) [total_products] => Array ( [type] => 4 [validate] => isPrice [required] => 1 ) [total_products_wt] => Array ( [type] => 4 [validate] => isPrice [required] => 1 ) [total_shipping] => Array ( [type] => 4 [validate] => isPrice ) [total_shipping_tax_incl] => Array ( [type] => 4 [validate] => isPrice ) [total_shipping_tax_excl] => Array ( [type] => 4 [validate] => isPrice ) [carrier_tax_rate] => Array ( [type] => 4 [validate] => isFloat ) [total_wrapping] => Array ( [type] => 4 [validate] => isPrice ) [total_wrapping_tax_incl] => Array ( [type] => 4 [validate] => isPrice ) [total_wrapping_tax_excl] => Array ( [type] => 4 [validate] => isPrice ) [round_mode] => Array ( [type] => 1 [validate] => isUnsignedId ) [round_type] => Array ( [type] => 1 [validate] => isUnsignedId ) [shipping_number] => Array ( [type] => 3 [validate] => isTrackingNumber ) [conversion_rate] => Array ( [type] => 4 [validate] => isFloat [required] => 1 ) [invoice_number] => Array ( [type] => 1 ) [delivery_number] => Array ( [type] => 1 ) [invoice_date] => Array ( [type] => 5 ) [delivery_date] => Array ( [type] => 5 ) [valid] => Array ( [type] => 2 ) [reference] => Array ( [type] => 3 ) [date_add] => Array ( [type] => 5 [validate] => isDate ) [date_upd] => Array ( [type] => 5 [validate] => isDate ) ) [classname] => Order ) [update_fields:protected] => [force_id] => [product_list] => Array ( [0] => Array ( [id_product_attribute] => 0 [id_product] => 448 [cart_quantity] => 1 [id_shop] => 1 [name] => RED BULL 25 CL (PACK DE 24 LATAS) [is_virtual] => 0 [description_short] => <p>Red Bull te da alas. ¡Déjate llevar!</p> [available_now] => [available_later] => [id_category_default] => 51 [id_supplier] => 0 [id_manufacturer] => 36 [on_sale] => 0 [ecotax] => 0.000000 [additional_shipping_cost] => 0.00 [available_for_order] => 1 [price] => 31.7 [active] => 1 [unity] => [unit_price_ratio] => 0.000000 [quantity_available] => 290 [width] => 0.000000 [height] => 0.000000 [depth] => 0.000000 [out_of_stock] => 2 [weight] => 0.000000 [date_add] => 2014-10-03 15:06:16 [date_upd] => 2015-09-03 12:12:37 [quantity] => 1 [link_rewrite] => red-bull-enerdrlata-25cl [category] => energetica [unique_id] => 000000044800000000001 [id_address_delivery] => 1 [advanced_stock_management] => 0 [supplier_reference] => [customization_quantity] => [id_customization] => [reference] => 49000 [ean13] => 0 [upc] => [minimal_quantity] => 1 [wholesale_price] => 0.000000 [id_image] => 448-336 [legend] => RED BULL 25 CL [reduction_type] => 0 [stock_quantity] => 290 [price_without_reduction] => 38.357 [price_with_reduction] => 38.357 [price_with_reduction_without_tax] => 31.7 [total] => 31.7 [total_wt] => 38.36 [price_wt] => 38.357 [reduction_applies] => [quantity_discount_applies] => [allow_oosp] => 0 [features] => Array ( ) [rate] => 21 [tax_name] => IVA ES 21% [warehouse_list] => Array ( [0] => 0 ) [in_stock] => 1 [carrier_list] => Array ( [16] => 16 [19] => 19 [18] => 18 ) ) ) ) END Link to comment Share on other sites More sharing options...
IdeaR Posted May 12, 2016 Author Share Posted May 12, 2016 Does Validate::isLoadedObject($order) return false? i dont understand Link to comment Share on other sites More sharing options...
IdeaR Posted May 12, 2016 Author Share Posted May 12, 2016 could be displayOrderConfirmation, but i think that some orders are creates and this hook don run, Link to comment Share on other sites More sharing options...
IdeaR Posted May 12, 2016 Author Share Posted May 12, 2016 How about hooking onto actionOrderStatusPostUpdate? https://github.com/PrestaShop/PrestaShop/blob/1.6.1.x/classes/order/OrderHistory.php#L359 That is called right after the invoice has been generated (or any other status update -- you might have to filter the status type) and should allow you to correctly update invoice_date. You might also have to reload the Order object in your hook => $order = new Order((int)$order->id); In my shop invoices are disable, invoices never are created. I need this module for stadistics, this dont run because check 'invoice_date' and not 'add_date' for buys. More info here https://www.prestashop.com/forums/topic/367885-dashboard-stats-are-empty-after-upgrading-to-1609/page-2 Link to comment Share on other sites More sharing options...
IdeaR Posted May 13, 2016 Author Share Posted May 13, 2016 This seems to work: public function hookActionOrderStatusPostUpdate($params) { $order = new Order((int)$params['id_order']); if (Validate::isLoadedObject($order) && empty($order->invoice_date)) { $order->invoice_date = $order->date_add; $order->save(); } } Can you confirm? Thanks for answer, finally run with this code: public function hookActionOrderStatusPostUpdate($params) { $order = new Order((int)$params['id_order']); if (Validate::isLoadedObject($order) && strtotime($order->invoice_date) <= 0) { $order->invoice_date = $order->date_add; $order->save(); } } empty($order->invoice_date) always is false, $order->invoice_date is a string ('0000-00-00 00:00:00') For check if invoice_date is empty there are two options: $order->invoice_date === '0000-00-00 00:00:00' or strtotime($order->invoice_date) <= 0 Thank you very much for your help!! Regards Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now