php Stock Sync Plugin This file contains the StockSyncPlugin class, which handles syncing stock and price of generated variations with their base products in WooCommerce. It has been updated to read its configuration from the consolidated 'agpd_settings' option. if (!defined('ABSPATH')) { exit; } if (!defined('AGPD_PLUGIN_DIR')) { return; } define('STOCK_SYNC_LOG_FILE', AGPD_PLUGIN_DIR . 'logsstock-sync.log'); define('STOCK_SYNC_META_STOCK', '_stock'); define('STOCK_SYNC_META_STOCK_STATUS', '_stock_status'); define('STOCK_SYNC_META_REGULAR_PRICE', '_regular_price'); define('STOCK_SYNC_META_SALE_PRICE', '_sale_price'); define('STOCK_SYNC_META_PRICE', '_price'); class StockSyncPlugin { private static $instance = null; private $wpdb; private $batch_size; private $action = 'agpd_stock_sync'; public static function get_instance() { if (self$instance === null) { self$instance = new self(); } return self$instance; } private function __construct() { global $wpdb; $this-wpdb = $wpdb; Get performance settings from the consolidated settings option. $settings = get_option('agpd_settings', []); $perf_settings = $settings['performance'] []; $this-batch_size = $perf_settings['batch_size'] 20; add_action($this-action, [$this, 'handle_task']); add_action('init', [$this, 'init_cron']); } Set up the cron job for stock syncing. public function init_cron() { $settings = get_option('agpd_settings', []); $perf_settings = $settings['performance'] []; $interval = $perf_settings['stock_sync_interval'] 'daily'; $cron_hook = 'stock_sync_cron'; if (!wp_next_scheduled($cron_hook)) { wp_schedule_event(time(), $interval, $cron_hook); agpd_log(Scheduled stock_sync_cron with interval $interval, true, 'info'); } add_action($cron_hook, [$this, 'run_scheduled_sync']); } Runs the scheduled sync process. public function run_scheduled_sync() { agpd_log('Starting scheduled stock sync.', true, 'info'); $base_products = $this-get_base_products(); if (empty($base_products)) { agpd_log('No base products found to sync.', true, 'info'); return; } as_unschedule_all_actions($this-action, [], 'agpd_stock_sync_group'); $batches = array_chunk($base_products, $this-batch_size, true); foreach ($batches as $batch_index = $batch) { as_schedule_single_action( time() + ($batch_index 10), Stagger batches $this-action, ['batch' = $batch], 'agpd_stock_sync_group' ); } agpd_log('Scheduled stock sync for ' . count($base_products) . ' base products.', true, 'info'); } Handles a single batch of products to sync. @param array $args Arguments containing the 'batch' of products. public function handle_task($args) { $batch = $args['batch'] []; if (empty($batch)) return; foreach ($batch as $base_sku = $base_data) { $this-process_base_product($base_sku, $base_data); } } Processes a single base product and its variations. private function process_base_product($base_sku, $base_data) { $variations = $this-get_variations_for_base($base_sku); if (empty($variations)) { return; } agpd_log(Syncing . count($variations) . variations for base SKU $base_sku., true, 'debug'); $this-sync_variations_with_base($base_data, $variations); } Gets all base products (products without a numeric suffix). private function get_base_products() { $query = $this-wpdb-prepare( SELECT p.ID AS post_id, pm.meta_value AS sku, pm_stock.meta_value AS stock, pm_status.meta_value AS stock_status, pm_regular.meta_value AS regular_price, pm_sale.meta_value AS sale_price FROM {$this-wpdb-posts} p INNER JOIN {$this-wpdb-postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = '_sku' LEFT JOIN {$this-wpdb-postmeta} pm_stock ON p.ID = pm_stock.post_id AND pm_stock.meta_key = %s LEFT JOIN {$this-wpdb-postmeta} pm_status ON p.ID = pm_status.post_id AND pm_status.meta_key = %s LEFT JOIN {$this-wpdb-postmeta} pm_regular ON p.ID = pm_regular.post_id AND pm_regular.meta_key = %s LEFT JOIN {$this-wpdb-postmeta} pm_sale ON p.ID = pm_sale.post_id AND pm_sale.meta_key = %s WHERE p.post_type = 'product' AND p.post_status = 'publish' AND pm.meta_value REGEXP '^[^-]+-[^-]+$', STOCK_SYNC_META_STOCK, STOCK_SYNC_META_STOCK_STATUS, STOCK_SYNC_META_REGULAR_PRICE, STOCK_SYNC_META_SALE_PRICE ); $results = $this-wpdb-get_results($query, ARRAY_A); if ($results === false) return []; $base_products = []; foreach ($results as $row) { $base_products[$row['sku']] = [ 'post_id' = $row['post_id'], 'stock' = $row['stock'], 'stock_status' = $row['stock_status'], 'regular_price' = $row['regular_price'], 'sale_price' = $row['sale_price'], ]; } return $base_products; } Gets variations for a given base SKU. private function get_variations_for_base($base_sku) { $settings = get_option('agpd_stock_sync_settings', ['include_all_variations' = 0]); $include_all = $settings['include_all_variations'] == 1; $like_pattern = $this-wpdb-esc_like($base_sku) . '-%'; $query = $this-wpdb-prepare( SELECT p.ID AS post_id FROM {$this-wpdb-posts} p INNER JOIN {$this-wpdb-postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = '_sku' WHERE p.post_type = 'product' AND pm.meta_value LIKE %s, $like_pattern ); if (!$include_all) { $query .= AND EXISTS ( SELECT 1 FROM {$this-wpdb-postmeta} pm2 WHERE pm2.post_id = p.ID AND pm2.meta_key = '_agpd_generated' AND pm2.meta_value = '1' ); } return $this-wpdb-get_col($query); } Syncs stock and price from a base product to its variations. private function sync_variations_with_base($base_data, $variation_ids) { if (empty($variation_ids)) return; $price = $base_data['sale_price'] $base_data['regular_price']; foreach ($variation_ids as $vid) { update_post_meta($vid, STOCK_SYNC_META_STOCK, $base_data['stock']); update_post_meta($vid, STOCK_SYNC_META_STOCK_STATUS, $base_data['stock_status']); update_post_meta($vid, STOCK_SYNC_META_REGULAR_PRICE, $base_data['regular_price']); update_post_meta($vid, STOCK_SYNC_META_PRICE, $price); if (!empty($base_data['sale_price'])) { update_post_meta($vid, STOCK_SYNC_META_SALE_PRICE, $base_data['sale_price']); } else { delete_post_meta($vid, STOCK_SYNC_META_SALE_PRICE); } wc_delete_product_transients($vid); } } } Initialize the stock sync functionality. add_action('plugins_loaded', function() { if (class_exists('WooCommerce')) { StockSyncPluginget_instance(); } }); Programmed To Your VIN GM Engine Control Module (ECM)
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm
  • Gm Black Box Pcm

Search…

Filter by Vehicle

  Search All
Scroll to Top