PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.7
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.7
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / includes / classes / purchase-orders / class-cmbird-purchase-automation.php
commercebird / includes / classes / purchase-orders Last commit date
class-cmbird-purchase-admin.php 1 year ago class-cmbird-purchase-automation.php 1 year ago class-cmbird-purchase-order-email.php 1 year ago class-cmbird-purchase-order.php 1 year ago class-cmbird-rest-shop-purchase-controller.php 1 year ago index.php 1 year ago
class-cmbird-purchase-automation.php
148 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * Commercebird Purchase Order Class - Automation
9 *
10 * @since 1.0.0
11 */
12 class CMBIRD_Purchase_Orders_Automation {
13
14 public function __construct() {
15 add_action( 'cmbird_purchase_order_auto', array( $this, 'create_purchase_order' ) );
16 }
17
18 /**
19 * Create purchase order for matching products based on automation settings.
20 *
21 * @since 1.0.0
22 */
23 public function create_purchase_order() {
24 $settings = get_option( 'cmbird_po_automation_settings' );
25
26 // Check if automation is enabled
27 if ( empty( $settings ) || empty( $settings['enable_automation'] ) || ! $settings['enable_automation'] ) {
28 return;
29 }
30
31 $selected_statuses = isset( $settings['product_stock_status'] ) ? (array) $settings['product_stock_status'] : array();
32
33 $products = array();
34
35 if ( in_array( 'out_of_stock', $selected_statuses, true ) ) {
36 $products = array_merge(
37 $products,
38 wc_get_products(
39 array(
40 'status' => 'publish',
41 'limit' => -1,
42 'stock_status' => 'outofstock',
43 )
44 )
45 );
46 }
47
48 if ( in_array( 'low_stock', $selected_statuses, true ) ) {
49 $products = array_merge(
50 $products,
51 wc_get_products(
52 array(
53 'status' => 'publish',
54 'limit' => -1,
55 'stock_status' => 'lowstock',
56 )
57 )
58 );
59 }
60
61 if ( in_array( 'on_backorder', $selected_statuses, true ) ) {
62 $products = array_merge(
63 $products,
64 wc_get_products(
65 array(
66 'status' => 'publish',
67 'limit' => -1,
68 'stock_status' => 'onbackorder',
69 )
70 )
71 );
72 }
73
74 // Remove duplicates
75 $products = array_unique( $products, SORT_REGULAR );
76
77 if ( empty( $products ) ) {
78 return;
79 }
80
81 // Optionally group by vendor in the future using this meta key
82 // $vendor_id = get_post_meta( $product->get_id(), 'vendor_id', true );
83
84 // Get existing purchase orders with specific statuses
85 $existing_purchase_orders = wc_get_orders(
86 array(
87 'type' => 'shop_purchase',
88 'status' => array( 'pending', 'awaiting-approval', 'approved' ),
89 'limit' => 5,
90 'return' => 'objects',
91 )
92 );
93
94 // Collect product IDs already in pending/awaiting/approved purchase orders
95 $excluded_product_ids = array();
96
97 foreach ( $existing_purchase_orders as $po_order ) {
98 foreach ( $po_order->get_items() as $item ) {
99 $excluded_product_ids[] = $item->get_product_id();
100 }
101 }
102
103 $included_product_ids = array();
104 // Then process each product, skipping the excluded ones
105 foreach ( $products as $product ) {
106 if ( in_array( $product->get_id(), $excluded_product_ids, true ) ) {
107 continue; // Skip this product
108 }
109
110 $stock_quantity = $product->get_stock_quantity();
111 $low_stock_amount = $product->get_low_stock_amount();
112 $quantity_to_order = 1;
113
114 if ( $product->is_on_backorder( 1 ) || $product->get_stock_status() === 'onbackorder' ) {
115 $target_stock = 10;
116 $quantity_to_order = max( 1, $target_stock - intval( $stock_quantity ) );
117 } elseif ( $product->get_stock_status() === 'outofstock' ) {
118 $quantity_to_order = 5;
119 } elseif ( null !== $stock_quantity && null !== $low_stock_amount && $low_stock_amount >= $stock_quantity ) {
120 $target_stock = $low_stock_amount + 5;
121 $quantity_to_order = max( 1, $target_stock - intval( $stock_quantity ) );
122 }
123 // add product and quantity to the included product IDs
124 $included_product_ids[ $product->get_id() ] = array(
125 'product' => $product,
126 'quantity' => $quantity_to_order,
127 );
128 }
129
130 // If no products to include, exit
131 if ( empty( $included_product_ids ) ) {
132 return;
133 }
134
135 // Create a shop_purchase order
136 $order = new CMBIRD_Purchase_Order();
137 // add the products to the order
138 foreach ( $included_product_ids as $product_id => $product_data ) {
139 $product = $product_data['product'];
140 $quantity = $product_data['quantity'];
141 $order->add_product( $product, $quantity );
142 }
143 $order->calculate_totals();
144 $order->update_status( 'awaiting-approval' );
145 $order->save();
146 }
147 }
148