public function checkProductInventory($mode = 'published', $compare_prices = false, $step = 0)
 {
     $batch_size = get_option('wplister_inventory_check_batch_size', 200);
     $limit = $batch_size;
     $offset = $batch_size * $step;
     // get listings - or return false
     $lm = new ListingsModel();
     $listings = $mode == 'published' ? WPLE_ListingQueryHelper::getAllPublished($limit, $offset) : WPLE_ListingQueryHelper::getAllEnded($limit, $offset);
     if (empty($listings)) {
         return false;
     }
     // restore previous data
     $tmp_result = get_option('wple_inventory_check_queue_data', false);
     if ($tmp_result) {
         $out_of_sync_products = $tmp_result['out_of_sync_products'];
         $published_count = $tmp_result['published_count'];
     } else {
         $out_of_sync_products = array();
         $published_count = 0;
     }
     // process published listings
     foreach ($listings as $item) {
         // check wc product
         $post_id = $item['post_id'];
         $_product = ProductWrapper::getProduct($post_id);
         // echo "<pre>";print_r($_product);echo"</pre>";die();
         // get stock level and price
         $stock = ProductWrapper::getStock($item['post_id']);
         $price = ProductWrapper::getPrice($item['post_id']);
         // $item['price_max'] = $price;
         // echo "<pre>";print_r($price);echo"</pre>";#die();
         // echo "<pre>";print_r($item);echo"</pre>";die();
         // apply profile settings to stock level
         $profile_data = ListingsModel::decodeObject($item['profile_data'], true);
         $profile_details = $profile_data['details'];
         $item['qty'] = $item['quantity'] - $item['quantity_sold'];
         // echo "<pre>";print_r($profile_details);echo"</pre>";#die();
         // apply max_quantity from profile
         $max_quantity = isset($profile_details['max_quantity']) && intval($profile_details['max_quantity']) > 0 ? $profile_details['max_quantity'] : false;
         if ($max_quantity) {
             $stock = min($max_quantity, intval($stock));
         }
         // apply price modified from profile
         $profile_start_price = isset($profile_details['start_price']) && !empty($profile_details['start_price']) ? $profile_details['start_price'] : false;
         if ($profile_start_price) {
             // echo "<pre>price: ";print_r($profile_start_price);echo"</pre>";#die();
         }
         // check if product has variations
         if ($_product) {
             $variations = $_product->product_type == 'variable' ? ProductWrapper::getVariations($item['post_id']) : array();
         } else {
             $variations = array();
         }
         // get total stock for all variations
         if (!empty($variations)) {
             // reset prices and stock
             $stock = 0;
             $price_min = PHP_INT_MAX;
             $price_max = 0;
             $ebay_stock = 0;
             $ebay_price_min = PHP_INT_MAX;
             $ebay_price_max = 0;
             // check WooCommerce variations
             foreach ($variations as $var) {
                 // total stock
                 if ($max_quantity) {
                     $stock += min($max_quantity, intval($var['stock']));
                 } else {
                     $stock += $var['stock'];
                 }
                 // min / max prices
                 $price_min = min($price_min, $var['price']);
                 $price_max = max($price_max, $var['price']);
             }
             // check eBay variations
             $cached_variations = maybe_unserialize($item['variations']);
             if (is_array($cached_variations)) {
                 foreach ($cached_variations as $var) {
                     $ebay_stock += $var['stock'];
                     $ebay_price_min = min($ebay_price_min, $var['price']);
                     $ebay_price_max = max($ebay_price_max, $var['price']);
                 }
             }
             // set default values
             $item['qty'] = $ebay_stock;
             $item['price'] = $ebay_price_min != PHP_INT_MAX ? $ebay_price_min : 0;
             $item['price_max'] = $ebay_price_max;
             // echo "<pre>";print_r($cached_variations);echo"</pre>";die();
         } else {
             $price_min = false;
             $price_max = false;
             $ebay_price_min = false;
             $ebay_price_max = false;
         }
         // check if product and ebay listing are in sync
         $in_sync = true;
         // check stock level
         if ($stock != $item['qty']) {
             $in_sync = false;
         }
         // check price
         if ($compare_prices) {
             $price_to_compare = $price;
             if ($profile_start_price) {
                 $price_to_compare = ListingsModel::applyProfilePrice($price, $profile_start_price);
             }
             if (round($price_to_compare, 2) != round($item['price'], 2)) {
                 $in_sync = false;
             }
             // check max price
             if (isset($price_max) && isset($item['price_max']) && round($price_max, 2) != round($item['price_max'], 2)) {
                 $in_sync = false;
             }
         }
         // if in sync, continue with next item
         if ($in_sync) {
             continue;
         }
         // mark listing as changed
         if (isset($_REQUEST['mark_as_changed']) && $_REQUEST['mark_as_changed'] == 'yes') {
             if ($_product) {
                 // only existing products can have a profile re-applied
                 $lm->markItemAsModified($item['post_id']);
             }
             // in case the product is locked or missing, force the listing to be changed
             ListingsModel::updateListing($item['id'], array('status' => 'changed'));
             $item['status'] = 'changed';
         }
         // remove unneccessary data to consume less memory - doesn't seem to work...
         // unset( $item['profile_data'] );
         // unset( $item['post_content'] );
         // unset( $item['details'] );
         // unset( $item['variations'] );
         // unset( $item['last_errors'] );
         // unset( $item['history'] );
         // unset( $item['eps'] );
         // unset( $item['template'] );
         // add to list of out of sync products
         $item['price_woo'] = $price;
         $item['price_woo_max'] = isset($price_max) ? $price_max : false;
         $item['stock'] = $stock;
         $item['exists'] = $_product ? true : false;
         $item['type'] = $_product ? $_product->product_type : 'missing';
         $item['profile_start_price'] = $profile_start_price;
         $out_of_sync_products[] = $item;
         // count products which have not yet been marked as changed
         if ($item['status'] == 'published') {
             $published_count += 1;
         }
     }
     // store result so far
     $tmp_result = array('mode' => $mode, 'compare_prices' => $compare_prices, 'out_of_sync_products' => $out_of_sync_products, 'published_count' => $published_count);
     update_option('wple_inventory_check_queue_data', $tmp_result, 'no');
     // true means we processed more items
     return true;
 }
