public function checkSoldStock() { // get all sold listings $listings = WPLE_ListingQueryHelper::getAllWithStatus('sold'); $out_of_stock_products = array(); // process published listings foreach ($listings as $item) { // get wc product $_product = ProductWrapper::getProduct($item['post_id']); // check stock level // $stock = ProductWrapper::getStock( $item['post_id'] ); $stock = $_product ? $_product->get_total_stock() : 0; if ($stock == 0) { continue; } // mark listing as changed // if ( isset( $_REQUEST['mark_as_changed'] ) && $_REQUEST['mark_as_changed'] == 'yes' ) { // ListingsModel::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; $out_of_stock_products[] = $item; } // return if empty if (empty($out_of_stock_products)) { $this->showMessage('No sold products have stock in WooCommerce.', 0, 1); return; } $msg = '<p>'; $msg .= 'Warning: Some sold listings are still in stock in WooCommerce.'; $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'>eBay ID</th>"; $msg .= "<th style='text-align:left'>Ended at</th>"; $msg .= "<th style='text-align:left'>Status</th>"; $msg .= "</tr>"; // table rows foreach ($out_of_stock_products as $item) { // get column data $qty = $item['quantity'] - $item['quantity_sold']; $sku = get_post_meta($item['post_id'], '_sku', true); $stock = $item['stock'] . ' x '; $title = $item['auction_title']; $post_id = $item['post_id']; $ebay_id = $item['ebay_id']; $status = $item['status']; $exists = $item['exists']; $date_ended = $item['date_finished'] ? $item['date_finished'] : $item['end_date']; // build links $ebay_url = $item['ViewItemURL'] ? $item['ViewItemURL'] : ($ebay_url = 'http://www.ebay.com/itm/' . $ebay_id); $ebay_link = '<a href="' . $ebay_url . '" target="_blank">' . $ebay_id . '</a>'; $edit_link = '<a href="post.php?action=edit&post=' . $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>{$ebay_link}</td>"; $msg .= "<td>{$date_ended}</td>"; $msg .= "<td>{$status}</td>"; $msg .= "</tr>"; } $msg .= '</table>'; // show 'check again' button $msg .= '<p>'; $url = 'admin.php?page=wplister-tools&action=check_wc_sold_stock&_wpnonce=' . wp_create_nonce('e2e_tools_page'); $msg .= '<a href="' . $url . '" class="button">' . __('Check again', 'wplister') . '</a> '; $msg .= '</p>'; // $msg .= '<p>'; // $url = 'admin.php?page=wplister-tools&action=check_wc_out_of_stock&mark_as_changed=yes&_wpnonce='.wp_create_nonce('e2e_tools_page'); // $msg .= '<a href="'.$url.'" class="button">'.__('Mark all as changed','wplister').'</a> '; // $msg .= 'Click this button to mark all found listings as changed in WP-Lister, then revise all changed listings.'; // $msg .= '</p>'; $this->showMessage($msg, 1, 1); }