Jump to content

Add Product to cart with custom prize when product was added


Recommended Posts

Hey, I have small problem with my custom module. I add to custom field on product page, customer put to this inputs a text and when his add the product to cart, automatically will be added another item with prize = count of text with custom inputs * prize for letter from module configuration. I do module configuration etc.  but i dont know how to add this custom item.

Share this post


Link to post
Share on other sites

Hi,

The  sopping cart  can contain only  id_product,  id_product_attribute(  id  product  combination )  and id_customization.

If you have  a custom  item  you will have to modify  it by adding new field  for the id of your  custom  id.

Share this post


Link to post
Share on other sites
On 1/7/2020 at 2:56 AM, zawiszaty said:

Hey, I have small problem with my custom module. I add to custom field on product page, customer put to this inputs a text and when his add the product to cart, automatically will be added another item with prize = count of text with custom inputs * prize for letter from module configuration. I do module configuration etc.  but i dont know how to add this custom item.

Hi, 

There are 2 points need to be updated for your requirement to fulfilled, you may try the below points 

=> First you need to create that custom item as product because while adding in cart you need product id 
 
        $context = Context::getContext();
        $cart = $context->cart;
        $cart->updateQty((int)($qty), (int)($id_product),0);custom product id you want to add
        $cart->update();

=> For price of that item you can set Specific price for current cart and product you added 

        $sp = new SpecificPrice();
        $sp->id_product = (int)$id_product; // custom product id you want to add
        $sp->id_product_attribute = 0
        $sp->id_cart = (int)$current_id_cart;
        $sp->from_quantity = 1;
        $sp->price = $new_price_without_tax;
        $sp->reduction_type = 'amount';
        $sp->reduction_tax = 1;
        $sp->reduction = 0;
        $sp->from = "0000-00-00 00:00:00";
        $sp->to = "0000-00-00 00:00:00"; 
        $sp->add(); 

let us know if still any query 


 

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More