public function importSingleProduct($item, $product_node) { $variation_type = is_string($product_node->variation_type) ? $product_node->variation_type : '_none_'; // convert empty object to string WPLA()->logger->info("* importSingleProduct() - SKU " . $item['sku'] . ' - type: ' . $variation_type); $lm = new WPLA_ListingsModel(); // // check if product already exists by ASIN (disabled) // $this->last_insert_id = $this->getProductIdByOriginalId( $item['asin'] ); // if ( $this->last_insert_id ) WPLA()->logger->info('found existing product by ASIN '.$item['asin'].' - post_id: '.$this->last_insert_id ); // if ( ! $this->last_insert_id ) { // $this->last_insert_id = self::getProductIdBySKU( $item['sku'] ); // if ( $this->last_insert_id ) { // update_post_meta( $this->last_insert_id, '_wpla_asin', $item['asin'] ); // WPLA()->logger->info('found existing product by SKU '.$item['sku'].'- post_id: '.$this->last_insert_id ); // } // } // check if product already exists - by SKU $this->last_insert_id = self::getProductIdBySKU($item['sku']); // if a product exists, update ASIN if ($this->last_insert_id) { update_post_meta($this->last_insert_id, '_wpla_asin', $item['asin']); WPLA()->logger->info('Found existing product by SKU ' . $item['sku'] . ' - post_id: ' . $this->last_insert_id); } // if no product exists, import if (!$this->last_insert_id) { $data = $this->mapAmazonListingToWoo($item, $product_node); $this->last_insert_id = $this->addProduct($data); } else { // if a parent variation was found, import missing child variations if ('parent' == $product_node->variation_type) { $data = $this->mapAmazonListingToWoo($item, $product_node); $post_id = $this->last_insert_id; // add each variation foreach ($data['variations'] as $variation) { // skip existing SKUs if ($variation_id = self::getProductIdBySKU($variation->sku)) { WPLA()->logger->info('skipped existing variation ' . $variation->sku . ' - parent_id: ' . $post_id); continue; } // create child variation $this->addVariation($post_id, $variation, $data); WPLA()->logger->info('ADDED missing variation ' . $variation->sku . ' - parent_id: ' . $post_id); } } // is parent variation } // product exists // update listing with new post_id $lm->updateListing($item['id'], array('post_id' => $this->last_insert_id, 'status' => $item['source'] == 'imported' ? 'online' : 'matched')); }
public function checkProductStock() { // get all published listings $lm = new WPLA_ListingsModel(); $listings = $lm->getWhere('status', 'online'); $out_of_stock_products = array(); // process published listings foreach ($listings as $item) { // get wc product $item = (array) $item; $_product = $this->getProduct($item['post_id']); // checking parent variations makes no sense in WPLA, so skip them if ($_product->product_type == 'variable') { continue; } // check stock level $stock = WPLA_ProductWrapper::getStock($item['post_id']); // $stock = $_product ? $_product->get_total_stock() : 0; if ($stock > 0) { continue; } if ($item['quantity'] == 0) { continue; } // mark listing as changed if (isset($_REQUEST['mark_as_changed'])) { $lm->updateListing($item['id'], array('status' => 'changed')); $item['status'] = 'changed'; } // add to list of out of stock products $item['stock'] = $stock; $item['exists'] = $_product ? true : false; $item['parent_id'] = $_product->product_type == 'variation' ? $_product->parent->id : false; $out_of_stock_products[] = $item; } // return if empty if (empty($out_of_stock_products)) { WPLA()->showMessage('No out of stock products found.', 0, 1); return; } $msg = '<p>'; $msg .= sprintf('Warning: %s published listings are out of stock or missing in WooCommerce.', sizeof($out_of_stock_products)); $msg .= '</p>'; // table header $msg .= '<table style="width:100%">'; $msg .= "<tr>"; $msg .= "<th style='text-align:left'>Stock</th>"; $msg .= "<th style='text-align:left'>SKU</th>"; $msg .= "<th style='text-align:left'>Product</th>"; $msg .= "<th style='text-align:left'>Qty</th>"; $msg .= "<th style='text-align:left'>ASIN</th>"; $msg .= "<th style='text-align:left'>Status</th>"; $msg .= "</tr>"; // table rows foreach ($out_of_stock_products as $item) { // get column data $sku = $item['sku']; $qty = $item['quantity']; $stock = $item['stock'] . ' x '; $title = $item['listing_title']; $post_id = $item['post_id']; $asin = $item['asin']; $status = $item['status']; $exists = $item['exists']; // build links // $amazon_url = $item['ViewItemURL'] ? $item['ViewItemURL'] : $amazon_url = 'http://www.amazon.com/itm/'.$asin; $amazon_url = 'admin.php?page=wpla&s=' . $asin; $amazon_link = '<a href="' . $amazon_url . '" target="_blank">' . $asin . '</a>'; $edit_link = '<a href="post.php?action=edit&post=' . ($item['parent_id'] ? $item['parent_id'] : $post_id) . '" target="_blank">' . $title . '</a>'; // mark non existent products if (!$exists) { $stock = 'N/A'; $post_id .= ' missing!'; } // build table row $msg .= "<tr>"; $msg .= "<td>{$stock}</td>"; $msg .= "<td>{$sku}</td>"; $msg .= "<td>{$edit_link} (ID {$post_id})</td>"; $msg .= "<td>{$qty} x </td>"; $msg .= "<td>{$amazon_link}</td>"; $msg .= "<td>{$status}</td>"; $msg .= "</tr>"; } $msg .= '</table>'; $msg .= '<p>'; $url = 'admin.php?page=wpla-tools&action=check_wc_out_of_stock&mark_as_changed=yes&_wpnonce=' . wp_create_nonce('wpla_tools_page'); $msg .= '<a href="' . $url . '" class="button">' . __('Mark all as changed', 'wpla') . '</a> '; $msg .= 'Click this button to mark all found listings as changed in WP-Lister.'; $msg .= '</p>'; WPLA()->showMessage($msg, 1, 1); }
public function fixNewVariationListings($variations, $parent_listing) { WPLA()->logger->info("fixNewVariationListings()"); $lm = new WPLA_ListingsModel(); // get post_id of parent which has been created by now // $parent_listing = $lm->getItemByASIN( $parent_listing->asin ); $parent_listing = $lm->getItemBySKU($parent_listing->sku); // catch Invalid argument error if (!is_array($variations)) { WPLA()->logger->error("no variations found for parent variable ASIN {$parent_listing->asin} (not checked)"); WPLA()->logger->error("no variations found for parent variable SKU {$parent_listing->sku}"); WPLA()->logger->error('variations:' . print_r($variations, 1)); // echo 'Error: no variations found for variable ASIN '.$parent_listing->asin.'<br>'; // echo "<pre>variations: ";print_r($variations);echo"</pre>";#die(); return; } foreach ($variations as $var) { $post_id = WPLA_ProductBuilder::getProductIdBySKU($var->sku); if (!$post_id) { WPLA()->logger->warn("fixing SKU {$var->sku} ... no product found for this SKU!!"); continue; } $data = array('post_id' => $post_id, 'parent_id' => $parent_listing->post_id); $lm->updateListing($var->listing_id, $data); WPLA()->logger->info("fixed SKU {$var->sku} - post_id: {$post_id}"); } }