【问题标题】:Where my mistake with woocommerce api custom option?我对 woocommerce api 自定义选项的错误在哪里?
【发布时间】:2015-04-13 15:18:13
【问题描述】:

我在带有自定义选项的源中的错误在哪里?我正在使用 woocommerce rest api。在 foreach 中,我需要添加不同的选项,例如 S - blue、M - red、S - red、M - blue,但我在 wordpress 中得到了空输入:

有我的代码和文档: 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);
}

【问题讨论】:

    标签: php wordpress api woocommerce


    【解决方案1】:

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

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

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

    $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”不能一起使用。使用其中任何一个。这个解决方案对我有用。请尝试让我知道它是否对您有用/有用。

    【讨论】:

    • 我也有同样的问题。你的解决方案没有奏效。尝试了 slug 和 option,以及 name 和 option。
    猜你喜欢
    • 2017-12-23
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 2011-07-09
    • 1970-01-01
    相关资源
    最近更新 更多