Wednesday, 4 September 2019

Add product to cart on a non shop page

We are using the software of Odoo 12 for enterprises.

We want to duplicate the behaviour of the add to cart button on the product pages. So we want to add a button on a different page and when you click the button you add a product to your cart and get redirected to your cart. The product is hardcoded to that button.

This is the form used on the product page template:

       <form t-if="product._is_add_to_cart_possible()" action="/shop/cart/update" method="POST">
          <input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
          <div class="js_product js_main_product">
            <t t-placeholder="select">
              <input type="hidden" class="product_id" name="product_id" t-att-value="product_variant.id"/>
              <input type="hidden" class="product_template_id" name="product_template_id" t-att-value="product.id"/>
              <t t-if="first_possible_combination" t-call="sale.variants">
                <t t-set="ul_class" t-value="'flex-column'"/>
                <t t-set="parent_combination" t-value="None"/>
              </t>
              <t t-else="">
                <ul class="d-none js_add_cart_variants" t-att-data-attribute_exclusions="{'exclusions: []'}"/>
              </t>
            </t>
            <t t-call="website_sale.product_price"/>
            <p t-if="True" class="css_not_available_msg alert alert-warning">This combination does not exist.</p>
            <a role="button" id="add_to_cart" class="btn btn-primary btn-lg mt8 js_check_product a-submit" href="#">Add to Cart</a>
          </div>
        </form>

The info of our product: product.id = 135 and product_template.id = 83.

I've found that the javascript responsible for the adding to the cart is called using: /web/content/.../.../web.assets_frontend.js. This is a very large file but you can check an example here: file.

Which qweb/form/js/... should I add on my custom page to add a product to my cart?

Thanks for any help, I've been stuck on this for a long time!

Edit: As @Philippe Pageau pointed out I can use some code to get the correct product already. I've tried implementing it with the form using this code (the simplest version of the form I can think of):

      <t t-set="products" t-value="request.env['product.product'].search([['id', '=', 135]])"/>
        <t t-foreach="products" t-as="product">
          <form action="/shop/cart/update" method="POST">
             <input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
             <input type="hidden" class="product_id" name="product_id" value="135"/>
             <input type="hidden" class="product_template_id" name="product_template_id" value="83"/>
             <a role="button" id="add_to_cart" class="btn btn-primary btn-lg mt8 js_check_product a-submit" href="#">Add to Cart</a>
          </form>
      </t>

But this does nothing, what am I missing?



from Add product to cart on a non shop page

No comments:

Post a Comment