Plugin Directory

Changeset 3045832


Ignore:
Timestamp:
03/05/2024 04:20:16 PM (2 years ago)
Author:
skynetsolutions
Message:

1.1.25 - Updated stock to track new products better and ensure stock options get setup properly.

Location:
midwest-logistics/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • midwest-logistics/trunk/classes/class-ml-order.php

    r3008368 r3045832  
    246246                    $response = $response["response"];
    247247                }
     248
    248249                //add the log
    249250                midwest_logistics_add_communication_log($postString,$response,$order ->get_order_number(),"order",$CRMorder,$responseText);
  • midwest-logistics/trunk/classes/class-ml-stock.php

    r3035897 r3045832  
    22class ML_Stock {
    33    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.
    55    private $metaStockCheck = "_midwest_logistics_check_stock";
    66    private $metaStockCounterOption = "mw_stock_counter";
    77    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       
    821        //this must be set for the wp scheduler to work.
    922        add_action( 'midwest_logistics_inventory_stock_update',array($this, 'get_inventory') );
     
    1730        //Basically if no items are found then the option is incremented and then used to find products that have changed.
    1831        //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         }
    2432        $pull_stock_setting = true;
    2533       
     
    4553        }
    4654
    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      
    8355        $skuArray = [];
    8456        /* First we check if there are products we have never checked. If so we do those first.
     
    9163            'limit' => $productLimit,
    9264            'type' => array('simple','variable'),
    93             $this->metaStockUpdated => $stock_counter,
     65            $this->metaStockUpdated => "Y",
    9466            $this->metaStockCheck => 'Y'
    9567        );
    9668
    9769        $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        }
    9974        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
    143112        return $skuArray;
    144113
     
    200169        $skusArray = array();
    201170        $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       
    207172        if($debugout == true) {
    208             $stock_counter = get_option($this->metaStockCounterOption,1);
    209173            echo "Products being ran: <br>";
    210174            var_dump($skusResults);
    211175            echo "<br><br><br>";
    212             echo "Stock Counter Number: " . $stock_counter . " <br>";
    213             echo "<br><br><br>";
    214         }
     176        }
     177
    215178        if(count($skusResults) > 0) {
    216             $stock_counter = get_option($this->metaStockCounterOption,1);
    217            
     179
    218180            foreach ($skusResults as $singleSKU) {               
    219181                $skus .= $singleSKU["midwestSKU"] . ",";
     
    226188                $product = wc_get_product($parentId);
    227189             
    228                 $product->update_meta_data($this->metaStockUpdated,$stock_counter);
    229                 $product->save_meta_data();
    230190            }
    231191        }
     
    263223            }
    264224            if($httpcode == "200") {
     225
    265226                if(json_decode($response,true) !== null) {
    266227                    $jsonResponse = json_decode($response,true);
    267228                    $curl_result = $jsonResponse["result"]; 
    268229                    $products = $jsonResponse["products"]; 
     230                   
     231     
    269232                    if(is_array($products)) {
     233                       
    270234                        foreach($products as $product) {
    271235                            $stockAmount = $product["stock"];
     
    276240                                if(isset($skusArray[$sku])) {
    277241                                    $productId = $skusArray[$sku]["id"];
    278              
     242
    279243                                    //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                                    }
    282248                                    if($debugout == true) {
    283249                                        echo "Product " . $productId . " stock updated to " . $stockAmount ,"<br>";
     
    295261                                    midwest_logistics_add_communication_log($postString,json_encode($responseText),$productId,"product","stock_update", json_encode($responseText));
    296262                                   
    297 
    298                                    
    299      
    300263                                }                                                               
    301264                            }                                                                   
     
    304267                }
    305268            }
    306            
    307            
    308          
     269
    309270            if($httpcode == "500") {
    310271                new WP_Error(  MIDWESTLOGISTICS_NAME . ' API',MIDWESTLOGISTICS_NAME . " API is down." );
     
    352313
    353314        }
    354 
    355315        return $wp_query_args;
    356316    }
    357317    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        }
    364330
    365331    }
     
    368334$ML_Stock = new ML_Stock();
    369335
    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     }
     336add_action( 'wp_loaded', 'mw_stock_manual_run' );
     337function 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   
    380349   
    381350   
  • midwest-logistics/trunk/inc/productFunctions.php

    r2953869 r3045832  
    105105                }
    106106                $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                }
    107119            }
    108120        }         
     
    110122}
    111123// 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' );
     125add_action( 'woocommerce_new_product', 'midwest_logistics_woocommerce_fields_save', 10, 1 );
     126add_action( 'woocommerce_update_product', 'midwest_logistics_woocommerce_fields_save', 10, 1 );
     127function 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)) {
    115130        $midwest_logistics_product_select = !empty( $_POST['_midwest_logistics_product_select'] ) ? sanitize_text_field($_POST['_midwest_logistics_product_select']) : "";
     131
    116132        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
    125160                } 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                }
    131176            }
    132 
    133         }   
    134     }
     177        }
     178       
     179       
     180    }
     181   
    135182}
    136183
  • midwest-logistics/trunk/midwest-wholesale.php

    r3035897 r3045832  
    44    * Plugin URI:  https://plugins.skynet-solutions.net/
    55    * Description: Midwest Wholesale Plugin allows you to automatically add Woocommerce orders into the Midwest Logistics order system.
    6     * Version:     1.1.24
     6    * Version:     1.1.25
    77    * WC requires at least: 6.0.0
    88    * WC tested up to: 8.2.0
  • midwest-logistics/trunk/readme.txt

    r3035897 r3045832  
    55Tested up to: 6.4.1
    66Requires PHP: 7.0
    7 Stable tag: 1.1.24
     7Stable tag: 1.1.25
    88License: GPLv3
    99
     
    7575
    7676== 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
    7780= 1.1.22 (2024-01-17) =
    7881* Update - removed customer id from settings. It is no longer need with our API.
Note: See TracChangeset for help on using the changeset viewer.