简体   繁体   English

如何从控制器将产品添加到 sylius 购物车项目

[英]how to add product to sylius cart item from a controller

After much fruitless research am still stranded trying to use sylius cart.经过多次徒劳的研究,我仍然无法使用 sylius 推车。 I found out that one way to add item to cart from the twig template is我发现从树枝模板将项目添加到购物车的一种方法是

<form action="{{ path('sylius_cart_item_add', {'id': product.id}) }}" method="post" class="clearfix">
            {% set form = sylius_cart_form({'product': product}) %}
            {{ form_widget(form.quantity, {'attr': {'class': 'center-text'}, 'empty_value': '1'}) }}
            {{ form_widget(form._token) }}
            <button type="submit" class="btn btn-success"><i class="icon-shopping-cart icon-white"></i> {{ 'sylius.add_to_cart'|trans }}</button>
        </form>

when I try to use this I get error当我尝试使用它时,我收到错误

An exception has been thrown during the rendering of a template ("The option "product" does not exist. Defined options are: "action", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "max_length", "method", "pattern", "post_max_size_message", "property_path", "read_only", "required", "translation_domain", "trim", "validation_groups", "virtual".")模板渲染过程中抛出异常(“选项“产品”不存在。定义的选项是:“动作”、“allow_extra_fields”、“attr”、“auto_initialize”、“block_name”、“by_reference”、 "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_data_class", ", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "max_length", “方法”、“模式”、“post_max_size_message”、“property_path”、“read_only”、“必需”、“translation_domain”、“trim”、“validation_groups”、“virtual”。)

if I replace the如果我更换

sylius_cart_form({'product': product})

with

sylius_cart_form({'data_class': sylius_cart_item})

the error goes out when I submit the quantity does not persist.当我提交数量不持续时错误消失。

Twig:枝条:

 <form action="{{ path('sylius_cart_item_add', {'id': product.id}) }}" method="post">
    {{ form(form) }}
    <input type="submit" value="Submit">
 </form>

The form should be sent by the controller.表格应由控制器发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM