【发布时间】:2019-07-29 12:53:47
【问题描述】:
我们正在为企业使用 Odoo 12 的软件。
我们希望复制产品页面上添加到购物车按钮的行为。 因此,我们想在不同的页面上添加一个按钮,当您单击该按钮时,您会将产品添加到您的购物车并被重定向到您的购物车。 产品已硬编码到该按钮。
这是产品页面模板上使用的表格:
<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>
我们产品的信息:product.id = 135和product_template.id = 83。
我发现负责添加到购物车的 javascript 被调用使用:/web/content/.../.../web.assets_frontend.js。这是一个非常大的文件,但您可以在此处查看示例:file。
我应该在我的自定义页面上添加哪个 qweb/form/js/... 以将产品添加到我的购物车?
感谢您的帮助,我已经卡了很长时间了!
编辑: 正如@Philippe Pageau 指出的那样,我已经可以使用一些代码来获得正确的产品。我已经尝试使用此代码(我能想到的最简单的表单版本)使用表单来实现它:
<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>
但这无济于事,我错过了什么?
编辑2:
感谢@Adan Cortes,我们现在更进一步,但仍有 1 个问题。
现在,当用户单击按钮时,产品会以特定数量添加到购物车中。
这是我现在的代码:
<t t-set="products" t-value="request.env['product.product'].search([['id', '=', 135]])"/>
<t t-foreach="products" t-as="product">
<div id="product_detail" class="oe_website_sale">
<form action="/shop/cart/update" method="POST">
<h4 t-esc="product.name"/>
<h6 t-esc="product.price"/>
<input class="form-control" data-min="1" name="add_qty" value="1"/>
<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
<input type="hidden" class="product_id" name="product_id" value="135"/>
<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>
</div>
</t>
但这些是我最后的问题:
此代码不显示产品价格。
<h6 t-esc="product.price"/>显示0.00。那么如何显示价格呢?终于可以只用一个按钮和表单一次添加多个产品了吗?
【问题讨论】:
标签: javascript html forms qweb odoo-12