You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce特定品类替换产品图片为占位图代码失效求助

Fix: Replace Tobacco Product Images with Placeholder for Specific WooCommerce Category

Hey Robin, sorry to hear that the initial remove_action snippet didn't work out—WooCommerce can be tricky with action priorities and theme overrides, especially when targeting specific product categories. Let's get this sorted for your Dutch client's upcoming tobacco regulations.

Why Your Initial Snippet Failed

The remove_action call might not have worked because:

  • Your theme (or a plugin) could be registering the product thumbnail action with a different priority than 10.
  • You need to target only the waterpijptabak category, not remove thumbnails site-wide.

Solution 1: Use a Filter to Replace Images (Most Reliable)

The woocommerce_product_get_image filter lets you directly override the product image HTML, and we can restrict it to your target category. Add this to your child theme's functions.php (or use a code snippets plugin to avoid theme update issues):

add_filter('woocommerce_product_get_image', 'replace_tobacco_product_images_with_placeholder', 10, 6);
function replace_tobacco_product_images_with_placeholder($html, $product, $size, $attr, $placeholder, $image_id) {
    // Check if we're on the target product category archive
    if (is_product_category('waterpijptabak')) {
        // Update this URL to your actual placeholder image path
        $placeholder_url = get_stylesheet_directory_uri() . '/images/tobacco-placeholder.png';
        
        // Build accessible, properly formatted placeholder HTML
        $html = sprintf(
            '<img src="%s" alt="%s" %s>',
            esc_url($placeholder_url),
            esc_attr($product->get_name()),
            wc_get_product_image_attr($product, $size)
        );
    }
    return $html;
}

Notes for This Solution:

  • Placeholder Path: Upload your placeholder image to your child theme's images folder, or replace the URL with an absolute link to your image.
  • Single Product Pages: If you need to replace images on individual product pages in this category too, add an extra check inside the if condition:
    || (is_product() && has_term('waterpijptabak', 'product_cat', $product->get_id()))
    

Solution 2: Override the Shop Loop Thumbnail Action

If the filter doesn't work (some themes override WooCommerce's default image output), try removing and replacing the loop thumbnail action specifically for your category:

// Remove default thumbnail action (adjust priority if your theme uses a different one)
remove_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);

// Add custom thumbnail output
add_action('woocommerce_before_shop_loop_item_title', 'custom_tobacco_shop_loop_thumbnail', 10);
function custom_tobacco_shop_loop_thumbnail() {
    global $product;
    
    if (is_product_category('waterpijptabak')) {
        $placeholder_url = get_stylesheet_directory_uri() . '/images/tobacco-placeholder.png';
        echo sprintf(
            '<img src="%s" alt="%s" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail" />',
            esc_url($placeholder_url),
            esc_attr($product->get_name())
        );
    } else {
        // Fallback to default thumbnails for other categories
        woocommerce_template_loop_product_thumbnail();
    }
}

Final Checks

  1. Clear Caches: WooCommerce, your hosting, and browser caches can prevent changes from showing up immediately.
  2. Verify Category Slug: Double-check that waterpijptabak is the correct slug for your product category (confirm this in WooCommerce > Products > Categories).
  3. Use Child Theme: Always add custom code to a child theme instead of the parent theme to avoid losing changes when the theme updates.

内容的提问来源于stack exchange,提问作者Robin Buren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 18:03:03