我正在使用WooCommerce在WordPress平台下创建我的电子商务商店,但我只想为登录用户显示归档商店页面图像。
非登录用户将看到占位符,阻止他们看到商店的图像。
我遵循了这教程,这似乎是我想要的,但没有视觉结果一致,我相信这个钩子不是无关的,也不是被反对的。
//Display different images for non-logged in users
add_filter( 'woocommerce_placeholder_img_src', 'growdev_custom_woocommerce_placeholder', 10 );
function growdev_custom_woocommerce_placeholder( $image_url ) {
if ( is_user_logged_in() ) {
return;
}
$image_url = 'https://vps1420.secured-cloud.net/wp-content/uploads/2021/04/516.webp'; // change this to the URL to your custom placeholder
return $image_url;
}现在,图像仍然像往常一样显示(不管我是否登录)。
如何在WooCommerce中为未注册用户显示不同的图像?
发布于 2021-04-12 09:53:39
您可以使用woocommerce_before_shop_loop_item_title动作钩子。首先删除woocommerce_before_shop_loop_item_title钩子,然后再添加该钩子,并使用is_user_logged_in()检查用户登录与否。代码转到您的活动主题functions.php文件。
使用这个
woocommerce_get_product_thumbnail()函数得到产品缩略图。 使用此wc_placeholder_img()函数获取产品占位符图像。
// Remove product images from the shop loop
function remove_shop_images() {
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
}
add_action( 'after_setup_theme', 'remove_shop_images' );
function display_product_image_based_on_logged_in(){
if( is_user_logged_in() ){
echo woocommerce_get_product_thumbnail();
}else{
echo wc_placeholder_img();
}
}
add_action( 'woocommerce_before_shop_loop_item_title', 'display_product_image_based_on_logged_in', 10 );https://stackoverflow.com/questions/67055913
复制相似问题