WooCommerce 商店页面中产品图片替换的钩子是什么

What hook for product image replacement in WooCommerce shop page

当 WooCommerce returns 商店页面中的产品图片时调用什么函数?我想用其他东西替换图像。

要找出答案,您需要查看 content-product.php template source code 第 36 行到第 42 行:

    /**
     * Hook: woocommerce_before_shop_loop_item_title.
     *
     * @hooked woocommerce_show_product_loop_sale_flash - 10
     * @hooked woocommerce_template_loop_product_thumbnail - 10
     */
    do_action( 'woocommerce_before_shop_loop_item_title' );

所以在查看 woocommerce_template_loop_product_thumbnail() involved hooked template function source code, you will see that it uses woocommerce_get_product_thumbnail() function 时:

if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {

    /**
     * Get the product thumbnail, or the placeholder if not set.
     *
     * @param string $size (default: 'woocommerce_thumbnail').
     * @param int    $deprecated1 Deprecated since WooCommerce 2.0 (default: 0).
     * @param int    $deprecated2 Deprecated since WooCommerce 2.0 (default: 0).
     * @return string
     */
    function woocommerce_get_product_thumbnail( $size = 'woocommerce_thumbnail', $deprecated1 = 0, $deprecated2 = 0 ) {
        global $product;

        $image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );

        return $product ? $product->get_image( $image_size ) : '';
    }
}

所以要进行更改,您必须用这种方式用自定义函数替换 woocommerce_template_loop_product_thumbnail() 挂钩函数:

remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'custom_loop_product_thumbnail', 10 );
function custom_loop_product_thumbnail() {
    global $product;

    if ( $product ) {    
        // your code for image replacement below (to be echoed)
    }
}

要仅定位商店页面,您将使用 is_shop() 条件标记。