xertion Posted October 15, 2015 Share Posted October 15, 2015 I'm building a Payment-module to Prestashop and I'm having issues with the validateOrder() function which creates the order and saves it to the database actually creates 2 orders with the same reference, id_cart and so on. It also sends 2 emails and so on. I use $cart->OrderExists() to check if order already exists before I call validateOrder(). I also made a debug and followed the messages in ps_log table and found the following. 2015-10-15 12:07:11 PaymentModule::validateOrder - Function called 2015-10-15 12:07:11 PaymentModule::validateOrder - Order is about to be added [1] 2015-10-15 12:07:11 PaymentModule::validateOrder - OrderDetail is about to be added [1] 2015-10-15 12:07:11 PaymentModule::validateOrder - OrderCarrier is about to be added [1] 2015-10-15 12:07:11 PaymentModule::validateOrder - Order is about to be added [2] 2015-10-15 12:07:11 PaymentModule::validateOrder - OrderDetail is about to be added [2] 2015-10-15 12:07:11 PaymentModule::validateOrder - OrderCarrier is about to be added [2] 2015-10-15 12:07:11 PaymentModule::validateOrder - Payment is about to be added 2015-10-15 12:07:11 PaymentModule::validateOrder - Message is about to be added [1] 2015-10-15 12:07:11 PaymentModule::validateOrder - Hook validateOrder is about to be called [1] 2015-10-15 12:07:13 PaymentModule::validateOrder - Order Status is about to be added [1] 2015-10-15 12:07:15 PaymentModule::validateOrder - Mail is about to be sent [1] 2015-10-15 12:07:16 PaymentModule::validateOrder - Message is about to be added [2] 2015-10-15 12:07:16 PaymentModule::validateOrder - Hook validateOrder is about to be called [2] 2015-10-15 12:07:18 PaymentModule::validateOrder - Order Status is about to be added [2] 2015-10-15 12:07:19 PaymentModule::validateOrder - Mail is about to be sent [2] 2015-10-15 12:07:19 PaymentModule::validateOrder - End of validateOrder As you can see. validateOrder() is only being called once, which remove the idea of it's me that accidently calls the function twice. You also see it only finishes once. But inbetween almost all functions are called twice... My complete controller that handles the payment can be found here: https://dpaste.de/EMYm I call "validateOrder" at line 425. Anyone have any ideas of why validateOrder() would only be called once, but do all its stuff twice? Im running on 1.6.0.9 Link to comment Share on other sites More sharing options...
xertion Posted October 15, 2015 Author Share Posted October 15, 2015 So after additinal debugging it seems like this have something to do with $this->context->cart->getPackageList(); and PaymentModule.php:252 foreach ($package_list as $id_address => $packageByAddress). It seems like Prestashop is intentionally creating 2 orders. On the bottom of the first order it says "LINKED ORDERS" and it displays a link to the second order. However that order contains no products or items. This makes me believe that Prestashop for some reason think that I want to split the order into 2 packages/addresses (back to getPackageList()) even though I only add 1 product in the cart and in the order. Ideas? Link to comment Share on other sites More sharing options...
xertion Posted October 16, 2015 Author Share Posted October 16, 2015 I did more testing and debugging. There is a foreach loop in PaymentModule.php:232 that adds id_warehouse and id_carrier to $package_list. If I do a print_r($package_list); before, and after the foreach loop. I can see that the loop has added a second item in the $package_list array with key 0. The item is empty except for id_warehouse and id_carrier keys. See complete array ($package_list) below. This seems to be the reason why it is creating 2 orders/packages since later in the PaymentModule there is a foreach loop that adds an order for each item in the $package_list array... Help? Ideas? Array ( [84842] => Array ( [0] => Array ( [product_list] => Array ( [0] => Array ( [id_product_attribute] => 0 [id_product] => 5037 [cart_quantity] => 1 [id_shop] => 1 [name] => Cellofanpåsar Spooky Boots [is_virtual] => 0 [description_short] => <p>20 st snygga cellofanpåsar för Halloweenfestens godis och fiskdamm</p> [available_now] => [available_later] => [id_category_default] => 166 [id_supplier] => 0 [id_manufacturer] => 0 [on_sale] => 0 [ecotax] => 0.000000 [additional_shipping_cost] => 0.00 [available_for_order] => 1 [price] => 23.2 [active] => 1 [unity] => [unit_price_ratio] => 0.000000 [quantity_available] => 2 [width] => 0.000000 [height] => 0.000000 [depth] => 0.000000 [out_of_stock] => 2 [weight] => 0.000000 [date_add] => 2013-08-23 11:10:42 [date_upd] => 2015-05-06 13:02:09 [quantity] => 1 [link_rewrite] => cellofanpasar-spooky-boots [category] => cellobags [unique_id] => 000000503700000000000 [id_address_delivery] => 84842 [advanced_stock_management] => 0 [supplier_reference] => [reduction_type] => 0 [id_customization] => [customization_quantity] => [price_attribute] => [ecotax_attr] => [reference] => 44259 [weight_attribute] => [ean13] => 11179442591 [upc] => [pai_id_image] => [pai_legend] => [minimal_quantity] => 1 [wholesale_price] => 5.880000 [stock_quantity] => 2 [total] => 23.2 [total_wt] => 29 [price_wt] => 29 [id_image] => 5037-6254 [legend] => [reduction_applies] => [quantity_discount_applies] => [allow_oosp] => 0 [features] => Array ( ) [rate] => 25 [tax_name] => Moms SE 25% [warehouse_list] => Array ( [0] => ) [in_stock] => 1 [carrier_list] => Array ( [0] => 71 ) ) ) [carrier_list] => Array ( [0] => 71 ) [warehouse_list] => Array ( [0] => ) [id_warehouse] => 0 ) ) [0] => Array ( [0] => Array ( [id_warehouse] => 0 [id_carrier] => 71 ) ) ) Link to comment Share on other sites More sharing options...
innovacy Posted October 16, 2015 Share Posted October 16, 2015 (edited) This is a behaviour as expected (yes, really!). Let me explain. Prestashop will create multiple orders for one validated cart under some circumstances. The most common is, when you have products in the cart that are in stock and some that are out of stock. After all, you need to be able to track the status of each separate, as not everything will be send out at once. Another is, if your order seems to require more than one package to be delivered. Another reason can be different warehouses and/or carriers, as such indicates they might be managed separate for shipping. Than as last possibility there could be additional modules, that force the creation of separate orders (I have coded such one where customers should be able to order products for delivery on different dates). As you already discovered, these processes happen through the package list. You need to check if you have one of these cases happening here, the most common is really then in/out-of-stock case for splitted orders. You shouldn't change how this works, it is intended to work this way and probably isn't an issue with your payment module (I didn't check your code though). Edited October 16, 2015 by innovacy (see edit history) Link to comment Share on other sites More sharing options...
xertion Posted October 16, 2015 Author Share Posted October 16, 2015 This is a behaviour as expected (yes, really!). Let me explain. Prestashop will create multiple orders for one validated cart under some circumstances. The most common is, when you have products in the cart that are in stock and some that are out of stock. After all, you need to be able to track the status of each separate, as not everything will be send out at once. Another is, if your order seems to require more than one package to be delivered. Another reason can be different warehouses and/or carriers, as such indicates they might be managed separate for shipping. Than as last possibility there could be additional modules, that force the creation of separate orders (I have coded such one where customers should be able to order products for delivery on different dates). As you already discovered, these processes happen through the package list. You need to check if you have one of these cases happening here, the most common is really then in/out-of-stock case for splitted orders. You shouldn't change how this works, it is intended to work this way and probably isn't an issue with your payment module (I didn't check your code though). Thanks for responding. Well since I'm only adding ONE product to the cart, I don't understand how it could be any of those issues. It can't be that it requires more than 1 package, it can't be that it's out of stock (I already check, but also if its only a single product it doesnt make sense to send a package and wait with one), and it cant be different warehouses or carriers for different products, because like I said, it's only 1 product in my testing. I did do some more testing and it seems like $package_list is suppose to contain the id_address_delivery as the key of each item. Example $package_list = Array([78125] => Array());. In my case, the key of the items in $package_list is 0. Which means, it think that the id_address_delivery is 0. Even though I already added $id_address_delivery to the $cart object. Also, $delivery_option_list ($cart->getDeliveryOptionList()) contains an array of each item in the cart. Here I can see that $id_address_delivery is set to 0. This happens even though I set $this->context->cart->id_address_delivery to my address and called .save(); afterwards to update the entry in the database. (Check my link to my code in first post to see how I add id_address_delivery). The result of $id_address_delivery being set to 0 on the products, is that later in the foreach-loop that I mentioned in previous post, it adds id_carrier and id_warehouse to key 0. Which creates a new key in the array (which results in the order running twice, since it now has 2 keys. One with key 0, one with the real id_address_delivery as key). Link to comment Share on other sites More sharing options...
innovacy Posted October 16, 2015 Share Posted October 16, 2015 Ok, if it's just one product and you receive 2 orders, that makes absolute no sense. Are the orders exactly the same or are there differences? Why are you adding $id_address_delivery to the $cart object? And why do you call save()? The $cart object is handled by the checkout already and a payment module usually won't need to touch it. Are you duplicating here maybe things that the shop is doing in it's controllers? What's the purpose of it in your module? Link to comment Share on other sites More sharing options...
eriknuds Posted November 11, 2015 Share Posted November 11, 2015 I think you may be experiencing the same issue I did, read my post here: https://www.prestashop.com/forums/topic/293374-duplicate-shopping-cart-and-orders/?p=2171973 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