Last active
March 17, 2021 17:11
-
-
Save aiiddqd/50b3e7ccb3a0bfe61ff81cca8fe6e224 to your computer and use it in GitHub Desktop.
Сниппет для реализации интеграции множества складов и остатков для МойСклад и магазина с WooCommerce, плагин WooMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooMS Multi Warehouse | |
* Plugin URI: https://github.com/wpcraft-ru/wooms/issues/327 | |
* Description: Добавляет механизм сохранения остатков по множеству складов в метаполя продукта | |
* Version: 1.1 | |
*/ | |
defined('ABSPATH') || exit; // Exit if accessed directly | |
/** | |
* Synchronization the stock of goods from MoySklad | |
*/ | |
class MultiWH | |
{ | |
static public $config_wh_list = [ | |
'Основной склад' => 'wh_main', | |
'Розничный магаз' => 'wh_retail', | |
]; | |
/** | |
* The init | |
*/ | |
public static function init() | |
{ | |
add_action('plugins_loaded', function () { | |
add_filter('wooms_product_save', array(__CLASS__, 'update_product'), 30, 2); | |
add_filter('wooms_variation_save', array(__CLASS__, 'update_variation'), 30, 2); | |
}); | |
} | |
/** | |
* Update product | |
*/ | |
public static function update_product($product, $data_api) | |
{ | |
$url = ''; | |
if ($product->get_type() == 'simple') { | |
$url = sprintf('https://online.moysklad.ru/api/remap/1.2/report/stock/bystore?filter=product=https://online.moysklad.ru/api/remap/1.2/entity/product/%s', $product->get_meta('wooms_id')); | |
} | |
if ($product->get_type() == 'variation') { | |
$url = sprintf('https://online.moysklad.ru/api/remap/1.2/report/stock/bystore?filter=variant=https://online.moysklad.ru/api/remap/1.2/entity/variant/%s', $product->get_meta('wooms_id')); | |
} | |
if (empty($url)) { | |
return $product; | |
} | |
$data = wooms_request($url); | |
if (!isset($data['rows'])) { | |
return $product; | |
} | |
$result = []; | |
foreach (self::$config_wh_list as $name => $key) { | |
$value = 0; | |
if (isset($data['rows'][0]['stockByStore'])) { | |
foreach ($data['rows'][0]['stockByStore'] as $row) { | |
if ($row['name'] == $name) { | |
$value = $row['stock']; | |
} | |
} | |
} | |
$result[] = [ | |
'key' => $key, | |
'name' => $name, | |
'value' => $value, | |
]; | |
} | |
foreach ($result as $item) { | |
$product->update_meta_data($item['key'], $item['value']); | |
} | |
return $product; | |
} | |
} | |
MultiWH::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment