Hello, I'm trying to integrate a POS system into my application. I'm almost done, but I'm having trouble with the inventory connection. Could you please help me?
The issue is that I'm able to add line items successfully, but when I try to add a line item from a different inventory source, it doesn't get added. Here's the relevant part of my code—can you take a look and let me know what might be going wrong?
code
require_once '/Applications/XAMPP/xamppfiles/htdocs/npc/woo/wp-load.php';
use AtumPO\Models\POExtended;
use AtumMultiInventory\Inc\Helpers as MIHelpers;
use AtumMultiInventory\Models\Inventory;
// === Safety checks ===
if (!class_exists(POExtended::class)) die("❌ ATUM PO not loaded\n");
if (!class_exists(Inventory::class)) die("❌ ATUM MI not loaded\n");
echo "✅ ATUM PO + MI loaded.\n";
// === Step 1: Create PO ===
$po = new POExtended();
$po->set_status('atum_pending');
$po->set_date_created(current_time('mysql', true));
$po->set_date_expected(current_time('mysql'));
$po->set_description('Test PO with MI support');
$po->set_supplier(54519); // ✅ your supplier ID
$po->save();
echo "✅ PO created: #{$po->get_id()}\n";
// === Step 2: Add product ===
$product_id = 53696;
$product = wc_get_product($product_id);
if (!$product) die("❌ Product not found\n");
$po->add_product($product, 1); // this sets all required internal hooks
$po->save();
echo "✅ Product {$product->get_name()} added\n";
// === Step 3: Link inventory ===
$line_items = $po->get_items(['line_item']);
$line_item_id = null;
foreach ($line_items as $item) {
if ((int)$item->get_product_id() === (int)$product_id) {
$line_item_id = $item->get_id();
break;
}
}
if (!$line_item_id) die("❌ Line item not found\n");
echo "✅ Line item ID: $line_item_id\n";
// === Step 4: Link to Main Inventory ===
$inventories = MIHelpers::get_product_inventories_sorted($product_id);
$main_inventory = null;
foreach ($inventories as $inv) {
if ($inv->is_main()) {
$main_inventory = $inv;
break;
}
}
if (!$main_inventory) die("❌ No main inventory found\n");
echo "✅ Main Inventory ID: {$main_inventory->id}\n";
// === Step 5: Link inventory properly ===
$inventory_obj = new Inventory($main_inventory->id);
$extra_data = maybe_serialize([
'name' => $main_inventory->name,
'sku' => $product->get_sku(),
'supplier_sku' => $main_inventory->supplier_sku,
'inventory_date' => current_time('mysql'),
'is_main' => 'yes',
'location' => [],
'expiry_threshold' => 0,
]);
$inventory_obj->save_order_item_inventory($line_item_id, $main_inventory->id, [
'qty' => 1,
'subtotal' => $product->get_price(),
'total' => $product->get_price(),
'extra_data' => $extra_data,
]);
echo "✅ Linked line item $line_item_id to main inventory\n";