phpprestashopproductcartprestashop-1.6

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


Solution

  • 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);
    }