WooCommerce: change product image permalink with filter/action hook

ぐ巨炮叔叔 提交于 2020-01-16 02:53:08

问题


I am looking for a filter/action hook (or any other way) to change image URL that is displayed on cart page as a thumbnail.
Example image: http://jamescollings.co.uk/wp-content/uploads/2014/12/cart-donation-form.png

I found that it is retrieved via $_product->get_image() method, but I could not find anything similar to $_product->set_image().


回答1:


I have found 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).



来源:https://stackoverflow.com/questions/32876133/woocommerce-change-product-image-permalink-with-filter-action-hook

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