Magento 2将产品添加到类别(代码)

时间:2017-05-23 07:48:08

标签: categories magento2

我想通过代码创建产品,我想将产品添加到类别中 如何按代码将产品添加到类别中? 我尝试将类别添加到产品\Magento\Catalog\Model\Product,但没有方法setCategory或类似的东西 然后我尝试将产品添加到类别Magento\Catalog\Model\Category,并且没有方法addProduct或类似的东西。   我看到了功能

CategoryLinkManagementInterface -> assignProductToCategories(
$product->getSku(),
$product->getCategoryIds() //but there is not categories yet
)

3 个答案:

答案 0 :(得分:1)

Sub SaveSheetsIntoArray()
    Dim shArray, i
    ReDim shArray(1 To Sheets.count)
    For i = 1 To Sheets.count
        shArray(i) = ThisWorkbook.Sheets(i).Name
        Debug.Print shArray(i)
    Next
    'do something with the array "shArray"
End Sub

答案 1 :(得分:0)

您需要获取类别ID和产品ID来设置数据:恭喜:

$this->getCategoryLinkManagement()->assignProductToCategories(
                $product->getSku(),
                $product->getCategoryIds()
            );

also impliment this function :

private function getCategoryLinkManagement()
{
    if (null === $this->categoryLinkManagement) {
        $this->categoryLinkManagement = \Magento\Framework\App\ObjectManager::getInstance()
            ->get('Magento\Catalog\Api\CategoryLinkManagementInterface');
    }
    return $this->categoryLinkManagement;
}

你应该管理的休息依赖:

Magento\Catalog\Api\CategoryLinkManagementInterface

intialize : protected $categoryLinkManagement;

答案 2 :(得分:0)

def weasy_pdf_generation(request, id):
    # my data
    _, _, draft_details = get_draft_details('setup', request, id)
    radios_dict = {k:v[1] for k,v in draft_details.items()}
    # rendering to string
    html_template = render_to_string('tax/setupreview report.html', radios_dict)
    styles = CSS(url="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css")
    pdf_file = HTML(string=html_template).write_pdf(stylesheets=[styles])

    #response details
    response = HttpResponse(pdf_file, content_type='application/pdf')
    response['Content-Disposition'] = 'filename="home_page.pdf"'
return response