1
0
mirror of synced 2025-02-07 18:49:24 +03:00
woocommerce-module/woo-retailcrm/include/class-wc-retailcrm-inventories.php
2017-12-04 12:04:03 +03:00

71 lines
2.2 KiB
PHP

<?php
/**
* RetailCRM Integration.
*
* @package WC_Retailcrm_Inventories
* @category Integration
* @author RetailCRM
*/
if ( ! class_exists( 'WC_Retailcrm_Inventories' ) ) :
/**
* Class WC_Retailcrm_Inventories
*/
class WC_Retailcrm_Inventories
{
public function __construct()
{
$this->retailcrm_settings = get_option( 'woocommerce_integration-retailcrm_settings' );
if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) {
include_once( WP_PLUGIN_DIR . '/woo-retailcrm/include/api/class-wc-retailcrm-proxy.php' );
}
$this->retailcrm = new WC_Retailcrm_Proxy(
$this->retailcrm_settings['api_url'],
$this->retailcrm_settings['api_key'],
$this->retailcrm_settings['api_version']
);
}
public function load_stocks()
{
$page = 1;
do {
$result = $this->retailcrm->storeInventories(array(), $page, 250);
$totalPageCount = $result['pagination']['totalPageCount'];
$page++;
foreach ($result['offers'] as $offer) {
if (isset($offer['externalId'])) {
$product = wc_get_product($offer['externalId']);
if ($product != false) {
if ($product->get_type() == 'variable') {
continue;
}
update_post_meta($offer['externalId'], '_manage_stock', 'yes');
$product->set_stock_quantity($offer['quantity']);
$product->save();
}
}
}
} while ($page <= $totalPageCount);
}
public function updateQuantity()
{
$options = array_filter(get_option( 'woocommerce_integration-retailcrm_settings' ));
if ($options['sync'] == 'yes') {
$this->load_stocks();
} else {
return false;
}
}
}
endif;