Changeset 2494428
- Timestamp:
- 03/12/2021 05:59:31 PM (5 years ago)
- Location:
- stock-tracking-reporting-for-woocommerce/trunk
- Files:
-
- 5 edited
-
README.txt (modified) (3 diffs)
-
src/hooks/rest_resources.php (modified) (3 diffs)
-
src/services/activation.php (modified) (2 diffs)
-
src/services/inventory.php (modified) (3 diffs)
-
stock-tracking-reporting-for-woocommerce.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
stock-tracking-reporting-for-woocommerce/trunk/README.txt
r2494367 r2494428 1 1 === Stock Tracking & Reporting for Woocommerce === 2 Stable tag: 1.1. 22 Stable tag: 1.1.3 3 3 Contributors: innocow 4 4 Donate link: http://innocow.com/tipjar … … 53 53 == Changelog == 54 54 55 = 1.1.3 = 56 * Fixed activation and tracking bugs with unmanaged inventory items. 57 55 58 = 1.1.2 = 56 59 * Tested to Woocommere 5.1.0 … … 64 67 == Upgrade notice == 65 68 69 = 1.1.3 = 70 * Fixed activation and tracking bugs with unmanaged inventory items. 71 66 72 = 1.1.2 = 67 73 * Tested to Woocommere 5.1.0 -
stock-tracking-reporting-for-woocommerce/trunk/src/hooks/rest_resources.php
r2477174 r2494428 49 49 && intval( $array_query_params["all"] ) === 1 ) { 50 50 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 ) { 57 54 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 ); 67 57 68 58 } … … 85 75 $array_filters = [ 86 76 "limit" => 10, 87 "orderby" => "id",88 "order" => "DESC",89 77 ]; 90 78 91 $array_of_wc_products_by_title = wc_get_products(79 $array_of_wc_products_by_title = $Inventory->get_products( 92 80 array_merge( $array_filters, [ "s" => $safe_s ] ) 93 81 ); … … 97 85 ); 98 86 99 $array_of_wc_products_by_sku = wc_get_products(87 $array_of_wc_products_by_sku = $Inventory->get_products( 100 88 array_merge( $array_filters, [ "sku" => $safe_s ] ) 101 89 ); -
stock-tracking-reporting-for-woocommerce/trunk/src/services/activation.php
r2477174 r2494428 14 14 use Innocow\Stock_Records\Models\Transaction\Transaction; 15 15 use Innocow\Stock_Records\Models\Transaction\Transaction_Search; 16 use Innocow\Stock_Records\Services\Inventory; 16 17 use Innocow\Stock_Records\Services\Storage; 17 18 use Innocow\Stock_Records\Services\WP_Options; … … 63 64 $tr_key = $Stock_Records->get_translation_key(); 64 65 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(); 70 68 71 );69 foreach( $product_ids as $product_id ) { 72 70 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(); 74 74 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") ); 76 78 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. 82 84 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 ); 86 87 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 ); 90 90 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() ) { 96 92 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 ); 99 99 100 $Storage = new Storage(); 101 $Transactions = $Storage->search( $Transaction_Search ); 100 } else { 102 101 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 } 104 115 105 116 $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 ); 110 120 $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 }135 121 136 122 } -
stock-tracking-reporting-for-woocommerce/trunk/src/services/inventory.php
r2477174 r2494428 40 40 } 41 41 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 42 86 public function set_product_stock( $product_id, $quantity, $is_decrease=false ) { 43 87 … … 134 178 $WC_Order = $WC_Order_Item_Product->get_order(); 135 179 180 if ( ! $WC_Product->managing_stock() ) { 181 return false; 182 } 183 136 184 if ( ! $WC_Product->exists() ) { 137 138 185 throw new \RuntimeException( "Invalid product, it does not exist." ); 139 140 186 } 141 187 … … 154 200 155 201 if ( $is_credit ) { 156 157 202 $Transaction->set_credit( $WC_Order_Item_Product->get_quantity() ); 158 159 203 } else { 160 161 204 $Transaction->set_debit( $WC_Order_Item_Product->get_quantity() ); 162 163 205 } 164 206 165 207 if ( ! is_null( $note ) ) { 166 167 208 $Transaction->set_note( $note ); 168 169 209 } 170 210 -
stock-tracking-reporting-for-woocommerce/trunk/stock-tracking-reporting-for-woocommerce.php
r2494367 r2494428 2 2 3 3 /** 4 * Version: 1.1. 24 * Version: 1.1.3 5 5 * Plugin Name: Stock Tracking & Reporting for Woocommerce 6 6 * Plugin URI: https://innocow.com
Note: See TracChangeset
for help on using the changeset viewer.