public function checkFBAStock($mode = 'in_stock_only') { // get all published listings $lm = new WPLA_ListingsModel(); $out_of_sync_products = array(); if ($mode == 'all_stock') { $listings = $lm->getAllItemsUsingFBA(); } else { $listings = $lm->getAllItemsWithStockInFBA(); } // process FBA listings foreach ($listings as $item) { // get wc product $item = (array) $item; $_product = $this->getProduct($item['post_id']); if (!$_product) { continue; } // 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']); if ($stock == $item['fba_quantity']) { continue; } // copy FBA qty to Woo if (isset($_REQUEST['wpla_copy_fba_qty_to_woo'])) { update_post_meta($item['post_id'], '_stock', $item['fba_quantity']); continue; } // add to list of out of stock products $item['stock'] = $stock; $item['parent_id'] = $_product->product_type == 'variation' ? $_product->parent->id : false; $out_of_sync_products[] = $item; } // return if empty if (empty($out_of_sync_products)) { WPLA()->showMessage('All FBA products are in sync with WooCommerce.', 0, 1); return; } $msg = '<p>'; $msg .= sprintf('There are %s FBA products have a different stock level in WooCommerce.', sizeof($out_of_sync_products)); $msg .= '</p>'; // table header $msg .= '<table style="width:100%">'; $msg .= "<tr>"; $msg .= "<th style='text-align:left'>SKU</th>"; $msg .= "<th style='text-align:left'>Product</th>"; $msg .= "<th style='text-align:left'>FBA</th>"; $msg .= "<th style='text-align:left'>WooCommerce</th>"; $msg .= "<th style='text-align:left'>ASIN</th>"; $msg .= "<th style='text-align:left'>Status</th>"; $msg .= "</tr>"; // table rows foreach ($out_of_sync_products as $item) { // get column data $sku = $item['sku']; $qty = $item['quantity']; $fba_qty = $item['fba_quantity']; $stock = $item['stock']; $title = $item['listing_title']; $post_id = $item['post_id']; $asin = $item['asin']; $status = $item['status']; // 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>'; // build table row $msg .= "<tr>"; $msg .= "<td>{$sku}</td>"; $msg .= "<td>{$edit_link}</td>"; $msg .= "<td>{$fba_qty}</td>"; $msg .= "<td>{$stock}</td>"; $msg .= "<td>{$amazon_link}</td>"; $msg .= "<td>{$status}</td>"; $msg .= "</tr>"; } $msg .= '</table>'; $msg .= '<p>'; $url = 'admin.php?page=wpla-tools&tab=inventory&action=check_wc_fba_stock&wpla_copy_fba_qty_to_woo=yes&mode=' . $mode . '&_wpnonce=' . wp_create_nonce('wpla_tools_page'); $msg .= '<a href="' . $url . '" class="button">' . __('Copy FBA quantity to WooCommerce', 'wpla') . '</a> '; $msg .= 'Click this button set the stock level in WooCommerce to the current FBA quantity for each found product.'; $msg .= '</p>'; WPLA()->showMessage($msg, 1, 1); }
public function insertMissingVariation($variation_id, $sku, $parent_listing) { global $wpdb; // get variation product data // $variation_id = $var['post_id']; // $variation_attributes = $var['variation_attributes']; $variable_product = get_product($variation_id); if (!$variable_product) { continue; } // echo "<pre>";print_r($var);echo"</pre>";#die(); // compile variation-theme from attribute names // $attribute_names = array_keys( $variation_attributes ); // $vtheme = join( '-', $attribute_names ); // generate title suffix from attribute values // $attribute_values = array_values( $variation_attributes ); // $suffix = join( ', ', $attribute_values ); // get attributes values and append to listing title $variation_attributes = $variable_product->get_variation_attributes(); $attribute_values = array_values($variation_attributes); $listing_title = $parent_listing->listing_title; $listing_title .= ' - ' . join(', ', $attribute_values); // handle custom listing title // $listing_title = $parent_listing->listing_title; // if ( $product_value = get_post_meta( $post_id, '_amazon_title', true ) ) // $listing_title = $product_value; // build single variation listing item $data = array(); $data['post_id'] = $variation_id; $data['parent_id'] = $parent_listing->post_id; $data['vtheme'] = $parent_listing->vtheme; $data['listing_title'] = $listing_title; $data['price'] = WPLA_ProductWrapper::getPrice($variation_id); $data['quantity'] = WPLA_ProductWrapper::getStock($variation_id); // $data['sku'] = WPLA_ProductWrapper::getSKU( $variation_id ); $data['sku'] = $sku; $data['date_created'] = date('Y-m-d H:i:s', time()); $data['status'] = 'prepared'; $data['source'] = 'woo'; $data['profile_id'] = $parent_listing->profile_id; $data['account_id'] = $parent_listing->account_id; $data['product_type'] = $variable_product->product_type; WPLA()->logger->info('insert new variation ' . $variation_id . ' - title: ' . $data['listing_title']); // insert in listings table $wpdb->insert($this->tablename, $data); echo $wpdb->last_error; // apply profile (price) $listing_id = $wpdb->insert_id; $profile = new WPLA_AmazonProfile($parent_listing->profile_id); if ($profile) { $this->applyProfileToItem($profile, $listing_id); } // return success $success = $listing_id ? true : false; return $success; }