我们的某些产品使用的是外部图片而非帖子缩略图,我为此网址设置了acf。看来您可以过滤woocommerceget_image()
功能,但是我找不到找到当前产品ID的方法来获取该字段。
这是我到目前为止的内容:
function offsite_product_images($image_url, //variable $this wont work here){
//need some way to get the current product's id
if(get_field('thumbnail_url', $id)){
$image_url = get_field('thumbnail_url', $id);
}
return $image_url;
}
add_filter( 'woocommerce_product_get_image', 'offsite_product_images');
woocommerce get_image()函数:
public function get_image( $size = 'woocommerce_thumbnail', $attr = array(), $placeholder = true ) {
if ( has_post_thumbnail( $this->get_id() ) ) {
$image = get_the_post_thumbnail( $this->get_id(), $size, $attr );
} elseif ( ( $parent_id = wp_get_post_parent_id( $this->get_id() ) ) && has_post_thumbnail( $parent_id ) ) {
$image = get_the_post_thumbnail( $parent_id, $size, $attr );
} elseif ( $placeholder ) {
$image = wc_placeholder_img( $size );
} else {
$image = '';
}
return apply_filters( 'woocommerce_product_get_image', wc_get_relative_url( $image ), $this, $size, $attr, $placeholder, $image );
}
我什至尝试更改函数以直接传递id,但不会这么做。
对于获得产品或其ID传递到我的过滤器的任何帮助,将不胜感激。
您的功能代码不完整,作为WC_Product
对象缺少一些参数,您需要获取产品ID。请尝试以下操作:
add_filter( 'woocommerce_product_get_image', 'offsite_product_images', 10, 5 );
function offsite_product_images( $image, $product, $size, $attr, $placeholder ){
if( get_field('thumbnail_url', $product->get_id() ) ){
$image = get_field('thumbnail_url', $product->get_id() );
}
return $image;
}
代码进入您的活动子主题(或活动主题)的functions.php文件中。它应该工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句