Dima
Dima

Reputation: 17

Odoo8 website transfer input value from product page to cart page

I'm working on project in odoov8. I need to add a field on the product page and then create an order which includes this field.

To be more specific, I have added Start Date field on the product page, I can select a date on it, but when I hit "Add to Cart" and I go to Cart page, product don't have the date I have selected. I need similar functionality that quantity field has.

Here is the code I used to display the Date field on product page:

<template id="product_quantity" inherit_id="website_sale.product">
            <xpath expr="//a[@id='add_to_cart']" position="after">
                <p>
                    <group colspan="2" col="2">
                        <label for="date">Rent Start Date
                            <input type="date" string="Rent Start Date"  class="form-control" name="start_date" id="start_date"  data-oe-model="ir.ui.view" data-oe-field="arch"  />
                        
                        </label>
                    </group>
                </p>
            </xpath>
    </template>

And here is how it looks:
Date field on product page

Also, I'm interested in a way to initialize this field with current date.

Any help is appreciated. Thank you!

Upvotes: 0

Views: 76

Answers (1)

Andrei Poehlmann
Andrei Poehlmann

Reputation: 351

You need to extend the model sale.order and add a new field, in your case call it maybe start_date, but make sure it does not already exist on sale.order.

You can see an example of how to define such a field in the original Odoo v8 repo here. You can then set a default value for that field, see for example here.

If I recall correctly, the Add to cart button triggers the controller at the route '/shop/cart/cart_update', so you will have to extend the method cart_update and update the sale order according to your needs.

I would suggest you extend the method, do a super call as it is for example done here (this is just an example in case you have never done this, of course you need to adjust it) and save the return value for super in a variable, e.g. res = super(...).

After the super you change the sale order (you need to fetch it somehow, either you check if the sale order is already available in res.qcontext (probably will not since the parent returns a redirect), or you fetch it via request.website.sale_get_order() which can be seen here.

After you update the sale order, you finally return res.

Something along these lines should do it:

@http.route()
def cart_update(self, product_id, add_qty=1, set_qty=0, **kw):
    res = super(WebsiteSale, self).cart_update(product_id,
                                               add_qty, set_qty, **kw)
    order = request.website.sale_get_order()
    # update your order now 
    # ... 

    # finally return 
    return res

Upvotes: 1

Related Questions