kloon / WooCommerce-REST-API-Client-Library

A PHP wrapper for the WooCommerce REST API
GNU General Public License v3.0
356 stars 175 forks source link

Product custom meta data are not working for WooCommerce version 3.xx #237

Open Shibly opened 6 years ago

Shibly commented 6 years ago

Product with custom field are not working in WoCommerce version 3.xxx . Current code:

        $wc_data = array(
            'title' => $data['name'],
            'sku' => trim($data['serial_number']),
            'status' => $status,
            'regular_price' => trim($data['selling_price']),
            'description' => stripslashes($data['description']),
            'managing_stock' => true,
            'stock_quantity' => trim($data['total_quantity']),
            'categories' => $cat_ids,
            'tags' => array($data['season_id']),
            'enable_html_description' => true
        );

        if (isset($data['product_image'])) {
            $wc_data['custom_meta']['custom_image_url'] = $data['product_image'];
        }

        if (isset($data['swatch_image'])) {
            $wc_data['custom_meta']['swatch_image_url'] = $data['swatch_image'];
        }

        if (isset($data['purchase_price'])) {
            $wc_data['custom_meta']['wholesale_price'] = $data['purchase_price'];
        }

        if (isset($data['color_name'])) {
            $wc_data['custom_meta']['product_color'] = $data['color_name'];
        }

        if (isset($data['material_name'])) {
            $wc_data['custom_meta']['product_materials'] = $data['material_name'];
        }

        if (isset($data['dimensions'])) {
            $wc_data['custom_meta']['product_dimensions'] = $data['dimensions'];
        }

        if (isset($data['construction'])) {
            $wc_data['custom_meta']['product_construction'] = $data['construction'];
        }

        if (isset($data['origin']) && $data['origin'] != '') {
            $wc_data['custom_meta']['product_origin'] = $data['origin'];
        }

        if (isset($data['note'])) {
            $wc_data['custom_meta']['product_season'] = $data['note'];
        }

        if (isset($data['product_gallery']) && count($data['product_gallery']) > 1) {
            $wc_data['custom_meta']['product_gallery'] = $data['product_gallery'];
        } else {
            $wc_data['custom_meta']['product_gallery'] = array();
        }

      $client = new WC_API_Client($store_url, $consumer_key, $consumer_secret, $options);
      $is_created = $client->products->create($wc_data);

Same code was working fine in WooCommerce version 2.xx

Thanks :)