Exemple #2
0
 function wpla_inventory_status_changed($post_id)
 {
     // re-apply profile to update ebay_auctions table
     $lm = new ListingsModel();
     $listing_id = $lm->markItemAsModified($post_id);
     // $this->wplister_revise_inventory_status( $post_id );
     do_action('wplister_revise_inventory_status', $post_id);
 }
 function wplister_product_handle_submitbox_actions($post_id, $post)
 {
     if (isset($_POST['wpl_ebay_revise_on_update'])) {
         // call markItemAsModified() to re-apply the listing profile
         $lm = new ListingsModel();
         $lm->markItemAsModified($post_id);
         WPLE()->logger->info('revising listing ' . $_POST['wpl_ebay_listing_id']);
         // call EbayController
         WPLE()->initEC();
         $results = WPLE()->EC->reviseItems($_POST['wpl_ebay_listing_id']);
         WPLE()->EC->closeEbay();
         WPLE()->logger->info('revised listing ' . $_POST['wpl_ebay_listing_id']);
         // $message = __('Selected items were revised on eBay.', 'wplister');
         // $message .= ' ID: '.$_POST['wpl_ebay_listing_id'];
         // $class = (false) ? 'error' : 'updated';
         // echo '<div id="message" class="'.$class.'" style="display:block !important"><p>'.$message.'</p></div>';
     }
     if (isset($_POST['wpl_ebay_relist_on_update'])) {
         // call markItemAsModified() to re-apply the listing profile
         $lm = new ListingsModel();
         $lm->markItemAsModified($post_id);
         WPLE()->logger->info('relisting listing ' . $_POST['wpl_ebay_listing_id']);
         // call EbayController
         WPLE()->initEC();
         $results = WPLE()->EC->relistItems($_POST['wpl_ebay_listing_id']);
         WPLE()->EC->closeEbay();
         WPLE()->logger->info('relisted listing ' . $_POST['wpl_ebay_listing_id']);
         // $message = __('Selected items were revised on eBay.', 'wplister');
         // $message .= ' ID: '.$_POST['wpl_ebay_listing_id'];
         // $class = (false) ? 'error' : 'updated';
         // echo '<div id="message" class="'.$class.'" style="display:block !important"><p>'.$message.'</p></div>';
     }
 }