【发布时间】:2022-07-04 21:00:22
【问题描述】:
在这里不知所措。如果我登录后端并在仪表板中更新产品以添加/删除属性,缓存将被正确清除并且产品将正确过滤。一切正常。
但是,我需要以编程方式添加/更新产品。
我尝试了以下方法:
$wc_data_store = new ReflectionMethod(‘WC_Product_Data_Store_CPT’, ‘update_lookup_table’);
$wc_data_store->setAccessible(true);
/// update product code goes here
wp_cache_delete($post_id, ‘post_meta’);
if (isset($wc_data_store)) $wc_data_store->invokeArgs(new WC_Product_Data_Store_CPT, array($success, ‘wc_product_meta_lookup’));
…当这不起作用时,我在每次产品更新后尝试了一种自定义方法…
// update product code goes here … then call following procedure each product to clear cache
public static function flushCacheUpdateLookupTable($the_product)
{
$product = wc_get_product($the_product);
if ($product) {
$id = $product->get_id();
wc_delete_product_transients($id);
wp_cache_delete($id, ‘post_meta’);
wp_cache_delete($id, ‘posts’);
wp_cache_delete(‘lookup_table’, ‘object_’ . $id);
$productType = $product->get_type();
$datastoreType = ‘product’;
switch ($productType) {
case ‘variable’:
case ‘grouped’:
case ‘variation’:
$datastoreType .= ‘-‘ . $productType;
}
$data_store = \WC_Data_Store::load($datastoreType);
if (method_exists(‘WC_Product_Data_Store_CPT’, ‘update_lookup_table’)) {
$product_data_store = new \WC_Product_Data_Store_CPT();
$reflection = new \ReflectionMethod($product_data_store, ‘update_lookup_table’);
if ($reflection->isPublic()) {
$data_store->update_lookup_table($id, ‘wc_product_meta_lookup’);
} else {
//in the meantime an increase of zero in the product sales will force the update…
$data_store->update_product_sales($id, 0, ‘increase’);
}
}
}
}
我尝试进入工具并手动按下每个按钮来清除瞬态、重建、重新生成等。我无法正确清除缓存,以便产品按属性过滤,除非我去进入产品并手动添加/删除属性以触发属性缓存的更新(这不是一个可行的选择,因为我们每天都在讨论数百种产品更新)。
感谢您的任何想法,现在已经有几天没有任何工作了。
【问题讨论】:
标签: php wordpress caching woocommerce