Kaamyi
Kaamyi

Reputation: 63

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

Upvotes: 1

Views: 1978

Answers (2)

Kaamyi
Kaamyi

Reputation: 63

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

Upvotes: 3

dh42
dh42

Reputation: 38

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.

Upvotes: 0

Related Questions