单个产品页面上的 Woocommerce 挂钩溢出
Woocommerce hook overflow on Single Product Page
我在 Functions.php 中的代码在相应的 WooCommerce 产品上显示存储在自定义字段(使用高级自定义字段插件)中的文本。
我的问题是,我只能在 Shop/product 存档中显示它。当我尝试挂钩单一产品页面缩略图时,它不起作用。
如何挂钩到主单一产品页面图像?
商店存档 - 正确显示。
单个产品页面 - 主图片缺失,仅在相关产品上显示
我的代码在functions.php
function wpo_before_shop_loop_item_title() {
if (defined('WPO_IS_ENABLED') && (WPO_IS_ENABLED === false)) {
// Disabled by configuration.
} else {
$post_id = get_the_ID();
// Is this a featured product?
$terms = wp_get_post_terms(
$post_id,
'product_visibility',
array('fields' => 'names')
);
$is_featured = in_array('featured', $terms);
// Get our custom overlay properties.
$custom_overlay_text = get_post_meta($post_id, 'product_overlay_text', true);
$is_custom_overlay_visible = boolval(get_post_meta(
$post_id,
'is_product_overlay_enabled',
true
));
// Featured product star.
if ($is_featured) {
echo '<div class="prod-overlay prod-overlay-feat">';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '</div>'; // .prod-overlay
}
// Custom overlay text.
if ($is_custom_overlay_visible && !empty($custom_overlay_text)) {
echo '<div class="prod-overlay prod-overlay-text">';
printf(
'<span class="prod-overlay-label">%s</span>',
esc_html($custom_overlay_text)
);
echo '</div>'; // .prod-overlay
}
}
}
add_action(
'woocommerce_before_shop_loop_item_title',
'wpo_before_shop_loop_item_title', 'woocommerce_before_add_to_cart_button', 'woocommerce_product_thumbnails' ,
9
);
woocommerce_before_shop_loop_item_title
挂钩仅针对在循环中呈现的产品执行 (source). You need to use one of the hooks that are actually executed on the single product page. For identifying the correct hook, you can for example use the Visual Hook Guide or the WooCommerce Source Code.
您可以使用 woocommerce_product_thumbnails
动作挂钩。试试下面的代码。
function woocommerce_product_thumbnails_single_product() {
if( is_product() ){
global $product;
if (defined('WPO_IS_ENABLED') && (WPO_IS_ENABLED === false)) {
// Disabled by configuration.
} else {
$post_id = $product->get_id();
// Is this a featured product?
$terms = wp_get_post_terms(
$post_id,
'product_visibility',
array('fields' => 'names')
);
$is_featured = in_array('featured', $terms);
// Get our custom overlay properties.
$custom_overlay_text = get_post_meta($post_id, 'product_overlay_text', true);
$is_custom_overlay_visible = boolval(get_post_meta(
$post_id,
'is_product_overlay_enabled',
true
));
// Featured product star.
if ($is_featured) {
echo '<div class="prod-overlay prod-overlay-feat">';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '</div>'; // .prod-overlay
}
// Custom overlay text.
if ($is_custom_overlay_visible && !empty($custom_overlay_text)) {
echo '<div class="prod-overlay prod-overlay-text">';
printf(
'<span class="prod-overlay-label">%s</span>',
esc_html($custom_overlay_text)
);
echo '</div>'; // .prod-overlay
}
}
}
}
add_action( 'woocommerce_product_thumbnails','woocommerce_product_thumbnails_single_product' );
已测试并有效
我在 Functions.php 中的代码在相应的 WooCommerce 产品上显示存储在自定义字段(使用高级自定义字段插件)中的文本。
我的问题是,我只能在 Shop/product 存档中显示它。当我尝试挂钩单一产品页面缩略图时,它不起作用。
如何挂钩到主单一产品页面图像?
商店存档 - 正确显示。
单个产品页面 - 主图片缺失,仅在相关产品上显示
我的代码在functions.php
function wpo_before_shop_loop_item_title() {
if (defined('WPO_IS_ENABLED') && (WPO_IS_ENABLED === false)) {
// Disabled by configuration.
} else {
$post_id = get_the_ID();
// Is this a featured product?
$terms = wp_get_post_terms(
$post_id,
'product_visibility',
array('fields' => 'names')
);
$is_featured = in_array('featured', $terms);
// Get our custom overlay properties.
$custom_overlay_text = get_post_meta($post_id, 'product_overlay_text', true);
$is_custom_overlay_visible = boolval(get_post_meta(
$post_id,
'is_product_overlay_enabled',
true
));
// Featured product star.
if ($is_featured) {
echo '<div class="prod-overlay prod-overlay-feat">';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '</div>'; // .prod-overlay
}
// Custom overlay text.
if ($is_custom_overlay_visible && !empty($custom_overlay_text)) {
echo '<div class="prod-overlay prod-overlay-text">';
printf(
'<span class="prod-overlay-label">%s</span>',
esc_html($custom_overlay_text)
);
echo '</div>'; // .prod-overlay
}
}
}
add_action(
'woocommerce_before_shop_loop_item_title',
'wpo_before_shop_loop_item_title', 'woocommerce_before_add_to_cart_button', 'woocommerce_product_thumbnails' ,
9
);
woocommerce_before_shop_loop_item_title
挂钩仅针对在循环中呈现的产品执行 (source). You need to use one of the hooks that are actually executed on the single product page. For identifying the correct hook, you can for example use the Visual Hook Guide or the WooCommerce Source Code.
您可以使用 woocommerce_product_thumbnails
动作挂钩。试试下面的代码。
function woocommerce_product_thumbnails_single_product() {
if( is_product() ){
global $product;
if (defined('WPO_IS_ENABLED') && (WPO_IS_ENABLED === false)) {
// Disabled by configuration.
} else {
$post_id = $product->get_id();
// Is this a featured product?
$terms = wp_get_post_terms(
$post_id,
'product_visibility',
array('fields' => 'names')
);
$is_featured = in_array('featured', $terms);
// Get our custom overlay properties.
$custom_overlay_text = get_post_meta($post_id, 'product_overlay_text', true);
$is_custom_overlay_visible = boolval(get_post_meta(
$post_id,
'is_product_overlay_enabled',
true
));
// Featured product star.
if ($is_featured) {
echo '<div class="prod-overlay prod-overlay-feat">';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '</div>'; // .prod-overlay
}
// Custom overlay text.
if ($is_custom_overlay_visible && !empty($custom_overlay_text)) {
echo '<div class="prod-overlay prod-overlay-text">';
printf(
'<span class="prod-overlay-label">%s</span>',
esc_html($custom_overlay_text)
);
echo '</div>'; // .prod-overlay
}
}
}
}
add_action( 'woocommerce_product_thumbnails','woocommerce_product_thumbnails_single_product' );
已测试并有效