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
Related
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.
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.
I would like to programmatically reorder specific orders by cronjob and send an order confirmation by mail. I have a simple php file in my root directory which is triggered by a cronjob every once in a while:
<?php
include_once 'app/Mage.php';
Mage::app();
//some existing order ids
$orderIds= array('911', '1106', '926');
foreach($orderIds as $orderId){
Mage::unregister('rule_data');
Mage::getModel('adminhtml/session_quote')
->clear();
/* #var Mage_Sales_Model_Order $order */
$order = Mage::getModel('sales/order')->load($orderId)
->setReordered(true);
/* #var Mage_Sales_Model_Quote $quote */
$quote = Mage::getModel('sales/quote')
->setStoreId($order->getStoreId())
->assignCustomer(Mage::getModel('customer/customer')->load($order->getCustomerId()))
->setUseOldShippingMethod(true);
/* #var Mage_Adminhtml_Model_Sales_Order_Create $model */
$model = Mage::getModel('adminhtml/sales_order_create')
->initFromOrder($order)
->setQuote($quote);
/* #var Mage_Sales_Model_Order $newOrder */
$newOrder = $model->createOrder();
$newOrder->setQuoteId($quote->getId())
->sendNewOrderEmail();
$model->getSession()
->clear();
}
It seems to work so far, new orders are placed and the emails are sent. But the problem I experienced is that the customer in the new orders is always the one from the first old order (in this case the order with the id 911). Of course, this also affects the order confirmation emails, so they're all sent to the same email address... Also, the order items seem to add up in the cart, so the last order which is reordered contains all the order items of the previous orders... What am I doing wrong?
I appreciate every help I can get! Thank you!
There are two problems:
You are calling Mage::getModel('adminhtml/session_quote')->clear(),
which gets a new instance of the session and tries to clear that.
But the Mage_Adminhtml_Model_Sales_Order_Create class gets the
session by using Mage::getSingleton('adminhtml/session_quote'),
which always gets the same instance from the Magento registry. So
you are trying to clear another session instance.
Even if you would try to clear the right session instance from the
Magento registry by using:
Mage::getSingleton('adminhtml/session_quote')->clear(), there would
still be a problem. As the Mage_Adminhtml_Model_Session_Quote class
doesn't define this method, it ultimately calls
Varien_Object->unsetData(), which only does this: $this->_data = array();
But the information in the Mage_Adminhtml_Model_Session_Quote class
is stored in the $_quote, $_customer, $_store and $_order properties,
so they don't actually get cleared, but persist.
I solved the problem by deleting the Mage_Adminhtml_Model_Session_Quote instance in Magento registry before each of the orders, so that it needs to create a new one for each order.
Just add this at the beginning of the loop:
Mage::unregister('_singleton/adminhtml/session_quote');
Cheers! ;)
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'];
}
I would like to create automatically a cart for a customer with a product. Then the customer can login on the site and validate the cart.
I saw how to create a cart in my module but i don't understand how to affect the cart to a customer?
Binding created cart to customer is very simple:
$cart = new Cart();
//Add products or what so ever
$cart->id_customer = $my_customer_id;
$cart->update();
or simply update your ps_cart database table and set proper id_customer value. You can create a module with this code in a function with hook transplanted to actionAuthentication, which is executed after customer successful login to your e-shop.