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 |