我的错误与woocommerce api自定义选项?

时间:2015-04-13 15:18:14

标签: php wordpress api woocommerce

我在自定义选项的来源中的错误在哪里?我正在使用woocommerce rest api。在foreach中我需要添加不同的选项,如S - 蓝色,M - 红色,S - 红色,M - 蓝色,但我在wordpress中输入空白:

enter image description here

有我的代码和DOC: http://woothemes.github.io/woocommerce-rest-api-docs/#create-a-product

public function addProduct($data)
    {
    $wc_api = $this->_getClient();

    // size
    $sizeArray = array();
    foreach($data['size'] as $size){
        $sizeArray[] = $size;
    }

    // color
    $colorArray = array();
    foreach($data['color'] as $color){
        $colorArray[] = $color;
    }

    foreach ($data['size'] as $size) {
        foreach ($data['color'] as $color) {
            $options[] = 
                [
                    'regular_price' => $data['price'],
                    'attributes'    => 
                        [ 
                            array('name' => 'Size', 'slug' => 'size', 'option' => $size),
                            array('name' => 'Color', 'slug' => 'color', 'option' => $color)
                        ]
                ];
        }
    }
    // http://woothemes.github.io/woocommerce-rest-api-docs/#create-a-product
    $newProductData = array(
        'product' => array(
            'title' => $data['title'],
            'type' => 'variable',
            'regular_price' => $data['price'],
            'description' => $data['description'],
            'sku' => $data['sku'],
            'tags' => [ $data['tags'] ],
            'attributes' =>
                [
                    array('name' => 'Size', 'slug' => 'size', 'position' => '0', 'visible' => true, 'variation' => true, 'options' => [ implode(' | ', $sizeArray) ]),
                    array('name' => 'Color', 'slug' => 'color', 'position' => '1', 'visible' => true, 'variation' => true, 'options' => [ implode(' | ', $colorArray) ])
                ],
            'variations' => $options,
            'images' => [ array('src' => $data['image'], 'position' => '0') ],
            'virtual' => true
        )
    );

    return $wc_api->create_product($newProductData);
}

1 个答案:

答案 0 :(得分:0)

当我尝试创建一个特别是可变产品的产品时,我遇到了同样的问题。终于得到了解决方案。

如果您的代码是正确的并且它只是没有创建变体,那么我找到的解决方案是您必须只使用一个属性' name'或者' slug'用'选项'属性保持原样。请尝试仅使用' name'和'选项'在属性或' slug'和'选项'。

我在这里给我的代码作为例子。

$child_prod_json = json_encode(
    array( 'product' =>
    array( 'title'           => $child_prod->product->title,
        'type'           => 'variable',
        'variations'         => array(array(
        'regular_price'      => $child_prod->product->regular_price,
        'sale_price'         => $child_prod->product->sale_price,
        'images'         => array( array( 'src'      => $child_prod->product->images[0]->src,
            'position'   => $child_prod->product->images[0]->position ) ),
        'attributes'    => array(array('name'=>$child_prod->product->attributes[0]->name,'option'=>$child_prod->product->attributes[0]->option)),
        'managing_stock'     => $child_prod->product->managing_stock,
        'in_stock'       => $child_prod->product->in_stock,
        'stock_quantity'     => $child_prod->product->stock_quantity,
        'shipping_class' => $shipping_class,
        'shipping_class_id'     =>$shipping_class_id,
    'purchase_note'     =>$child_prod->product->purchase_note,          
        ),)
    )  )    );

对于这个例子,我只使用第一个属性,即[0]位置作为演示。

我的观点是 - ' name'和' slug'不会一起工作。使用其中任何一个。这个解决方案对我有用。请尝试了解它是否对您有用或有用。