查看内容

magento2程序添加customer option

2017-9-20 12:52| 发布者: lutish| 查看: 472| 评论: 0

<?php


  // 初始化程序
require __DIR__ . '\app/bootstrap.php';
$bootstrap = \magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');

$state->setAreaCode('frontend');

$today_date = date("m/d/Y");
$added_date = date('m/d/Y',strtotime("+17 day"));

$set_product = $objectManager->create('\Magento\Catalog\Model\Product');



    $set_product->setCanSaveCustomOptions(1);
    
    $options = [
    [
        'title' => 'drop_down option',
        'type' => 'drop_down',
        'is_require' => true,
        'sort_order' => 4,
        'values' => [
            [
                'title' => 'drop_down option 1',
                'price' => 10,
                'price_type' => 'fixed',
                'sku' => 'drop_down option 1 sku',
                'sort_order' => 1,
            ],
            [
                'title' => 'drop_down option 2',
                'price' => 20,
                'price_type' => 'percent',
                'sku' => 'drop_down option 2 sku',
                'sort_order' => 2,
            ],
        ],
    ]
];
    
    $customOptions = [];

/** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory $customOptionFactory */
$customOptionFactory = $objectManager->create('Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory');
$optionValueFactory = $objectManager->create('Magento\Catalog\Api\Data\ProductCustomOptionValuesInterfaceFactory');

foreach ($options as $option) {
    /** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterface $customOption */
    $customOption = $customOptionFactory->create(['data' => $option]);
    $customOption->setProductSku(strtotime('now'));
    if (isset($option['values'])) {
        $values = [];
        foreach ($option['values'] as $value) {
            $value = $optionValueFactory->create(['data' => $value]);
            $values[] = $value;
        }
        $customOption->setValues($values);
    }
    $customOptions[] = $customOption;
}

$set_product->setOptions($customOptions);
    
    
    
    $set_product->save();
    // get id of product
    $get_product_id = $set_product->getId();
    echo "Upload simple product id :: ".$get_product_id."\n";
}
catch(Exception $exception)
{
    // errro in exception/code
    echo $exception->getMessage();
}
1.此方法使用
Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory

在首次保存产品时写入customer option数据

<?php

 
  // 初始化程序
require __DIR__ . '\app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');

$state->setAreaCode('frontend');

$today_date = date("m/d/Y");
$added_date = date('m/d/Y',strtotime("+17 day"));

$set_product = $objectManager->create('\Magento\Catalog\Model\Product');


$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of Object manager
$productId = 98;
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
$values = array(
                    array(
                        'title'=>'Red',
                        'price'=>10,
                        'price_type'=>"fixed",
                        'sku'=>"Red1",
                        'sort_order'=>1,
                        'is_delete'=>0,
                        'option_type_id'=>-1,
                    ),
                    array(
                        'title'=>'White',
                        'price'=>10,
                        'price_type'=>"fixed",
                        'sku'=>"White1",
                        'sort_order'=>1,
                        'is_delete'=>0,
                        'option_type_id'=>-1,
                    ),
                    array(
                        'title'=>'Black',
                        'price'=>10,
                        'price_type'=>"fixed",
                        'sku'=>"black1",
                        'sort_order'=>1,
                        'is_delete'=>0,
                        'option_type_id'=>-1,
                    )
                );
         
$options = array(
                    array(
                        "sort_order"    => 1,
                        "title"         => "Field Option",
                        "price_type"    => "fixed",
                        "price"         => "",
                        "type"          => "field",
                        "is_require"    => 0
                    ),
                    array(
                        "sort_order"    => 2,
                        "title"         => "Color",
                        "price_type"    => "fixed",
                        "price"         => "",
                        "type"          => "drop_down",
                        "is_require"    => 0,
                        "values"        => $values
                    ),
                    array(
                        "sort_order"    => 2,
                        "title"         => "Multiple Option",
                        "price_type"    => "fixed",
                        "price"         => "",
                        "type"          => "multiple",
                        "values"        => $values,
                        "is_require"    => 0
                    )
                );
         
        foreach ($options as $arrayOption) {
            $product->setHasOptions(1);
            $product->getResource()->save($product);
            $option = $objectManager->create('\Magento\Catalog\Model\Product\Option')
                    ->setProductId($productId)
                    ->setStoreId($product->getStoreId())
                    ->addData($arrayOption);
            $option->save();
            $product->addOption($option);
        }
此方法为向已知产品写入customer option值,使用
\Magento\Catalog\Model\Product\Option
两种方法完全可以满足magento2关于customer option的需要。


返回顶部