在 Woocommerce 单个产品页面中显示带有自定义简码的产品数据
Display product data with custom shortcodes in Woocommerce single product pages
在 Woocommerce 中,我使用了 3 种不同的简码功能来显示:
- 产品类别列表,
- 产品 SKU
- 特定属性的值。
我的 3 函数如下所示:
function display_woo_sku() {
global $product;
return $product->get_sku();
}
add_shortcode( 'woo_sku', 'display_woo_sku' );
function display_woo_farve() {
global $product;
return $product->get_attribute( 'farve' );
}
add_shortcode( 'woo_farve', 'display_woo_farve' );
function display_woo_style() {
global $post, $product;
$categ = $product->get_categories();
return $categ;
}
add_shortcode( 'woo_style', 'display_woo_style' );
这确实有效,但在更新产品时会抛出错误,并使 Facebook Pixel 插件出现问题。
我看不出我的代码有什么问题,但肯定有问题,因为它与插件和第三方像素工具冲突。
感谢任何帮助。
由于多种原因,您的代码出现了一些错误……试试这个:
function display_woo_sku() {
if( ! is_admin() && ! is_product() ) return;
global $post;
// Get an instance of the WC_Product Object
$product = wc_get_product( $post->ID );
return $product->get_sku();
}
add_shortcode( 'woo_sku', 'display_woo_sku' );
function display_woo_attr_farve() {
if( ! is_admin() && ! is_product() ) return;
global $post;
// Get an instance of the WC_Product Object
$product = wc_get_product( $post->ID );
return $product->get_attribute( 'farve' );
}
add_shortcode( 'woo_farve', 'display_woo_attr_farve' );
function display_woo_cats() {
if( ! is_admin() && ! is_product() ) return;
global $post;
// $categ = $product->get_categories(); // <== <== <== <== IS DEPRECATED
return wc_get_product_category_list( $post->ID );
}
add_shortcode( 'woo_cats', 'display_woo_cats' );
此代码在您的活动子主题(或主题)的 function.php 文件中。 已测试并有效。
它应该可以解决您的问题……
在 Woocommerce 中,我使用了 3 种不同的简码功能来显示:
- 产品类别列表,
- 产品 SKU
- 特定属性的值。
我的 3 函数如下所示:
function display_woo_sku() {
global $product;
return $product->get_sku();
}
add_shortcode( 'woo_sku', 'display_woo_sku' );
function display_woo_farve() {
global $product;
return $product->get_attribute( 'farve' );
}
add_shortcode( 'woo_farve', 'display_woo_farve' );
function display_woo_style() {
global $post, $product;
$categ = $product->get_categories();
return $categ;
}
add_shortcode( 'woo_style', 'display_woo_style' );
这确实有效,但在更新产品时会抛出错误,并使 Facebook Pixel 插件出现问题。
我看不出我的代码有什么问题,但肯定有问题,因为它与插件和第三方像素工具冲突。
感谢任何帮助。
由于多种原因,您的代码出现了一些错误……试试这个:
function display_woo_sku() {
if( ! is_admin() && ! is_product() ) return;
global $post;
// Get an instance of the WC_Product Object
$product = wc_get_product( $post->ID );
return $product->get_sku();
}
add_shortcode( 'woo_sku', 'display_woo_sku' );
function display_woo_attr_farve() {
if( ! is_admin() && ! is_product() ) return;
global $post;
// Get an instance of the WC_Product Object
$product = wc_get_product( $post->ID );
return $product->get_attribute( 'farve' );
}
add_shortcode( 'woo_farve', 'display_woo_attr_farve' );
function display_woo_cats() {
if( ! is_admin() && ! is_product() ) return;
global $post;
// $categ = $product->get_categories(); // <== <== <== <== IS DEPRECATED
return wc_get_product_category_list( $post->ID );
}
add_shortcode( 'woo_cats', 'display_woo_cats' );
此代码在您的活动子主题(或主题)的 function.php 文件中。 已测试并有效。
它应该可以解决您的问题……