Woocommerce Rest API更新产品图像给出错误

时间:2018-08-23 07:12:51

标签: php wordpress laravel woocommerce woocommerce-rest-api

我想用产品图片更新我的产品,但未更新。我正在使用woocommerce rest API添加产品。

我正在按照以下步骤更新产品:

  1. 创建一个视图文件以显示所有产品列表
  2. 在控制器中创建编辑功能以获取所有产品数据
  3. 从编辑表单上传新图像时,它会显示错误

这是我的代码:

if($request->hasfile('filename'))
        {

            foreach($request->file('filename') as $image)
            {
                $name=$image->getClientOriginalName();
                $image->move('C:/xampp/htdocs/New-flex/wp-content/uploads/backend-product-image', $name);  
                $productimg_path_store_db = "http://localhost/New-flex/wp-content/uploads/backend-product-image/";
                $productimg_allimg[] = $productimg_path_store_db.$name;  
            }
        }

        $AddContentText = $request->get('AddContentText');
        if (!empty($AddContentText)) {
            /* For List All product qnumber check in array */
            $newallproducts_data = $woocommerce->get('products',array('per_page' => 100));
            $array = json_decode(json_encode($newallproducts_data), True);
            $partssku = array();
            $partid = array();
            $parturl = array();
            foreach ($array as $key ) {
                $partssku[] = $key['sku'];
                $partid[] = $key['id'];
                $parturl[] = $key['permalink'];
            }
            /* Create associative array product id and sku for compare */
            $comb = array_combine($partssku,$partid);

            /* Combine content QNUMBER and QTY in array */
            $chunks = array_chunk(preg_split('/(;|,)/', $AddContentText), 2);
            $result = array_combine(array_column($chunks, 0), array_column($chunks, 1));

            /*Product qnumber allready avilabale array*/
            $diff = array_intersect_key($result,$comb);
            /*NEW PRODUCT array*/
            $newproduct = array_diff($result, $diff);
            /* Qnumber insert in database */
            $parts_name = implode(',', array_keys($result));
            /* count insert in database */
            $noofpartsuse = implode(',', array_values($result));

            /*echo "<pre>";
                echo "main data";
                print_r($result);
                echo "allproduct array";
                print_r($comb);
                echo "Product qnumber allready avilabale";
                print_r($diff);
                echo "NEW PRODUCT";
                print_r($newproduct);
            echo "</pre>";*/

            // print_r($newproduct_data);
            //print_r($woocommerce->post('products', $data));
        }
        else{
            $parts_name = '';
            $noofpartsuse = '';
        }
        $productactive = $request->get('productactive');
        if(!empty($productactive))
        {
        $productactive = "publish";
        }
        else{
        $productactive = "draft";
        }

        $ProductListOrder = $request->get('ProductListOrder');

        $producttype = $request->get('producttype');

        $ProductQNumber = $request->get('ProductQNumber');

        $ProductName = $request->get('name');

        $ProductWidthMM = $request->get('ProductWidthMM');

        $ProductLengthMM = $request->get('ProductLengthMM');

        $ProductWidthInch = $request->get('ProductWidthInch');

        $ProductLengthInch = $request->get('ProductLengthInch');

        $Productinfotext = $request->get('Productinfotext');

        $Producttechnocaldesc = $request->get('Producttechnocaldesc');

        $metadescription = $request->get('metadescription');

        $proimgalt = $request->get('proimg-alt');

        $proyoutubelink = $request->get('proyoutubelink');

        $proyoutubelinkalt = $request->get('proyoutubelink-alt');

        $provimeolink = $request->get('provimeolink');

        $provimeolinkalt = $request->get('provimeolink-alt');

        $onshapelink = $request->get('onshapelink');

        $onshapelinkalt = $request->get('onshapelink-alt');

        $data = [
            'name' => $ProductName,
            'type' => $producttype,
            'status' => $productactive,
            'regular_price' => '',
            'description' => $Productinfotext,
            'short_description' => $Producttechnocaldesc,
            'sku' => $ProductQNumber,
            'categories' =>array (),
            'meta_data' => [
                [
                    'key' => 'list_order',
                    'value' => $ProductListOrder
                ],
                [
                    'key' => 'ProductWidthMM',
                    'value' => $ProductWidthMM
                ],
                [
                    'key' => 'ProductLengthMM',
                    'value' => $ProductLengthMM
                ],
                [
                    'key' => 'ProductWidthInch',
                    'value' => $ProductWidthInch
                ],
                [
                    'key' => 'ProductLengthInch',
                    'value' => $ProductLengthInch
                ],
                [
                    'key' => 'proyoutubelink',
                    'value' => $proyoutubelink
                ],
                [
                    'key' => 'proyoutubelink-alt',
                    'value' => $proyoutubelinkalt
                ],
                [
                    'key' => 'provimeolink',
                    'value' => $provimeolink
                ],
                [
                    'key' => 'provimeolink-alt',
                    'value' => $provimeolinkalt
                ],
                [
                    'key' => 'onshapelink',
                    'value' => $onshapelink
                ],
                [
                    'key' => 'onshapelink-alt',
                    'value' => $onshapelinkalt
                ],
                [
                    'key' => 'UseParts-link',
                    'value' => $parts_name
                ],
                [
                    'key' => 'Noofparts-use',
                    'value' => $noofpartsuse
                ]
            ],
            'images' => array ()
        ];

        /* image array for store image */
        if(!empty($productimg_allimg)){
            $images = &$data['images'];
            $n = 0;
            foreach($productimg_allimg as $id)
            {
            $images[] = array( //this array must be created dynamic 
            'src' => $id,
            'position' => $n++
            );
            }
            unset($images);
        }
        if(!empty($_POST['categories'])){
        /* Producta category array */
        $categories = &$data['categories'];
        foreach($_POST['categories'] as $cat)
        {
        $categories[] = array( //this array must be created dynamic 
        'id' => $cat
        );
        }
        unset($categories);
        }

        $data_insert = $woocommerce->post('products', $data);
        if($data_insert){
            foreach ($newproduct as $key => $value) {
                $newproduct_data = [
                                    'name' => $key,
                                    'sku' => $key
                                   ];
                $woocommerce->post('products', $newproduct_data);
            }
        }

        return redirect('products')->with('success', 'Product has been Added');

上面用laravel编写的代码

0 个答案:

没有答案