Plugin Directory

Changeset 2494428


Ignore:
Timestamp:
03/12/2021 05:59:31 PM (5 years ago)
Author:
innocow
Message:

Fix: unmanaged inventory.

Location:
stock-tracking-reporting-for-woocommerce/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • stock-tracking-reporting-for-woocommerce/trunk/README.txt

    r2494367 r2494428  
    11=== Stock Tracking & Reporting for Woocommerce ===
    2 Stable tag: 1.1.2
     2Stable tag: 1.1.3
    33Contributors: innocow
    44Donate link: http://innocow.com/tipjar
     
    5353== Changelog ==
    5454
     55= 1.1.3 =
     56* Fixed activation and tracking bugs with unmanaged inventory items.
     57
    5558= 1.1.2 =
    5659* Tested to Woocommere 5.1.0
     
    6467== Upgrade notice ==
    6568
     69= 1.1.3 =
     70* Fixed activation and tracking bugs with unmanaged inventory items.
     71
    6672= 1.1.2 =
    6773* Tested to Woocommere 5.1.0
  • stock-tracking-reporting-for-woocommerce/trunk/src/hooks/rest_resources.php

    r2477174 r2494428  
    4949            && intval( $array_query_params["all"] ) === 1 ) {
    5050
    51                 $Loop = new \WP_Query(
    52                     array(
    53                         'post_type' => array( 'product', 'product_variation' ),
    54                         'posts_per_page' => -1
    55                     )
    56                 );
     51                $product_ids = $Inventory->get_products_as_ids_all();
     52               
     53                foreach( $product_ids as $product_id ) {
    5754
    58                 if ( $Loop->have_posts() ) {
    59 
    60                     while ( $Loop->have_posts() ) {
    61 
    62                         $Loop->the_post();
    63                         $WC_Product = new \WC_Product( get_the_id() );
    64                         $products_array[] = $Inventory->wc_product_to_array( $WC_Product );
    65 
    66                     }
     55                    $WC_Product = new \WC_Product( $product_id );
     56                    $products_array[] = $Inventory->wc_product_to_array( $WC_Product );
    6757
    6858                }
     
    8575                $array_filters = [
    8676                    "limit" => 10,
    87                     "orderby" => "id",
    88                     "order" => "DESC",
    8977                ];
    9078
    91                 $array_of_wc_products_by_title = wc_get_products(
     79                $array_of_wc_products_by_title = $Inventory->get_products(
    9280                    array_merge( $array_filters, [ "s" => $safe_s ] )
    9381                );
     
    9785                );
    9886
    99                 $array_of_wc_products_by_sku = wc_get_products(
     87                $array_of_wc_products_by_sku = $Inventory->get_products(
    10088                    array_merge( $array_filters, [ "sku" => $safe_s ] )
    10189                );
  • stock-tracking-reporting-for-woocommerce/trunk/src/services/activation.php

    r2477174 r2494428  
    1414use Innocow\Stock_Records\Models\Transaction\Transaction;
    1515use Innocow\Stock_Records\Models\Transaction\Transaction_Search;
     16use Innocow\Stock_Records\Services\Inventory;
    1617use Innocow\Stock_Records\Services\Storage;
    1718use Innocow\Stock_Records\Services\WP_Options;
     
    6364        $tr_key = $Stock_Records->get_translation_key();
    6465
    65         $Loop = new \WP_Query(
    66             array(
    67                 'post_type' => array( 'product', 'product_variation' ),
    68                 'posts_per_page' => -1
    69             )
     66        $Inventory = new Inventory();
     67        $product_ids = $Inventory->get_products_as_ids_all();
    7068
    71         );
     69        foreach( $product_ids as $product_id ) {
    7270
    73         if ( $Loop->have_posts() ) {
     71            // Get the product quantity from WooCommerce.
     72            $Product = new \WC_Product( $product_id );
     73            $stock_quantity = $Product->get_stock_quantity();
    7474
    75             while ( $Loop->have_posts() ) {
     75            $Activation_Transaction = new Transaction();
     76            $Activation_Transaction->set_product_id( $product_id );
     77            $Activation_Transaction->set_timestamp_created_as_dt( new \DateTime("now") );
    7678
    77                 // Step through $loop array.
    78                 $Loop->the_post();
    79                
    80                 // Get the id.
    81                 $product_id = get_the_id();
     79            // The following logic will attempt to get the status of the products in
     80            // the transaction table. If there are no records (first activation), it
     81            // will create the initial transactions for recording. If not, check if
     82            // the stock quantities are different, and if they are, add adjustments
     83            // so that they are synchronised.
    8284
    83                 // Get the product quantity from WooCommerce.
    84                 $Product = new \WC_Product( $product_id );
    85                 $stock_quantity = $Product->get_stock_quantity();
     85            $Transaction_Search = new Transaction_Search();
     86            $Transaction_Search->set_product_id( $product_id );
    8687
    87                 $Activation_Transaction = new Transaction();
    88                 $Activation_Transaction->set_product_id( $product_id );;
    89                 $Activation_Transaction->set_timestamp_created_as_dt( new \DateTime("now") );
     88            $Storage = new Storage();
     89            $Transactions = $Storage->search( $Transaction_Search );
    9090
    91                 // The following logic will attempt to get the status of the products in
    92                 // the transaction table. If there are no records (first activation), it
    93                 // will create the initial transactions for recording. If not, check if
    94                 // the stock quantities are different, and if they are, add adjustments
    95                 // so that they are synchronised.
     91            if ( $Transactions->is_initial_transaction() ) {
    9692
    97                 $Transaction_Search = new Transaction_Search();
    98                 $Transaction_Search->set_product_id( $product_id );
     93                $Activation_Transaction->set_note(
     94                    __( "Balance on activation.", $tr_key )
     95                 );
     96                $Activation_Transaction->set_debit( $stock_quantity );
     97                $Activation_Transaction->is_hidden( true );
     98                $Storage->create( $Activation_Transaction );
    9999
    100                 $Storage = new Storage();
    101                 $Transactions = $Storage->search( $Transaction_Search );
     100            } else {
    102101
    103                 if ( $Transactions->is_initial_transaction() ) {
     102                if ( $stock_quantity !== $Transactions->get_balance() ) {
     103
     104                    $difference = $stock_quantity - $Transactions->get_balance();
     105
     106                    if ( $difference < 0 ) {
     107
     108                        $Activation_Transaction->set_credit( abs( $difference ) );
     109
     110                    } else {
     111
     112                        $Activation_Transaction->set_debit( $difference );
     113
     114                    }
    104115
    105116                    $Activation_Transaction->set_note(
    106                         __( "Balance on activation.", $tr_key )
    107                      );
    108                     $Activation_Transaction->set_debit( $stock_quantity );
    109                     $Activation_Transaction->is_hidden( true );
     117                        __( "Activation adjustment.", $tr_key )
     118                    );
     119                    $Activation_Transaction->is_hidden( false );
    110120                    $Storage->create( $Activation_Transaction );
    111 
    112                 } else {
    113 
    114                     if ( $stock_quantity !== $Transactions->get_balance() ) {
    115 
    116                         $difference = $stock_quantity - $Transactions->get_balance();
    117 
    118                         if ( $difference < 0 ) {
    119 
    120                             $Activation_Transaction->set_credit( abs( $difference ) );
    121 
    122                         } else {
    123 
    124                             $Activation_Transaction->set_debit( $difference );
    125 
    126                         }
    127 
    128                         $Activation_Transaction->set_note(
    129                             __( "Activation adjustment.", $tr_key )
    130                         );
    131                         $Activation_Transaction->is_hidden( false );
    132                         $Storage->create( $Activation_Transaction );
    133 
    134                     }
    135121
    136122                }
  • stock-tracking-reporting-for-woocommerce/trunk/src/services/inventory.php

    r2477174 r2494428  
    4040    }
    4141
     42    public function get_products( array $extra_parameters=[] ) {
     43
     44        $default_parameters = [
     45            "orderby" => "id",
     46            "order" => "ASC",
     47            "return" => "objects",
     48            "status" => "published",
     49            "type" => "simple",
     50            "manage_stock" => true, // important, do not remove.
     51        ];
     52
     53        $parameters = array_merge( $default_parameters, $extra_parameters );
     54
     55        return wc_get_products( $parameters );
     56
     57    }
     58
     59    public function get_products_as_ids_all( array $extra_parameters=[] ) {
     60
     61        $default_parameters = [
     62            "limit" => -1,
     63            "return" => "ids",
     64        ];
     65
     66        $parameters = array_merge( $default_parameters, $extra_parameters );
     67
     68        return $this->get_products( $parameters );
     69
     70    }
     71
     72    public function get_products_all( array $extra_parameters=[] ) {
     73
     74        $default_parameters = [
     75            "limit" => -1,
     76            "return" => "objects",
     77        ];
     78
     79        $parameters = array_merge( $default_parameters, $extra_parameters );
     80
     81        return $this->get_products( $parameters );
     82
     83
     84    }   
     85
    4286    public function set_product_stock( $product_id, $quantity, $is_decrease=false ) {
    4387
     
    134178        $WC_Order = $WC_Order_Item_Product->get_order();
    135179
     180        if ( ! $WC_Product->managing_stock() ) {
     181            return false;
     182        }
     183
    136184        if ( ! $WC_Product->exists() ) {
    137 
    138185            throw new \RuntimeException( "Invalid product, it does not exist." );
    139 
    140186        }
    141187
     
    154200
    155201        if ( $is_credit ) {
    156 
    157202            $Transaction->set_credit( $WC_Order_Item_Product->get_quantity() );
    158 
    159203        } else {
    160 
    161204            $Transaction->set_debit( $WC_Order_Item_Product->get_quantity() );
    162 
    163205        }
    164206
    165207        if ( ! is_null( $note ) ) {
    166 
    167208            $Transaction->set_note( $note );
    168 
    169209        }
    170210
  • stock-tracking-reporting-for-woocommerce/trunk/stock-tracking-reporting-for-woocommerce.php

    r2494367 r2494428  
    22
    33/**
    4  * Version: 1.1.2
     4 * Version: 1.1.3
    55 * Plugin Name: Stock Tracking & Reporting for Woocommerce
    66 * Plugin URI: https://innocow.com
Note: See TracChangeset for help on using the changeset viewer.