how to update prestashop product details from external database? - prestashop

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.

Related

create new user account manually on prestashop

I am developing a module that get user data in a page , I wand to submit this data to prestashop database to create a new user account .
so how can I do this ?
how should I understand what data I should get from user Like email,phone number and etc . (I want to know this Shop get what data from user to sign them up).
thank you
You can use this piece of code to create a Customer.
$customer = new Customer();
$customer->firstname = 'John';
$customer->lastname = 'Doe';
$customer->email = 'johndoe#gmail.com';
$customer->passwd = md5(pSQL(_COOKIE_KEY_.'yoursecretpasswordhere'));
$customer->save();
Like Madhi said, you will get all information needed for the Customer in Customer.php
Cheers :)
All the things you need are defined in the "classes/Customer.php" file.
Each PrestShop class has one "public static $definition" and each field of the $definition that has the "'required' => true", is required

Prestashop actionUpdateQuantity loop

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.

How to create a order in Prestashop?

I'm creating a module to add orders from a API to prestashop.
I wonder though how to do it. I have been trying to create a cart and someway add products to it without any success. I want to create a order into prestashop that's already paid.
I haven't find a way to add a order or a product to the cart or to save it as paid.
I have done this so far with looking into the controllers prestashop have.
$context = Context::getContext();
foreach($ret->objects as $order) {
$context->cart->add();
$context->cookie->id_cart = (int)$context->cart->id;
// What to do now?
}
I don't know if this is to any help but i would love any point to correct direction.
To add an order entry into your database, you can initialize an order object as follows:
$order = new Order();
$order->id_shop = 1;
$order->id_cart = 12;
$order->id_customer = 23;
$order->payment = "COD";
$order->total_paid = 24500;
$order->add();
Don't forget to perform other related initializations, as well as execute hooks. You'll find pretty much everything you need reverse engineering PrestaShop's github.

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'];
}