Jan Fromm Posted June 1, 2022 Share Posted June 1, 2022 Is it possible to add products to the cart without the card-add form, but with a simple php wrapper instead? Something like this: $cart = $padloper->getCart(); $cart->addProduct($product); I remember that this once seemed to work with Padloper 1: Any hints are appreciated – thanks! Link to comment Share on other sites More sharing options...
Jan Fromm Posted June 3, 2022 Author Share Posted June 3, 2022 To explain my issue: I have about 120 digital products (fonts). Each of the products can be purchased for 3 license types, each of the license types has different factors based on the volume of usage. I could map this with product variations. But I would have over 4000 in total - a nightmare to create and maintain. On top of that, the product variations only differ in price, the rest stays the same. My idea is to create only the 120 basic products in the backend. In the frontend one would select (Padloper-independent) the product, the license type and the license volume. This information would then be passed to a PHP file that adds the product to the cart, and uses a hook to calculate the price and change it accordingly. I don’t need an add-to-cart form for this, but would rather do it like this: $cart = $padloper->getCart(); $cart->addProduct($product); Unfortunately, using this syntax doesn’t work. If anyone has any tips to point me in the right direction, I would be incredibly grateful. Link to comment Share on other sites More sharing options...
kongondo Posted June 3, 2022 Share Posted June 3, 2022 Hi @Jan Fromm This is how you add to cart programmatically: Please note that Padloper (2) does not make a distinction between $product ID and $variant ID. A variant is just a product that happens to be the child of a main product. Hence, addProduct() below takes the ID of the product page if you are adding a product without variants or the variant page if you are adding the variant of a product page. Add one (quantity) of a product or a variant <?php $padloper->cart->addProduct($product->id); // OR /** @var PadloperCart $cart */ // $cart = $padloper->cart; // $cart->addProduct($product->id); Add multiple (quantities) of a product or a variant <?php // add 17 (quantity) of this product $padloper->cart->addProduct($product->id, 17); // OR /** @var PadloperCart $cart */ // $cart = $padloper->cart; // $cart->addProduct($product->id,17); If you want to add multiple products: Find the products (PageArray of products) using a selector, e.g. by collection/categories. Then loop through and add each: <?php // add products from the collection 'accessories' /** @var PageArray $products */ $products = $padloper->find("categories=accessories"); foreach ($products as $product) { $padloper->cart->addProduct($product->id); } OR using findRaw() array of stdClass <?php /** @var array $products */ $products = $padloper->findRaw("categories=accessories,objects=1", ['id']); foreach ($products as $product) { $padloper->cart->addProduct($product->id); } OR using findRaw() plain array <?php /** @var array $products */ $products = $padloper->findRaw("categories=accessories", 'id'); foreach ($products as $productID) { $padloper->cart->addProduct($productID); } Hope this helps. Link to comment Share on other sites More sharing options...
kongondo Posted June 3, 2022 Share Posted June 3, 2022 8 hours ago, Jan Fromm said: and uses a hook to calculate the price and change it accordingly. Here's an example hook on how to modify the price of a product using the API https://docs.kongondo.com/hooks.html#price-hook Link to comment Share on other sites More sharing options...
Jan Fromm Posted June 6, 2022 Author Share Posted June 6, 2022 Hi @kongondo, This explains a lot, thanks for that! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now