Prestashop actionUpdateQuantity loop - prestashop

I'm developing a simple module that hooks to actionUpdateQuantity hook. So, every time the stock of a product is updated I must update the stock of other products.
But, to update the stock I call stockAvailable object, which trigger the actionUpdateQuantity hook. So, I have a endless loop.
Then I tried to manually update the stock directly on the database using SQL, but this have the problem that other modules don't "see" the stock updates. So, modules like MailAlert, ebay or Amazon don't update stock correctly.
I'm a bit stuck here.
How can I update the stock without enter a loop ?
Thanks!

I had similar issue before and think this is not best way but worked for me. Idea is to add class variable in your module:
protected $isSaved = false;
then in hookActionProductUpdate function first check that variable and later after you done saving data change its value
public function hookActionProductUpdate($params)
{
if ($this->isSaved)
return null;
...
$this->isSaved = true;
}

Another way to do this is, in your module when you submit new quantity make sure you also submit product id and attribute id. Then in your hook you can do a check.
public function hookActionUpdateQuantity($params)
{
if ((int)Tools::getValue('id_product') != $params['id_product']
|| (int)Tools::getValue('id_attribute') != $params['id_product_attribute']) {
return false;
}
// do your stuff
}
Everytime the hook actionUpdateQuantity triggers you have a $params array of product whose quantity is being updated.
$params['id_product'] // id of a product being updated
$params['id_product_attribute'] // id of product combination being updated
$params['quantity'] // quantity being set to product
This way your hook will run only once when you are updating quantity of a product from your module (form?). As you update other products quantity they will also trigger this hook but since the data in $params array is different than your POST'ed data, the hook method will return false.

Related

Prestashop keep cart after payment error

I would like to keep every products in my cart even if there is a payment error, I found that when the order is inserted in the database, the current cart session is "deleted" and a new one is created.
Is there a way to keep my cart session ? Or where is this happening ?
Finally found a way after 3 days :
In FrontController
if ($cart->OrderExists())
{
unset($this->context->cookie->id_cart, $cart, $this->context->cookie->checkedTOS);
$this->context->cookie->check_cgv = false;
}
This sample of code is deleting the cart session, I just need to remove this
$this->context->cookie->id_cart, $cart

how to update prestashop product details from external database?

i have one grocery shop and i have thinking to sell this product online .so i used one CMS cart called "prestashop" .
even i have installed and start working on that .but i have some problem.
i have my own inventory database to store all product details . with a UI that i can add product details to database (quantity, price etc) . now my question is how can interact with my inventory database using prestashop cart ?
is it possible ? what is the best way to do it ? please help
You can add the categories from external db using a script in your prestashop site. For this create a module with frontcontroller (for cron job or to run the script) and inside the script select the categories from external db, then loop through the categories and inside the loop create prestashop category object and place the category fields to the category object and call the category save method.
public function initContent()
{
parent::initContent();
$temp_categs = array(); //
//fetch data into $temp_catgs from the external db with the help mysqli_connect or whatever the driver - never modify presta db settings - write php code for db connection to external db and fetch
//loop through the fetched categories
foreach($temp_categs as $categ){
$new_categ = new Category();
//set all the field values
$new_categ->name = $categ['name'];
$new_categ->id_parent = $categ['id_parent'];
$new_categ->active = true;
$new_categ->field2 = $categ['field2'];
$new_categ->field3 = $categ['field3'];
//save the category - a category with new id will be created in prestashop
$new_categ->save();
}
}
}
I hope this would help you.

How to create a new product from a custom form and then add it to the cart?

I am trying to develop a custom page on PS 1.6 where a customer could create a new product from a form and then add it to the cart.
Let's say for example, i am selling woodcrafts and i want my customers to fill a form where they need to specify the type of wood, dimensions, ...
Depending on these criterias, the price would be modified and it will create a "final" product that will be added to the customer's cart.
I know how i will develop the form and i believe i can add the product to the cart with updateQty() from Cart.php but how do i instanciate my product from the data i get from the form? I am trying to search through all files but i can't seem to find where new products are instanciated from.
Thanks in advance for the help
I'm answering my question since i managed to do it. Here's my solution :
public static function créerProduct($name, $ean13, $category, $price, $description, $reference){
$product = new Product();
$languages=Language::getLanguages();
foreach($languages as $lang){
$product->name[$lang['id_lang']]=$name;
$product->link_rewrite[$lang['id_lang']]=$name;
$product->description[$lang['id_lang']]=$description;
}
$product->reference=$reference;
$product->quantity=0;
$product->id_category_default=$category;
$product->id_category[]=$product->id_category_default;
$product->price=$price;
$product->id_tax_rules_group=1;
$product->indexed=0;
try{
$product->save();
} catch (PrestaShopException $e){
echo $e->displayMessage();
}
$product->updateCategories(array_map('intval', $product->id_category));
StockAvailable::setQuantity($product->id,'',1);
return $product->id;
}
public static function addProduitauPanier($id_product){
$context=Context::getContext();
$result=$context->cart->updateQty(1,$id_product);
}
Can you not use attributes to develop the product? The reason being is that you are going to have in effect customers adding information to your database and then you are going to have to sanitize it and validate it. I would use Prestashop's built in attributes for doing something like this.

