WooCommerce对接Metrc API更新产品后库存未实时显示问题排查
WooCommerce与Metrc API对接插件的库存同步问题
插件功能概述
- 对接Metrc API拉取产品数据:产品不存在则创建(状态设为
pending),已存在则更新产品信息 - 重量型产品转换逻辑:
_qty = _metrc_qty / _package_size,将Metrc数据标签页的数量值,除以库存标签页的包装尺寸值,结果输出到库存标签页数量输入框 - 数据拉取调度:通过
get_active()函数用Curl请求Metrc API,由Action Scheduler每15分钟执行一次,拉取第三方系统中所有“活跃”产品的描述、数量等信息
核心代码片段
API数据处理与产品同步代码
// Other Code Above // Decode the JSON response $data = json_decode($response_body, true); // Format the JSON response with indentation $formatted_data = json_encode($data, JSON_PRETTY_PRINT); // Log the formatted JSON response to a file in the root of the active plugin folder $log_file = plugin_dir_path(__FILE__) . 'metrc_response.log'; // Path to the log file $log_data = date('Y-m-d H:i:s') . " - " . $formatted_data . "\n"; // Log data to write // Append the log data to the log file file_put_contents($log_file, $log_data, FILE_APPEND); // Extract the relevant information from the API response $packages = $data['Data']; // Get all published products in WooCommerce $products = wc_get_products(array('status' => 'publish')); foreach ($products as $product) { $product_package = get_post_meta($product->get_id(), '_metrc_package', true); $matched = false; foreach ($packages as $package) { $label = $package['Label']; if ($product_package === $label) { $matched = true; break; } } } // Import new products based on the API response foreach ($packages as $package) { $label = $package['Label']; $quantity = $package['Quantity']; $uom = $package['UnitOfMeasureAbbreviation']; $prodcat = $package['Item']['ProductCategoryName']; $itemName = $package['Item']['Name']; // Check if product exists based on package meta value, else create a new product $product_id = get_product_id_by_meta('_metrc_package', $label); if (!$product_id) { $new_product = new WC_Product(); // Set the product properties $new_product->set_name($package['Item']['Name']); $new_product->set_status('pending'); $new_product->set_regular_price(0); // Enable stock management $new_product->set_manage_stock(true); $new_product->set_stock_status('instock'); // Save the product $product_id = $new_product->save(); // Update the package meta value for the new product update_post_meta($product_id, '_metrc_package', $label); update_post_meta($product_id, '_metrc_category', $prodcat); update_post_meta($product_id, '_metrc_item', $itemName); // Assign the initial default category to the new product wp_set_object_terms($product_id, $prodcat, 'product_cat', true); // Set the Product SKU update_post_meta($product_id, '_sku', $label); } else { $new_product = wc_get_product($product_id); } // Update _metrc_qty and _metrc_uom with the package quantity and unit of measurement update_post_meta($product_id, '_metrc_qty', $quantity); update_post_meta($product_id, '_metrc_uom', $uom); update_post_meta($product_id, '_metrc_item', $itemName); Update_post_meta($product_id, '_metrc_category', $prodcat); // Set Product SKU update_post_meta($product_id, '_sku', $label); }
尝试禁用库存缓存的代码
function disable_woocommerce_stock_caching() { return false; } add_filter('woocommerce_stock_html_cache_stock_html_enabled', 'disable_woocommerce_stock_caching');
问题现状
通过API更新现有产品后,新值仅在产品编辑页可见,产品列表页仍显示旧信息(如库存数量20),只有手动点击编辑页的“更新”按钮后,前端和产品列表页才会同步新的库存值。已尝试禁用库存缓存,但问题未解决。
需求目标
实现API更新后无需手动操作,新值即可在产品列表页和前端正常显示。
内容的提问来源于stack exchange,提问作者David Desilets
相关产品推荐
相关产品推荐

