我是一位经验丰富的 C# 程序员,使用 WordPress 和 PHP 工作。我正在与一个团队合作构建 woocommerce 与外部销售点 API 的集成。
API 调用正在提取 3,500 个产品的 json(sku、属性、desc、价格、税费等)。 WordPress 程序员将其设置为一个 cron 作业,删除所有相关的 WordPress 数据,并使用 wp 调用通过所有产品、分类法等的循环来逐一重新填充它。
最大的问题是页面每 5 分钟超时只能处理大约 250 个项目,因此它不起作用。而且,我们需要一个可以每小时运行的解决方案,以使 woocommerce 网站尽可能与 POS 系统同步。
我建议使用 woocommerce API,但被告知它有项目限制,并且仍然没有解决这个问题。经过几天的搜索,我很难找到解决方案。
完整代码很长,所以这里是伪代码:
Delete all related info from:
wp_terms
wp_term_taxonomy
wp_term_relationships
wp_postmeta
wp_postmeta
wp_posts
wp_termmeta
For each product in 3,500 json products {
// build set of 35 attribute variables.. example:
if( !empty($product->on_hand) ){ $on_hand = number_format($product->on_hand, 0); } else { $on_hand = 0; }
... x35
// create/update 12 taxonomies... example:
if(!empty($is_staff_picks)){
$term_is_staff_picks = wp_set_object_terms( $post_id, 'Staff Picks', 'pa_special_requirements', true );
$product_attributes_data['pa_special_requirements'] = Array( 'name' => 'pa_special_requirements', 'value' => 'Staff Picks', 'is_visible' => '1', 'is_variation' => '0', 'is_taxonomy' => '1' );
}
... x12
// add attributes that aren't taxonomies.. example:
update_post_meta( $post_id,'_product_attributes',$product_attributes_data);
// create the simple product type.. example:
wp_set_object_terms( $post_id, 'simple', 'product_type' );
// make 32 function calls to update/add metadata.. example:
update_post_meta( $post_id, 'key', $val );
... x32
}
... x3,500
我重写了大部分功能。
现在,每个 POS 商品都有一个保存的元值,用作比较值(根据可能更改的描述部分构建)。
每 30 分钟,我们运行一个 cron 作业,提取所有项目并使用这一字段进行比较,以检查更改。我们
unset
的物品是相同的。
如果该项目已更改,我们将其放入“更新”数组中。如果它是新的,我们将其放入“要添加”的数组中,剩下的任何内容都已被删除,因此我们将数组的剩余部分保留为“删除”数组。
然后,遍历每个数组以从数据库中添加/更新/删除项目。尽管项目数量几乎增加了一倍,但整个过程现在只需要几分钟。
对于库存计数,我们做了类似的事情,但我们只查看与每个项目相关的现有库存计数(没有其他)。我们的更新时间有限,因此我们每 5 分钟运行一次,以保持库存尽可能准确。