I have a script (working) for a prestashop 1.7 to add product in my db
require_once('config/config.inc.php');
require_once('init.php');
$default_lang = Configuration::get("PS_LANG_DEFAULT");
$product = new Product();
$product->name = [$default_lang => "Acquisto cumulativo diretta online"];
$product->link_rewrite = "acquisto_online";
$product->price = 95.90;
$product->quantity = 10;
$product->id_category = [10];
$product->id_category_default = 10;
$bool = $product->add();
if ($bool)
{
$product->updateCategories($product->id_category);
StockAvailable::setQuantity((int)$product->id, 0, $product->quantity, Context::getContext()->shop->id);
}
It's working fine, I can add my product in a category I make as "not visible" and I can use that product just fine.
Then I need to create (without login) a cart for a specific user with this product (and maybe other product he already have in his cart)
so I write my check on database for id_customer, and create an instance of customer
$customer = new Customer(3); // where 3 in this example is the correct id of the customer.
then I check on db if a cart is already exist for this id_customer, if I find a regular cart I just use
$cart = new Cart(ID_CART_I_FIND_ON_DB);
$cart->updateQty(1, 122); // where 1 is the quantity, 122 the id of my product
and it work perfectly, the user refreshing is cart can see the new product.
Now the problem:
if I don't find a regular cart, I need to create a new one first
$cart = new Cart();
$cart->id_customer = (int)($customer->id);
$cart->id_address_delivery = (int) (0);
$cart->id_address_invoice = $cart->id_address_delivery;
$cart->id_lang = (int)(2);
$cart->id_currency = (int)(2);
$cart->id_carrier = 1;
$cart->recyclable = 0;
$cart->gift = 0;
$cart->add();
$cart->update();
$cart->save();
$cart->updateQty(1, 122);
it kinda works, I can see on my db this cart of course, I can see it in my Back Office pannel, but the user can't see it because of "secure key" and because of guest_id. I try to manually insert in db the correct secury key but nothing. I try to add a guest_id manually and then force the login of this user and sometimes the cart appears.
I'm sure I missing something. could someone help me with this? :)
Maybe there is a better way to "create" a new cart (if not exists) for a specific user from an external script (so without his cookie)
thanks
I actually find a workaround, maybe too long or just not elegant but it works.
Reading how the real code works I understand I need a session (with login) before making an actual cart for an user so I write:
$customer = new Customer(ID_USER);
$context=Context::getContext(); //I set a context manually
$context->customer = $customer;
//create a cookie
$context->smarty->assign('confirmation', 1);
$context->cookie->id_customer = (int)$customer->id;
$context->cookie->customer_lastname = $customer->lastname;
$context->cookie->customer_firstname = $customer->firstname;
$context->cookie->passwd = $customer->passwd;
$context->cookie->logged = 1;
if (!Configuration::get('PS_REGISTRATION_PROCESS_TYPE'))
$context->cookie->account_created = 1;
$customer->logged = 1;
$context->cookie->email = $customer->email;
$context->cookie->is_guest = !Tools::getValue('is_new_customer', 1);
// Update cart address
$context->cart->secure_key = $customer->secure_key;
then if cart didn't already exist of course:
$cart = new Cart();
$cart->id_customer = (int)($customer->id);
$cart->id_address_delivery = (int) (0);
$cart->id_address_invoice = $cart->id_address_delivery;
$cart->id_lang = (int)(2);
$cart->id_currency = (int)(2);
$cart->id_carrier = 1;
$cart->recyclable = 0;
$cart->secure_key = $customer->secure_key;
$cart->gift = 0;
$cart->add();
$cart->id_guest = $context->cart->id_guest;
$cart->update();
$cart->save();
of course user need to login again to see the cart, but prestashop seemes to "auto-create" a cart every time a user login (for x time) so this procedure is only when a user didn't login on website or not have a cart for some reason.