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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
<?php
namespace Onlineforce\Woocommerce_Izettle;
use Onlineforce\Izettle_Bindings\Izettle_Product;
use Onlineforce\Izettle_Bindings\Izettle_Product_Variant;
use \Onlineforce\Izettle_Bindings\Izettle_Request;
use \Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Inventory;
use \Onlineforce\Izettle_Bindings\UUID;
use \Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Products_Queue;
use \Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Images;
class Woocommerce_Izettle_Products {
private static function set_mapping( $wc_product_id, $product_uuid, $variant_uuid ) {
global $wpdb;
$wpdb->replace(
"{$wpdb->prefix}woocommerce_izettle_products",
array(
'product_uuid' => $product_uuid,
'variant_uuid' => $variant_uuid,
'woocommerce_id' => $wc_product_id,
),
array(
'%s',
'%s',
'%d',
)
);
}
public static function get_mapping( $wc_product_id ) {
global $wpdb;
$uuids = $wpdb->get_results(
$wpdb->prepare(
" SELECT product_uuid, variant_uuid
FROM {$wpdb->prefix}woocommerce_izettle_products
WHERE woocommerce_id = %d
",
(int) $wc_product_id
)
);
if ( empty( $uuids ) ) {
return false;
}
return $uuids[0];
}
private static function get_product_uuid( $wc_product_id ) {
$mapping = self::get_mapping( $wc_product_id );
if ( $mapping ) {
return $mapping->product_uuid;
}
return false;
}
private static function get_variant_uuid( $wc_product_id ) {
$mapping = self::get_mapping( $wc_product_id );
if ( $mapping ) {
return $mapping->variant_uuid;
}
return false;
}
public static function generate_barcode( $wc_product_id ) {
if ( get_post_meta( $wc_product_id, '_woocommerce_izettle_ean', true ) ) {
return false;
}
$generator = new \Picqer\Barcode\BarcodeGeneratorIzettle();
$new_barcode = 00 . 0000000 . hexdec( substr( md5( $wc_product_id ), 0, 7 ) );
$new_barcode = $generator->getCode( $new_barcode, $generator::TYPE_EAN_13 );
update_post_meta( $wc_product_id, '_woocommerce_izettle_ean', $new_barcode );
}
private static function get_barcode( $wc_product_id ) {
return get_post_meta( $wc_product_id, '_woocommerce_izettle_ean', true );
}
public static function get_barcode_html( $wc_product_id ) {
$generator = new \Picqer\Barcode\BarcodeGeneratorIzettle();
$barcode = $generator->getBarcode(
self::get_barcode( $wc_product_id ),
$generator::TYPE_EAN_13
);
return $barcode;
}
public static function is_synced( $wc_product_id ) {
global $wpdb;
$wc_product = wc_get_product( $wc_product_id );
if ( ! $wc_product ) {
return false;
}
if ( 'variable' === $wc_product->get_type() ) {
$variants_synced = true;
foreach ( $wc_product->get_children() as $wc_variant_id ) {
if ( ! self::is_synced( $wc_variant_id ) ) {
$variants_synced = false;
}
}
return $variants_synced;
}
return ! empty(
$wpdb->get_var(
$wpdb->prepare(
" SELECT variant_uuid
FROM {$wpdb->prefix}woocommerce_izettle_products
WHERE woocommerce_id = %d
",
(int) $wc_product_id
)
)
) ? true : false;
}
public static function get_product_from_uuids( $product_uuid, $variant_uuid ) {
global $wpdb;
return $wpdb->get_var(
$wpdb->prepare(
" SELECT woocommerce_id
FROM {$wpdb->prefix}woocommerce_izettle_products
WHERE product_uuid = %s
AND variant_uuid = %s
",
array( $product_uuid, $variant_uuid )
)
);
}
private static function generate_uuids() {
$tmp_product_uuid = UUID::generate(
UUID::UUID_TIME, UUID::FMT_STRING, rand()
);
$tmp_variant_uuid = UUID::generate(
UUID::UUID_TIME, UUID::FMT_STRING, rand()
);
$uuids = new \stdClass();
$uuids->product_uuid = $tmp_product_uuid;
$uuids->variant_uuid = $tmp_variant_uuid;
return $uuids;
}
public static function sync_product( $wc_product_id ) {
$already_synced = self::is_synced( $wc_product_id );
if ( $already_synced ) {
$result = self::update_product( $wc_product_id );
} else {
$result = self::create_product( $wc_product_id );
}
if ( wc_get_product( $wc_product_id )->get_manage_stock() ) {
foreach ( $result->get_variants() as $variant ) {
$wc_id = self::get_product_from_uuids(
$result->get_uuid(),
$variant->get_uuid()
);
Woocommerce_Izettle_Inventory::update_inventory_in_izettle(
$wc_id,
wc_get_product( $wc_id )->get_stock_quantity()
);
}
}
return $result;
}
public static function sync_all_products() {
$wc_query = new \WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
)
);
if ( ! $wc_query->have_posts() ) {
throw new \Exception(
'Failed to bulk sync products to iZettle: No Woo products found.'
);
}
$results = array();
foreach ( $wc_query->posts as $post ) {
$results[] = self::sync_product( $post->ID );
}
return $results;
}
public static function create_product( $wc_product_id ) {
$wc_product = wc_get_product( $wc_product_id );
if ( ! $wc_product ) {
throw new \Exception( "Could not find any WooCommerce product with ID {$wc_product_id}" );
}
$product_uuid = self::generate_uuids()->product_uuid;
update_post_meta(
$wc_product_id,
'_woocommerce_izettle_product_uuid',
$product_uuid
);
$izettle_product = new Izettle_Product(
$wc_product,
$product_uuid
);
$is_variable = 'variable' === $wc_product->get_type();
if ( $is_variable ) {
foreach ( $wc_product->get_children() as $wc_variant_id ) {
$wc_variant = wc_get_product( $wc_variant_id );
$izettle_variant = new Izettle_Product_Variant(
$wc_variant,
self::generate_uuids()->variant_uuid,
$izettle_product
);
$izettle_product->add_variant( $izettle_variant );
self::set_mapping(
$wc_variant_id,
$product_uuid,
$izettle_variant->get_uuid()
);
}
}
else {
$izettle_variant = new \Onlineforce\Izettle_Bindings\Izettle_Product_Variant(
$wc_product,
self::generate_uuids()->variant_uuid,
$izettle_product
);
$izettle_product->add_variant( $izettle_variant );
self::set_mapping( $wc_product_id, $product_uuid, $izettle_variant->get_uuid() );
}
$response = Izettle_Request::request(
'POST',
'https://products.izettle.com',
'/organizations/self/products',
$izettle_product
);
if ( isset( $response->body->developerMessage )
&& -1 !== strpos( 'already exists', $response->body->developerMessage ) ) {
return self::update_product( $wc_product_id );
}
if ( '201' === $response->headers['Status-Code'] ) {
return $izettle_product;
} else {
throw new \Exception(
"Failed to create product in iZettle: {$response->headers['Status']}"
);
}
}
public static function update_product( $wc_product_id ) {
$wc_product = wc_get_product( $wc_product_id );
if ( ! $wc_product ) {
throw new \Exception( "Could not find any WooCommerce product with ID {$wc_product_id}" );
}
if ( 'variation' === $wc_product->get_type() ) {
$uuids = self::get_mapping( $wc_product->get_parent_id() );
$product_uuid = get_post_meta(
$wc_product->get_parent_id(),
'_woocommerce_izettle_product_uuid',
true
);
} else {
$uuids = self::get_mapping( $wc_product_id );
$product_uuid = get_post_meta(
$wc_product_id,
'_woocommerce_izettle_product_uuid',
true
);
}
$izettle_product = new Izettle_Product(
$wc_product,
$product_uuid
);
$is_variable = 'variable' === $wc_product->get_type();
if ( $is_variable ) {
foreach ( $wc_product->get_children() as $wc_variant_id ) {
$wc_variant = wc_get_product( $wc_variant_id );
$izettle_variant = new Izettle_Product_Variant(
$wc_variant,
self::get_variant_uuid( $wc_variant_id ),
$izettle_product
);
$izettle_product->add_variant( $izettle_variant );
}
}
else {
$izettle_variant = new Izettle_Product_Variant(
$wc_product,
$uuids->variant_uuid,
$izettle_product
);
$izettle_product->add_variant( $izettle_variant );
}
$response = Izettle_Request::request(
'PUT',
'https://products.izettle.com',
"/organizations/self/products/{$product_uuid}",
$izettle_product
);
foreach ( $izettle_product->get_variants() as $variant ) {
Izettle_Request::request(
'PUT',
'https://products.izettle.com',
"/organizations/self/products/{$product_uuid}/variants/{$variant->get_uuid()}",
$variant
);
}
if ( '404' === $response->headers['Status-Code'] ) {
Izettle_Request::request(
'POST',
'https://products.izettle.com',
'/organizations/self/products',
$izettle_product
);
return $izettle_product;
}
if ( '204' === $response->headers['Status-Code'] ) {
return $izettle_product;
} else {
throw new \Exception(
"Failed to update product in iZettle: {$response->headers['Status']}"
);
}
}
}