如何实现WordPress图片按商品SKU批量关联及插件API替代方案
实现WooCommerce商品与图片自动关联的两种方案
场景说明
当前上传至WordPress的图片文件名包含商品SKU,格式示例:
arm-1001-01.jpg arm-1001-02.jpg arm-1001-03.jpg arm-1001-04.jpg arm-1002-01.jpg arm-1002-02.jpg arm-1003-01.jpg arm-1003-02.jpg arm-1003-03.jpg
原有代码仅支持文件名与SKU完全匹配的单张图片关联,现需实现:
- 所有文件名包含指定SKU的图片,自动关联到对应商品(如SKU为
arm-1001的商品关联所有arm-1001-xx.jpg图片) - 优先采用WPLR Sync插件API实现更优雅的关联逻辑:集合名与商品SKU对应(如
ARM-1002集合关联SKUARM-1002的商品;子商品ARM-1002-60对应集合ARM-1002-60-ROT)
方案一:修改原有代码实现文件名前缀匹配关联
代码修改思路
- 从图片文件名中提取SKU前缀(如从
arm-1001-01中提取arm-1001) - 查询对应SKU的商品,仅在商品无缩略图时设置当前图为缩略图,其余图片添加至商品图库
- 将图片附属到商品(设置
post_parent)
修改后的代码
add_action('add_attachment', function( $attachmentID ) { if ( ! class_exists( 'WC_Product' ) ) return; // 获取文件名(不含后缀) $src = wp_get_attachment_image_src( $attachmentID, 'full' ); $filename = pathinfo( $src[0], PATHINFO_FILENAME ); // 从文件名提取SKU前缀(匹配格式:前缀-数字,如arm-1001) if ( preg_match('/^([a-z]+-\d+)(?:-\d+)?$/i', $filename, $matches) ) { $target_sku = $matches[1]; } else { // 不符合格式则跳过 return; } // 查询对应SKU的商品 $args = array( 'meta_key' => '_sku', 'meta_value' => $target_sku, 'post_type' => 'product', 'posts_per_page' => 1, 'post_status' => 'publish', 'no_found_rows' => true, // 优化查询性能 ); $prods = get_posts( $args ); if ( empty($prods) ) return; $product = array_pop( $prods ); $product_obj = wc_get_product( $product->ID ); // 1. 设置商品缩略图(仅当无缩略图时) if ( ! $product_obj->get_image_id() ) { set_post_thumbnail( $product, $attachmentID ); } // 2. 将图片添加至商品图库 $gallery_ids = $product_obj->get_gallery_image_ids(); if ( ! in_array($attachmentID, $gallery_ids) ) { $gallery_ids[] = $attachmentID; $product_obj->set_gallery_image_ids( $gallery_ids ); $product_obj->save(); } // 3. 将图片附属到商品 $attachment = get_post( $attachmentID ); if ( $attachment->post_parent !== $product->ID ) { $attachment->post_parent = $product->ID; wp_update_post( $attachment ); } });
代码说明
- 正则表达式
/^([a-z]+-\d+)(?:-\d+)?$/i适配arm-1001-01这类文件名,精准提取SKU前缀 - 避免重复覆盖商品缩略图,仅在商品无主图时设置当前图片
- 将匹配图片批量加入商品图库,确保所有关联图片在商品页展示
- 用
no_found_rows参数优化查询性能,减少不必要的数据库计算
方案二:使用WPLR Sync API实现集合与商品关联(推荐)
核心逻辑
利用WPLR Sync的API监听集合更新事件,根据集合名匹配对应SKU的商品,批量关联集合内所有图片:
- 集合名
ARM-1002→ 匹配SKU为ARM-1002的商品 - 集合名
ARM-1002-60-ROT→ 匹配SKU为ARM-1002-60的商品(自动移除集合名末尾的-ROT)
示例代码
// 监听WPLR Sync集合更新事件 add_action('wplr_after_update_collection', function( $collection_id ) { if ( ! class_exists( 'WC_Product' ) ) return; // 获取集合信息 $collection = get_post( $collection_id ); if ( empty($collection) || $collection->post_type !== 'wplr_collection' ) return; $collection_name = $collection->post_title; // 从集合名提取目标SKU(如ARM-1002-60-ROT → ARM-1002-60) $target_sku = preg_replace('/-ROT$/i', '', $collection_name); // 查询对应SKU的商品 $args = array( 'meta_key' => '_sku', 'meta_value' => $target_sku, 'post_type' => 'product', 'posts_per_page' => 1, 'post_status' => 'publish', 'no_found_rows' => true, ); $prods = get_posts( $args ); if ( empty($prods) ) return; $product = array_pop( $prods ); $product_obj = wc_get_product( $product->ID ); // 获取集合内所有图片ID $gallery_ids = get_post_meta( $collection_id, 'wplr_gallery', true ); if ( empty($gallery_ids) || ! is_array($gallery_ids) ) return; // 1. 设置商品缩略图(取集合第一张图,仅当商品无缩略图时) if ( ! $product_obj->get_image_id() && ! empty($gallery_ids) ) { set_post_thumbnail( $product, $gallery_ids[0] ); } // 2. 将集合所有图片添加至商品图库 $current_gallery = $product_obj->get_gallery_image_ids(); $new_gallery = array_unique( array_merge($current_gallery, $gallery_ids) ); $product_obj->set_gallery_image_ids( $new_gallery ); $product_obj->save(); // 3. 将所有图片附属到商品 foreach ( $gallery_ids as $attachment_id ) { $attachment = get_post( $attachment_id ); if ( $attachment->post_parent !== $product->ID ) { $attachment->post_parent = $product->ID; wp_update_post( $attachment ); } } });
代码说明
- 监听
wplr_after_update_collection事件,集合创建或修改时自动触发关联 - 通过正则批量处理集合名,自动适配子商品的集合命名规则
- 一次性同步集合内所有图片,无需依赖文件名格式,逻辑更清晰
- 支持集合图片更新时自动同步到商品,维护成本更低
内容的提问来源于stack exchange,提问作者Redbeard
相关产品推荐
相关产品推荐

