public function mapAmazonListingToWoo($listing, $product_node = null) { // echo "<pre>listing data: ";print_r($listing);echo"</pre>";#die(); // Woo Product # Amazon Listing $data['asin'] = $listing['asin']; $data['name'] = $listing['listing_title']; $data['description'] = $listing['description']; $data['image'] = @$listing['attributes']->SmallImage->URL; $data['weight'] = @$listing['attributes']->ItemDimensions->Weight; $data['quantity'] = $listing['quantity']; $data['price'] = $listing['price']; $data['sku'] = $listing['sku']; $data['additional_description'] = ''; $data['products_date_added'] = $listing['date_published']; $data['categories'] = array(); $data['variations'] = array(); $data['attributes'] = array(); $data['dimensions'] = false; $data['images'] = isset($listing['additional_images']) ? json_decode($listing['additional_images']) : array(); $data['_meta_fields'] = array(); $data['condition_type'] = ''; $data['condition_note'] = ''; // get item condition from report details $report_row = json_decode($listing['details'], true); // echo "<pre>";print_r($report_row);echo"</pre>";#die(); if (is_array($report_row) && isset($report_row['item-condition'])) { $amazon_condition_type = WPLA_ImportHelper::convertNumericConditionIdToType($report_row['item-condition']); $data['condition_type'] = $amazon_condition_type; $data['condition_note'] = $report_row['item-note']; } // use PackageDimensions if ItemDimensions are not set if (!$data['weight']) { $data['weight'] = @$listing['attributes']->PackageDimensions->Weight; } // dimensions if (@$listing['attributes']->ItemDimensions) { $data['dimensions'] = array('width' => @$listing['attributes']->ItemDimensions->Width, 'length' => @$listing['attributes']->ItemDimensions->Length, 'height' => @$listing['attributes']->ItemDimensions->Height); } else { $data['dimensions'] = array('width' => @$listing['attributes']->PackageDimensions->Width, 'length' => @$listing['attributes']->PackageDimensions->Length, 'height' => @$listing['attributes']->PackageDimensions->Height); } // default values $data['post_status'] = 'publish'; // not used $data['special_price'] = ''; $data['weight_unit'] = 'kilogram'; // get 600px image instead of 75px $data['image'] = str_replace('_SL75_', '_SL600_', $data['image']); // fetch additional / highres product images $webHelper = new WPLA_AmazonWebHelper(); $webHelper->loadListingDetails($listing['id']); $product_images = $webHelper->getImages(); if (!empty($product_images)) { // $data['image'] = array_shift( $product_images ); $data['image'] = $product_images[0]; // regard option to import only main image if (get_option('wpla_enable_gallery_images_import', 1) == 1) { $data['images'] = $product_images; } } // parse bullet points if (isset($listing['attributes']->Feature) && is_array($listing['attributes']->Feature)) { // add post meta fields $bp_index = 1; foreach ($listing['attributes']->Feature as $feature) { $data['_meta_fields']['_amazon_bullet_point' . $bp_index] = $feature; $bp_index++; } // add list of features to description $featuresHtml = '<ul class="amazon_features">' . "\n"; foreach ($listing['attributes']->Feature as $feature) { $featuresHtml .= "\t" . '<li>' . $feature . '</li>' . "\n"; } $featuresHtml .= '</ul>' . "\n"; $data['description'] .= "\n" . $featuresHtml; } // add condition info to description if ($data['condition_note']) { $condition_type = wpla_spacify($data['condition_type']); $conditionHtml = '<div class="amazon_condition">'; $conditionHtml .= '<span class="amazon_condition_label">'; $conditionHtml .= __('Item Condition', 'wpla') . ': '; $conditionHtml .= '</span>'; $conditionHtml .= $condition_type . '. ' . $data['condition_note']; $conditionHtml .= '</div>' . "\n"; $conditionHtml = apply_filters('wpla_filter_imported_condition_html', $conditionHtml, $data); $data['description'] .= "\n" . $conditionHtml; } // parse Book specific attributes like author, binding and date published if (isset($listing['attributes']->ProductGroup) && $listing['attributes']->ProductGroup == 'Book') { if (isset($listing['attributes']->Publisher)) { $attrib = new stdClass(); $attrib->name = 'Publisher'; $attrib->value = is_array($listing['attributes']->Publisher) ? join('|', $listing['attributes']->Publisher) : $listing['attributes']->Publisher; $data['attributes'][$attrib->name] = $attrib; } if (isset($listing['attributes']->Binding)) { $attrib = new stdClass(); $attrib->name = 'Binding'; $attrib->value = $listing['attributes']->Binding; $data['attributes'][$attrib->name] = $attrib; } if (isset($listing['attributes']->Edition)) { $attrib = new stdClass(); $attrib->name = 'Edition'; $attrib->value = $listing['attributes']->Edition; $data['attributes'][$attrib->name] = $attrib; } if (isset($listing['attributes']->PublicationDate)) { $attrib = new stdClass(); $attrib->name = 'Publication date'; $attrib->value = $listing['attributes']->PublicationDate; $data['attributes'][$attrib->name] = $attrib; } if (isset($listing['attributes']->NumberOfPages)) { $attrib = new stdClass(); $attrib->name = 'Number of pages'; $attrib->value = $listing['attributes']->NumberOfPages; $data['attributes'][$attrib->name] = $attrib; } if (isset($listing['attributes']->Creator)) { $attrib = new stdClass(); $attrib->name = 'Author'; $attrib->value = is_array($listing['attributes']->Creator) ? join('|', $listing['attributes']->Creator) : $listing['attributes']->Creator; $data['attributes'][$attrib->name] = $attrib; } if (isset($listing['attributes']->Author)) { $attrib = new stdClass(); $attrib->name = 'Author'; $attrib->value = is_array($listing['attributes']->Author) ? join('|', $listing['attributes']->Author) : $listing['attributes']->Author; $data['attributes'][$attrib->name] = $attrib; } } // include variations from product node if ($product_node) { if (isset($product_node->variations) && is_array($product_node->variations)) { $data['variations'] = $product_node->variations; // fetch highres product images foreach ($data['variations'] as &$var) { if (!$var->listing_id) { continue; } $webHelper->loadListingDetails($var->listing_id); $var_images = $webHelper->getImages(); if (!empty($var_images)) { $var->variation_image = $var_images[0]; WPLA()->logger->info("found HighRes var image: " . $var->variation_image); } } // each var } } // allow other plugins to modify $data $data = apply_filters('wpla_filter_imported_product_data', $data, $listing, $report_row); return $data; }
public function jobs_run_task() { // quit if no job name provided if (!isset($_REQUEST['job'])) { return false; } if (!isset($_REQUEST['task'])) { return false; } $job = $_REQUEST['job']; $task = $_REQUEST['task']; // register shutdown handler global $wpla_shutdown_handler_enabled; $wpla_shutdown_handler_enabled = true; register_shutdown_function(array($this, 'shutdown_handler')); WPLA()->logger->info('running task: ' . print_r($task, 1)); // handle job name switch ($task['task']) { // update listing from Amazon (current used for new listings without ASIN) case 'updateProduct': // init $lm = new WPLA_ListingsModel(); $listing = $lm->getItem($task['id']); $account = WPLA_AmazonAccount::getAccount($listing['account_id']); $api = new WPLA_AmazonAPI($account->id); // get product attributes // $product = $api->getProduct( $listing['asin'] ); $result = $api->getMatchingProductForId($listing['sku'], 'SellerSKU'); // echo "<pre>";print_r($product);echo"</pre>";#die(); // echo "<pre>";print_r($product);echo"</pre>";die(); if ($result->success) { if (!empty($result->product->ASIN)) { // update listing attributes $listing_id = $listing['id']; // $lm->updateItemAttributes( $product, $listing_id ); // $listing = $lm->getItem( $listing_id ); // update values $lm->updateWhere(array('id' => $listing_id), array('asin' => $result->product->ASIN)); WPLA()->logger->info('new ASIN for listing #' . $listing['id'] . ': ' . $result->product->ASIN); // update product // $woo = new WPLA_ProductBuilder(); // $woo->updateProducts( array( $listing ) ); $success = true; $errors = ''; } else { $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']); $errors .= ' The product data received from Amazon was empty.'; $success = false; } } elseif ($result->Error->Message) { $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']) . '<br>Error: ' . $result->Error->Message; $success = false; } else { $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']); $success = false; } // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = empty($errors) ? array() : array(array('HtmlMessage' => $errors)); $response->success = $success; $this->returnJSON($response); exit; // create new WooCommerce product from imported listing // create new WooCommerce product from imported listing case 'createProduct': // init $lm = new WPLA_ListingsModel(); // $listing = $lm->getItem( $task['id'] ); $listing_id = $task['id']; // create product $ProductsImporter = new WPLA_ProductsImporter(); $success = $ProductsImporter->createProductFromAmazonListing($listing_id); $error = $ProductsImporter->lastError; $delay = $ProductsImporter->request_count * 1000; // ms // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = empty($error) ? array() : array(array('HtmlMessage' => $error)); $response->success = $success; $response->delay = $delay; $this->returnJSON($response); exit; // fetch full product description from Amazon and update WooCommerce product // fetch full product description from Amazon and update WooCommerce product case 'fetchFullProductDescription': $webHelper = new WPLA_AmazonWebHelper(); $webHelper->loadListingDetails($task['id']); // echo "<pre>";print_r($webHelper->images);echo"</pre>";#die(); $lm = new WPLA_ListingsModel(); $item = $lm->getItem($task['id']); if (!empty($webHelper->description)) { // update product $post_id = $item['post_id']; $post_data = array('ID' => $post_id, 'post_content' => trim($webHelper->description)); wp_update_post($post_data); $success = true; $errors = ''; } else { $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $item['asin']); $errors .= ' The product description received from Amazon was empty.'; $success = false; } // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = empty($errors) ? array() : array(array('HtmlMessage' => $errors)); $response->success = $success; $this->returnJSON($response); exit; // process Merchant or FBA Report and create / update listings // process Merchant or FBA Report and create / update listings case 'processReportPage': // process report page - both Merchant and FBA reports $response = WPLA_ImportHelper::ajax_processReportPage($job, $task); $this->returnJSON($response); exit; // process single row (SKU) Merchant or FBA Report - and create / update listings // process single row (SKU) Merchant or FBA Report - and create / update listings case 'processSingleSkuFromReport': // process report page - both Merchant and FBA reports $response = WPLA_ImportHelper::ajax_processReportPage($job, $task, true); $this->returnJSON($response); exit; default: // echo "unknown task"; // exit(); } }