PrestaShop add to cart from custom module - prestashop

I ma working on PrestaShop module, where the user can select some parameters on front view.
I need to add product to cart, including custom price, and selected parameters.
How should I do this? I don't want to override deafult behavior, as not all products will use my component.
Any help would be awesome.
Have a nice day, Bartek.

adding a product to a cart is pretty simple :
First create an empty cart :
$cart=new Cart();
... here you can set your cart properties id_address / id_carrier etc...
$cart->add();
Or, if you already have a cart to use just load the Cart object with its ID and perform :
$cart->updateQty($quantity, $id_product, $id_product_attribute, false);
$cart->update();
You should repeate the updateQty() method for each product / qties you'd need to add to the cart.
In order to customize product prices in cart you have to create product "specific prices" using the SpecificPrice object and bind them to that id_cart :
$mySpecificPrice = new SpecificPrice();
$mySpecificPrice->id_cart = $cart->id;
... add your discount / id_product here on specific price here ...
$mySpecificPrice->add();
Note that you can only set price discounts with SpecificPrice , surcharges are not allowed and will trigger an exeception.
If you need to increase your prices without any core modification, you'll forced to generate an Order and edit the order prices afterward.

Related

Odoo Decrease Product Quantity Programmatically

I have created a custom module where there is a field related to product. When the user checkout the product in the custom module, I want to decrease the quantity of product in the override create method. How to achieve this?
Finally, I found the solution code
warehouse = self.env['stock.warehouse'].search(
[('company_id', '=', self.env.company.id)], limit=1
)
self.env['stock.quant'].with_context(inventory_mode=True).create({
'product_id': product_id,
'location_id': warehouse.lot_stock_id.id,
'inventory_quantity': new_quantity,
})

Prestashop 1.7 - which hook to use in a module to get the used edited product quantity

I am building a simple module where I need to catch a backoffice quantity modification event (for product or variations) in real time and send the new quantity to an external API.
I am struggling in understanding which hook to use to get the actual user inserted quantity and not the "previous" product quantity.
If I use the static method StockAvailable::getQuantityAvailableByProduct inside the hookActionProductUpdate in my module, I am getting the original product quantity and not the new one, probably because the hook is called before the actual DB update.
Any clue ?
Try with: actionUpdateQuantity
You can pass the next parameters:
array(
'id_product' => (int) Product ID,
'id_product_attribute' => (int) Product attribute ID,
'quantity' => (int) New product quantity
);
List of hook here : https://devdocs.prestashop.com/1.7/modules/concepts/hooks/list-of-hooks/

SAP Hybris - current selected category or product in storefront

is possible get the currently selected product category or product in any mvc controller/service?
Thanks.
Yes, possible.
In ProductPageController.java, you will find ProductData as below.
final ProductData productData = productFacade.getProductForCodeAndOptions(productCode, extraOptions);
You will get Categories from,
productData.getCategories()
and similarly you will get product as well.

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

How to automatically create a cart for a customer in prestashop?

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.