Hook for customizing product image thumbnail

独自空忆成欢 提交于 2021-02-07 09:20:07

问题


I need a help on woo-commerce to override the cart product image thumbnail.
I am creating a plugin for customizing the product in the detail page and if we do "add to cart" it will be updated in the cart page with a customized thumbnail.

If any hook is available for overriding the image, please let me know.


回答1:


I've spent many hours searching for the answer also and even asked a Stackoverflow question (WooCommerce: change product image permalink with filter/action hook) which now happens to be duplicate (could not find this question prior to submitting my own).

The answer:

The hook is woocommerce_cart_item_thumbnail. So in your functions.php add

function custom_new_product_image($a) {

    $class = 'attachment-shop_thumbnail wp-post-image'; // Default cart thumbnail class.
    $src = [PATH_TO_YOUR_NEW_IMAGE];

    // Construct your img tag.
    $a = '<img';
    $a .= ' src="' . $src . '"';
    $a .= ' class="' . $class . '"';
    $a .= ' />';

    // Output.
    return $a;

}

add_filter( 'woocommerce_cart_item_thumbnail', 'custom_new_product_image' );

and your thumbnails will be replaced (more processing needed if you want to change each thumbnail individually).




回答2:


Please review the WooCommerce cart templates in woocommerce/templates/cart/cart.php.

There is a clear filter woocommerce_cart_item_thumbnail for the product thumbnail in the cart.

$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );



回答3:


To change thumbnail image size of WooCommerce cart page you need next steps:

  1. In function.php create the size you need:

    if ( function_exists( 'add_image_size' ) ) {
        add_image_size( 'custom-thumb', 100, 100, true ); // 100 wide and 100 high
    }
    
  2. In cart.php which should be located in your_theme\woocommerce\cart\cart.php find

    $thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image( 'custom-thumb' ), $cart_item, $cart_item_key );
    


来源:https://stackoverflow.com/questions/29120671/hook-for-customizing-product-image-thumbnail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!