1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
<?php
namespace Onlineforce\Woocommerce_Izettle;
use \Onlineforce\Izettle_Bindings\Izettle_Inventory;
class Woocommerce_Izettle_Inventory {
public static function get_inventory_from_izettle( $wc_product_id ) {
if ( ! get_option( 'izettle_inventory_enabled' )
|| ! get_option( 'izettle_inventory_pull_enabled' ) ) {
return false;
}
$cached_result = self::cache_get_izettle_qty( $wc_product_id );
if ( $cached_result ) {
return $cached_result;
}
$uuids = Woocommerce_Izettle_Products::get_mapping( $wc_product_id );
$default_location_uuid = Izettle_Inventory::get_default_location()->uuid;
$response = Izettle_Inventory::get_inventory(
$default_location_uuid, $uuids->product_uuid, $uuids->variant_uuid
);
if ( is_numeric( $response ) ) {
self::cache_set_izettle_qty( intval( $response ), $wc_product_id );
return intval( $response );
}
return false;
}
public static function update_inventory_in_izettle( $wc_product_id, $new_qty ) {
if ( ! get_option( 'izettle_inventory_push_enabled' ) || ! get_option( 'izettle_inventory_enabled' ) ) {
return false;
}
if ( ! Woocommerce_Izettle_Products::is_synced( $wc_product_id ) ) {
return false;
}
$uuids = Woocommerce_Izettle_Products::get_mapping( $wc_product_id );
$store_loc = Izettle_Inventory::get_default_location()->uuid;
$supplier_loc = Izettle_Inventory::get_supplier_location()->uuid;
Izettle_Inventory::start_tracking( $uuids->product_uuid );
$old_qty = self::get_inventory_from_izettle( $wc_product_id );
if ( false === $old_qty ) {
$old_qty = 0;
}
$change = $old_qty - $new_qty;
$from_loc = $store_loc;
$to_loc = $supplier_loc;
if ( 0 === $change ) {
return false;
}
if ( $change < 0 ) {
$from_loc = $supplier_loc;
$to_loc = $store_loc;
}
$response = Izettle_Inventory::update_inventory(
$from_loc,
$to_loc,
$uuids->product_uuid,
$uuids->variant_uuid,
abs( $change )
);
if ( '404' === $response->headers['Status-Code'] ) {
Izettle_Inventory::start_tracking( $uuids->product_uuid );
self::update_inventory_in_izettle( $wc_product_id, $new_qty );
}
return $new_qty;
}
public static function update_inventory_in_wc( $wc_product_id, $new_qty ) {
if ( null === $new_qty || false === $new_qty ) {
return false;
}
$wc_product = wc_get_product( $wc_product_id );
$old_qty = $wc_product->get_stock_quantity();
$change = $old_qty - $new_qty;
if ( 0 === $change ) {
return false;
}
wc_update_product_stock(
$wc_product, $new_qty
);
}
public static function get_izettle_inventory_all() {
try {
$izettle_inventory = Izettle_Inventory::get_inventory_all(
Izettle_Inventory::get_default_location()->uuid
);
return $izettle_inventory;
} catch ( \Exception $e ) {
Woocommerce_Izettle_Admin::plugin_log( $e->getMessage() );
}
}
public static function hook_update_stock( $wc_product ) {
return self::update_inventory_in_izettle(
$wc_product->get_id(),
$wc_product->get_stock_quantity()
);
}
private static function cache_set_izettle_qty( $qty, $wc_product_id ) {
set_transient(
'woocommerce_izettle_stock_' . $wc_product_id,
$qty,
1 * MINUTE_IN_SECONDS
);
}
private static function cache_get_izettle_qty( $wc_product_id ) {
return get_transient( 'woocommerce_izettle_stock_' . $wc_product_id );
}
}