How to show product quantity in the cart on custom page

I'm using Prestashop 1.5 and created page with list of grouped products. I want to show quantity of each product in the cart. At shopping cart page exist $product.cart_quantity property, but on my page it doesn't. Please, explain me, how to show product quantity in the cart on my page.
Easyest way I can think of is to access the data via cookies since cart data is stored in them.
You can get cookies data like this:
$context = Context::getContext();
echo '<pre>',print_r($context->cookie, true).'</pre>';
Prestashop Context is a registry for PHP variables that were previously accessed as globals. It aims to standardize the way these variables are accessed, and to make the code more robust by getting rid of global vars.
And our echo is just for example to show what info you can get from cookies.
When costumer adds something to the shopping-cart it automatically gives it a cart id (id_cart) and from there it's fairly easy to access to access that value to get all the info.
To get a cart id ( assuming you already got context ) use this
$Cart = $context->cart;
This returns you a ID of a current cart.
Now you want to return the current products in the cart ( with all the information it includes ). For that you have to use the public function located in prestashop_main_folder/classes/cart.php
So to return all the current products just use the following line
$Cart->getProducts($refresh = false, $id_product = false, $id_country = null)
And then it returns you a array with all the variables what you can easily then access.
BR's
You can do the following:
$context=Context::getContext();
$id_cart=$context->cookie->id_cart;
if($id_cart=='') $id_cart=Tools::getValue('id_cart');
$theCart = new Cart($id_cart);
$products = $theCart->getProducts(true);
$nbTotalProducts = 0;
foreach ($products as $product)
{
$nbTotalProducts += (int)$product['cart_quantity'];
}

Prestashop Module: hooks and orders

I am having multiple questions about this topic
As the title states, I am in need to find the correct hook to bind when an order has been placed and the payment was accepted.
1.) Which hook should I bind in my module when an order has been placed (and payed)?
2.) I am under the impression that there is no generalized hook for this, since some payment methods set the order status to 'payed' automatically (like a successful PayPal transaction) while other methodes require the shopowner to manually set the status to 'payed'. Are there any more than just those two that must be called to cover most cases?
3.) Eventhough I am still hoping that there is a generalized hook, if there's none, how would I approach this issue? Bind "actionPaymentConfirmation" aswell as "displayPaymentReturn" to cover both cases?
4.) Why is the hook "actionPaymentConfirmation" never called when I set the order status to "payed" in the backoffice. My code looks like this
public function install() {
if (!parent::install() || !$this->registerHook("actionPaymentConfirmation")) {
return false;
}
return true;
}
public function actionPaymentConfirmation($params) {
print_r($params); // stepping through with XDebug but the function is never being invoked
}
5.) Does anyone know a free module doing something simmilar I can dig into to get a better idea?
6.) Or might it be easier to override Prestashops core classes to tackle my problems? To break it down, I want to execute stuff after an order has been placed and the status is set to payment was accepted or remotely accepted.
Well, I hope I am not asking to much stuff at the same time, but as you can see, I am interested in mastering these things but have some troubles along the way. Have now been trying and especially search for answers for days now without any luck.
Regards!
I assume that you're with PrestaShop 1.5
1 actionValidateOrder (for new order) & actionOrderStatusPostUpdate (here you can check about the "paid" status)
2 Like 1.
3 Like 1.
4 The hook is actionOrderStatusPostUpdate
5
public function install()
{
return (parent::install()
AND $this->registerHook('newOrder')
AND $this->registerHook('actionOrderStatusPostUpdate'));
}
public function hookNewOrder($params)
{
return $this->hookActionOrderStatusPostUpdate($params);
}
public function hookActionOrderStatusPostUpdate($params)
{
//$params['newOrderStatus'] // after status changed
//$params['orderStatus'] // after order is placed
}
6 Look at 5.
Note: actionValidateOrder the new name (alias) of newOrder