Get the product category name and description in Woocommerce Single Product page

爱⌒轻易说出口 提交于 2021-01-28 04:39:54

问题


I've been using the WooCommerce Codex, but I can't seem to get the data to display. I simply want to display the product category and descriptions to display on single product pages for my own custom layout like so.

<?php global $product; echo $product->get_attributes; ?>

<?php global $product; echo $product->get_short_description; ?>

回答1:


As you can have many product categories for a product, you will need to use a foreach loop. The $term variable is the WP_Term object…

<?php 
    foreach( wp_get_post_terms( get_the_id(), 'product_cat' ) as $term ){
        if( $term ){
            echo $term->name . '<br>'; // product category name
            if ($term->description)
                echo $term->description . '<br>'; // Product category description
        }
    }
?>

Tested and works




回答2:


Use below code in your function.php file of your current theme

add_action('woocommerce_single_product_summary', function() {
    global $product;
    echo $product->list_attributes();
}, 25);

Next Use below code where you want product attributes to display

if ( ! defined( 'ABSPATH' ) ) 
{
    exit; // Exit if accessed directly
} 
$has_row    = false;
$attributes = $product->get_attributes();
ob_start();

just print_r($attributes) so you can get all attributes of specific product then just get attributes which you want to display.that's it



来源:https://stackoverflow.com/questions/51130745/get-the-product-category-name-and-description-in-woocommerce-single-product-page

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