Magento 2从代码

时间:2017-07-07 12:04:38

标签: php magento2

我正在尝试插入结帐/购买带有子项目的捆绑产品。 我正在使用此代码,但它只适用于简单的产品。

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$product = $objectManager->get('Magento\Catalog\Model\Product')->load(703);

$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');

$addToCartUrl =  $listBlock->getAddToCartUrl($product);

echo $addToCartUrl;

我也用过这个建议

$ objectManager = \ Magento \ Framework \ App \ ObjectManager :: getInstance();

$ product = $ this-> productRepository-> getById(“703”);

$ magento = array(82 =>“103”,82 =>“112”);

$ par = array('product'=> $ product-> getId(),'bundle_options'=>数组(1 => $ magento),'qty'=> $数量);

$这 - > cart-> addProduct命令($ _产品,$ PAR);

$这 - > cart->保存();

但仍然无效。有人能帮我吗? 感谢

1 个答案:

答案 0 :(得分:0)

//找到以下添加捆绑产品的代码。

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    <div class="form-group">
        @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
}

Reference link : https://magento.stackexchange.com/questions/112788/magento-2-add-bundled-product-to-cart-programmatically