Changeset 3045832
- Timestamp:
- 03/05/2024 04:20:16 PM (2 years ago)
- Location:
- midwest-logistics/trunk
- Files:
-
- 5 edited
-
classes/class-ml-order.php (modified) (1 diff)
-
classes/class-ml-stock.php (modified) (12 diffs)
-
inc/productFunctions.php (modified) (2 diffs)
-
midwest-wholesale.php (modified) (1 diff)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
midwest-logistics/trunk/classes/class-ml-order.php
r3008368 r3045832 246 246 $response = $response["response"]; 247 247 } 248 248 249 //add the log 249 250 midwest_logistics_add_communication_log($postString,$response,$order ->get_order_number(),"order",$CRMorder,$responseText); -
midwest-logistics/trunk/classes/class-ml-stock.php
r3035897 r3045832 2 2 class ML_Stock { 3 3 private $metaKeySKU = "_midwest_logistics_product_sku_text_field"; 4 private $metaStockUpdated = "_midwest_logistics_stock_updated"; 4 private $metaStockUpdated = "_midwest_logistics_stock_updated"; //used in product fucntions as well. 5 5 private $metaStockCheck = "_midwest_logistics_check_stock"; 6 6 private $metaStockCounterOption = "mw_stock_counter"; 7 7 public function __construct() { 8 /* 9 * How does stock work? 10 * IMPORTANT NOTE. If the product or variation does not have a price the product query does not return it. 11 * Basically it gets the products that have been marked to be shipped by Midwest. Limitied by the $productLimit varaible. 12 * It then loops though each producy and makes sure the product has a SKU. If it does it adds it to the list and marks meta metaStockUpdated data as Y 13 * It adds it to a list and send returns it to the function that called it. (get_inventory) 14 * That loops through each product found and combines them into a API call and makes the call 15 * After the call it loops through each result and begins updating the product with the correct SKU. 16 * It finds the product in the original list of products returned from the above steps . 17 * It then updates the stock and adds a log to the communication log. 18 * If no products are found then it runs the reset which resets all the product that are marked to be shipped by Midwest to "N" so that the cyle can begin again. 19 */ 20 8 21 //this must be set for the wp scheduler to work. 9 22 add_action( 'midwest_logistics_inventory_stock_update',array($this, 'get_inventory') ); … … 17 30 //Basically if no items are found then the option is incremented and then used to find products that have changed. 18 31 //The reset function allows us increment the option. 19 //delete_option( $this->metaStockCounterOption );20 $stock_counter = get_option($this->metaStockCounterOption,false);21 if($stock_counter == false) {22 $stock_counter = 1;23 }24 32 $pull_stock_setting = true; 25 33 … … 45 53 } 46 54 47 $args = array(48 'status' => 'publish',49 'limit' => $productLimit,50 'type' => array('simple','variable'),51 '_midwest_logistics_check_stock_not_exists' => ''52 );53 $newProducts = wc_get_products($args);54 if(count($newProducts) > 0) {55 foreach($newProducts as $newProduct) {56 $checkStock = "N";57 if ( $newProduct->is_type( 'variable' ) ) {58 $variations = $newProduct->get_available_variations();59 if(count($variations) > 0) {60 foreach($variations as $variation) {61 $variableProduct = wc_get_product($variation["variation_id"]);62 if($variableProduct != null) {63 $midwestSKU = $variableProduct->get_meta($this->metaKeySKU);64 if($midwestSKU != "") {65 $checkStock = "Y";66 break;67 }68 }69 }70 }71 } else {72 $midwestSKU = $newProduct->get_meta($this->metaKeySKU);73 if($midwestSKU != "") {74 $checkStock = "Y";75 }76 }77 $newProduct->update_meta_data($this->metaStockCheck,$checkStock);78 $newProduct->save_meta_data();79 }80 81 }82 83 55 $skuArray = []; 84 56 /* First we check if there are products we have never checked. If so we do those first. … … 91 63 'limit' => $productLimit, 92 64 'type' => array('simple','variable'), 93 $this->metaStockUpdated => $stock_counter,65 $this->metaStockUpdated => "Y", 94 66 $this->metaStockCheck => 'Y' 95 67 ); 96 68 97 69 $products = wc_get_products($args); 98 70 if(count($products) == 0) { 71 //we must have updated them all so reset the field 72 $this->reset(); 73 } 99 74 if(count($products) > 0) { 100 foreach($products as $product) { 101 $metaStockUpdatedDB = $product->get_meta($this->metaStockUpdated); 102 if ( $product->is_type( 'variable' ) ) { 103 $variations = $product->get_available_variations(); 104 if(count($variations) > 0) { 105 foreach($variations as $variation) { 106 $variableProduct = wc_get_product($variation["variation_id"]); 107 if($variableProduct != null) { 108 $id = $variableProduct->get_id(); 109 $midwestSKU = $variableProduct->get_meta($this->metaKeySKU); 110 if($midwestSKU != "") { 111 array_push($skuArray,array( 112 "post_id" => $id, 113 "midwestSKU" => $midwestSKU, 114 "parent_id" => $variableProduct->get_parent_id() 115 ) 116 ); 117 } 118 } 119 } 120 } 121 } else { 122 if($product != null) { 123 $id = $product->get_id(); 124 $midwestSKU = $product->get_meta($this->metaKeySKU); 125 //echo "id=" . $id . "<br />"; 126 //echo "metaStockUpdatedDB=" . $metaStockUpdatedDB . "<br /><br /><br />"; 127 if($midwestSKU != "") { 128 array_push($skuArray,array( 129 "post_id" => $id, 130 "midwestSKU" => $midwestSKU, 131 "parent_id" => $id 132 ) 133 ); 134 } else { 135 $product->update_meta_data($this->metaStockUpdated,$stock_counter); 136 $product->save_meta_data(); 137 } 138 } 139 } 140 } 141 } 142 75 foreach($products as $product) { 76 //$product->update_meta_data('_midwest_logistics_stock_updated', "Y" ); 77 //$product->save_meta_data(); 78 $updated = update_post_meta($product->get_id(),"_midwest_logistics_stock_updated","Y"); //using the Wordpress meta table becase that is where the prodcut meta is stored. 79 if ($product->is_type('variable')) { 80 $variations = $product->get_available_variations(); 81 foreach($variations as $variation) { 82 $variableProduct = wc_get_product($variation["variation_id"]); 83 if($variableProduct != null) { 84 85 $id = $variableProduct->get_id(); 86 $midwestSKU = $variableProduct->get_meta($this->metaKeySKU); 87 if($midwestSKU != "") { 88 array_push($skuArray, array( 89 "post_id" => $id, 90 "midwestSKU" => $midwestSKU, 91 "parent_id" => $variableProduct->get_parent_id() 92 )); 93 } 94 } 95 } 96 97 } else { 98 $id = $product->get_id(); 99 $midwestSKU = $product->get_meta($this->metaKeySKU); 100 if($midwestSKU != "") { 101 array_push($skuArray,array( 102 "post_id" => $id, 103 "midwestSKU" => $midwestSKU, 104 "parent_id" => $id 105 )); 106 } 107 108 } 109 } 110 } 111 143 112 return $skuArray; 144 113 … … 200 169 $skusArray = array(); 201 170 $skusJSONArray = array(); 202 if(count($skusResults) == 0) { 203 //we must have updated them all so reset the field 204 $this->reset(); 205 $skusResults = $this->get_SKUs(); 206 } 171 207 172 if($debugout == true) { 208 $stock_counter = get_option($this->metaStockCounterOption,1);209 173 echo "Products being ran: <br>"; 210 174 var_dump($skusResults); 211 175 echo "<br><br><br>"; 212 echo "Stock Counter Number: " . $stock_counter . " <br>"; 213 echo "<br><br><br>"; 214 } 176 } 177 215 178 if(count($skusResults) > 0) { 216 $stock_counter = get_option($this->metaStockCounterOption,1); 217 179 218 180 foreach ($skusResults as $singleSKU) { 219 181 $skus .= $singleSKU["midwestSKU"] . ","; … … 226 188 $product = wc_get_product($parentId); 227 189 228 $product->update_meta_data($this->metaStockUpdated,$stock_counter);229 $product->save_meta_data();230 190 } 231 191 } … … 263 223 } 264 224 if($httpcode == "200") { 225 265 226 if(json_decode($response,true) !== null) { 266 227 $jsonResponse = json_decode($response,true); 267 228 $curl_result = $jsonResponse["result"]; 268 229 $products = $jsonResponse["products"]; 230 231 269 232 if(is_array($products)) { 233 270 234 foreach($products as $product) { 271 235 $stockAmount = $product["stock"]; … … 276 240 if(isset($skusArray[$sku])) { 277 241 $productId = $skusArray[$sku]["id"]; 278 242 279 243 //Update the product. If it a variation thats okay. 280 wc_update_product_stock($productId,$stockAmount,'set',false); //It updates later on. 281 244 $currentProduct = wc_get_product($productId); 245 if($currentProduct != null) { 246 wc_update_product_stock($currentProduct,$stockAmount,'set',false); //It updates later on. 247 } 282 248 if($debugout == true) { 283 249 echo "Product " . $productId . " stock updated to " . $stockAmount ,"<br>"; … … 295 261 midwest_logistics_add_communication_log($postString,json_encode($responseText),$productId,"product","stock_update", json_encode($responseText)); 296 262 297 298 299 300 263 } 301 264 } … … 304 267 } 305 268 } 306 307 308 269 309 270 if($httpcode == "500") { 310 271 new WP_Error( MIDWESTLOGISTICS_NAME . ' API',MIDWESTLOGISTICS_NAME . " API is down." ); … … 352 313 353 314 } 354 355 315 return $wp_query_args; 356 316 } 357 317 private function reset() { 358 $stock_counter = get_option($this->metaStockCounterOption,1); 359 $stock_counter = $stock_counter + 1; 360 if($stock_counter > 1000) { 361 $stock_counter = 1; //reset 362 } 363 update_option($this->metaStockCounterOption,$stock_counter); 318 $args = array( 319 'status' => 'publish', 320 'limit' => -1, 321 'type' => array('simple','variable'), 322 $this->metaStockCheck => 'Y' 323 ); 324 $products = wc_get_products($args); 325 if(count($products) > 0) { 326 foreach($products as $product) { 327 update_post_meta($product->get_id(),$this->metaStockUpdated,"N"); //using the Wordpress meta table becase that is where the prodcut meta is stored. 328 } 329 } 364 330 365 331 } … … 368 334 $ML_Stock = new ML_Stock(); 369 335 370 add_action( 'init', 'mw_stock_manual_run' ); 371 function mw_stock_manual_run() { 372 $run_mw_stock = filter_input( INPUT_GET, 'run_mw_stock', FILTER_SANITIZE_URL ); 373 if(isset($run_mw_stock)) { 374 if($run_mw_stock == "Y") { 375 $ML_Stock = new ML_Stock(); 376 $ML_Stock->get_inventory(true); 377 378 } 379 } 336 add_action( 'wp_loaded', 'mw_stock_manual_run' ); 337 function mw_stock_manual_run() { 338 if(is_admin()) { 339 $run_mw_stock = filter_input( INPUT_GET, 'run_mw_stock', FILTER_SANITIZE_URL ); 340 if(isset($run_mw_stock)) { 341 if($run_mw_stock == "Y") { 342 $ML_Stock = new ML_Stock(); 343 $ML_Stock->get_inventory(true); 344 345 } 346 } 347 } 348 380 349 381 350 -
midwest-logistics/trunk/inc/productFunctions.php
r2953869 r3045832 105 105 } 106 106 $product->save_meta_data(); 107 108 $parentProduct = wc_get_product($product->get_parent_id()); 109 if($parentProduct != null) { 110 $product->update_meta_data('_midwest_logistics_check_stock', "N" ); 111 if($midwest_logistics_product_select == "Y") { 112 $parentProduct->update_meta_data('_midwest_logistics_check_stock', "Y" ); 113 114 } else { 115 $parentProduct->update_meta_data('_midwest_logistics_check_stock', "N" ); 116 } 117 $parentProduct->save_meta_data(); 118 } 107 119 } 108 120 } … … 110 122 } 111 123 // Save Fields 112 add_action( 'save_post', 'midwest_logistics_woocommerce_fields_save' ); 113 function midwest_logistics_woocommerce_fields_save($post_id) { 114 if(isset($_POST)) { 124 //add_action( 'save_post', 'midwest_logistics_woocommerce_fields_save' ); 125 add_action( 'woocommerce_new_product', 'midwest_logistics_woocommerce_fields_save', 10, 1 ); 126 add_action( 'woocommerce_update_product', 'midwest_logistics_woocommerce_fields_save', 10, 1 ); 127 function midwest_logistics_woocommerce_fields_save($product_id) { 128 //only update if they posted a save. not if the product was saved in another way. 129 if(isset($_POST)) { 115 130 $midwest_logistics_product_select = !empty( $_POST['_midwest_logistics_product_select'] ) ? sanitize_text_field($_POST['_midwest_logistics_product_select']) : ""; 131 116 132 if( $midwest_logistics_product_select !== "" ) { 117 $product = wc_get_product($post_id); 118 if($product != null) { 119 if($midwest_logistics_product_select == "Y") { 120 // Text Field 121 $midwest_logistics_product_sku_text_field = sanitize_text_field($_POST['_midwest_logistics_product_sku_text_field']); 122 $product->update_meta_data('_midwest_logistics_product_sku_text_field', esc_attr( $midwest_logistics_product_sku_text_field ) ); 123 $product->update_meta_data('_midwest_logistics_product_select', "Y" ); 124 $product->update_meta_data('_midwest_logistics_check_stock', "Y" ); 133 $product = wc_get_product( $product_id ); 134 135 if($product != null) { 136 if ($product->is_type( 'variable' )) { 137 138 $setProductasStockUpdate = false; 139 $variations = $product->get_available_variations(); 140 if(count($variations) > 0) { 141 foreach($variations as $variation) { 142 $variableProduct = wc_get_product($variation["variation_id"]); 143 if($variableProduct != null) { 144 $id = $variableProduct->get_id(); 145 $midwestSKU = $variableProduct->get_meta("_midwest_logistics_product_sku_text_field"); 146 if($midwestSKU != "" && $setProductasStockUpdate == false) { 147 //set it to be checked by Midwest 148 $setProductasStockUpdate = true; 149 } 150 } 151 } 152 } 153 154 if($setProductasStockUpdate == true) { 155 $product->update_meta_data('_midwest_logistics_check_stock', "Y" ); 156 $product->update_meta_data('_midwest_logistics_stock_updated', "N" ); //sets the stock to be udpated on the next run 157 $product->save_meta_data(); 158 } 159 125 160 } else { 126 $product->update_meta_data('_midwest_logistics_product_select', "N" ); 127 $product->delete_meta_data('_midwest_logistics_product_sku_text_field'); 128 $product->update_meta_data('_midwest_logistics_check_stock', "N" ); 129 } 130 $product->save_meta_data(); 161 if($midwest_logistics_product_select == "Y") { 162 // Text Field 163 $midwest_logistics_product_sku_text_field = sanitize_text_field($_POST['_midwest_logistics_product_sku_text_field']); 164 $product->update_meta_data('_midwest_logistics_product_sku_text_field', esc_attr( $midwest_logistics_product_sku_text_field ) ); 165 $product->update_meta_data('_midwest_logistics_product_select', "Y" ); 166 $product->update_meta_data('_midwest_logistics_check_stock', "Y" ); 167 } else { 168 $product->update_meta_data('_midwest_logistics_product_select', "N" ); 169 $product->delete_meta_data('_midwest_logistics_product_sku_text_field'); 170 $product->update_meta_data('_midwest_logistics_check_stock', "N" ); 171 } 172 173 $product->update_meta_data('_midwest_logistics_stock_updated', "N" ); //sets the stock to be udpated on the next run 174 $product->save_meta_data(); 175 } 131 176 } 132 133 } 134 } 177 } 178 179 180 } 181 135 182 } 136 183 -
midwest-logistics/trunk/midwest-wholesale.php
r3035897 r3045832 4 4 * Plugin URI: https://plugins.skynet-solutions.net/ 5 5 * Description: Midwest Wholesale Plugin allows you to automatically add Woocommerce orders into the Midwest Logistics order system. 6 * Version: 1.1.2 46 * Version: 1.1.25 7 7 * WC requires at least: 6.0.0 8 8 * WC tested up to: 8.2.0 -
midwest-logistics/trunk/readme.txt
r3035897 r3045832 5 5 Tested up to: 6.4.1 6 6 Requires PHP: 7.0 7 Stable tag: 1.1.2 47 Stable tag: 1.1.25 8 8 License: GPLv3 9 9 … … 75 75 76 76 == Changelog == 77 = 1.1.25 (2024-02-21) = 78 * Update - updated stock to improve stock checking for new products or existing products when a customer first installs the plugin. 79 77 80 = 1.1.22 (2024-01-17) = 78 81 * Update - removed customer id from settings. It is no longer need with our API.
Note: See TracChangeset
for help on using the changeset viewer.