在自定义的单个帖子中显示Woocommerce产品属性

时间:2018-08-15 11:00:27

标签: php wordpress woocommerce product custom-taxonomy

我有一个自定义的单一帖子,并显示产品价格,产品标签和内容,现在我需要在内容下方显示product属性,但是我测试了很多代码,但我无法在单个帖子中显示。 我需要在单个帖子中为echo product属性提供一个代码。

这是我的 single.php

        <?php get_header(); ?>
    <div itemscope itemtype="http://schema.org/Product" id="single">
    <div id="similar">
    <h2><i class="icon-film"></i>similar product</h2>
    <?php
    $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'post_type' => 'product' , 'orderby' => 'rand' , 'numberposts' => 8, 'post__not_in' => array($post->ID) ) );
    if( $related ) foreach( $related as $post ) {
    setup_postdata($post); ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php if ( has_post_thumbnail() ) {the_post_thumbnail('first-thumb');} else { ?></a>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <img src="<?php bloginfo('template_directory'); ?>/assets/img/no-image.png" alt="<?php the_title(); ?>" /><?php } ?></a>
    <?php }
    wp_reset_postdata(); ?>

    <ul class="product_tags">
    <h3><i class=" icon-uniE887"></i>product tag</h3>
    <?php woocommerce_product_loop_tags() ?> 
    </ul>

    </div>
    <div id="single_body">
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>




    <div class="h1"><i class="icon-quill"></i><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    <h1><span itemprop="name">
    <?php the_title(); ?>
    </span></h1>
    </a>
    <ul class="title_icon">
    <li title="view"><i class="icon-uniE850"></i><?php if(function_exists('the_views')) { the_views(); } ?></li>
    <li title="comment"><i class="icon-bubbles2"></i><?php comments_popup_link('0 ','1 ', '% '); ?></li>
    <li title="date"><i class="icon-clock"></i><?php the_time('Y/m/d') ?></li>
    </ul>
    </div>
    <p><?php the_content()?></p>
    <?php endwhile;endif; ?>
    </div>

    <ul class="product_etc">
    <h3><i class="icon-uniE02A"></i>detail of productل</h3>
    <li itemprop="offers" itemscope itemtype="http://schema.org/Offer"><strong>price</strong>
    <div class="price" itemprop="price" ><?php echo $product->get_price_html(); ?></div></li>
    <li><strong>category</strong><span><?php echo $product->get_categories( ', ', '' . _n( '', '', sizeof( get_the_terms( $post->ID, '' ) ), '' ) . ' ', '' ); ?></span></li>
    <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>

    </ul>

    <div class="clear"></div>
    </div>
    <div class="clear"></div>
    <div class="comments-page"><?php comments_template(); ?></div>

    <?php wp_reset_query(); ?>
    <?php get_footer(); ?>

这是我的 function.php

                            <?php
    add_filter('show_admin_bar','__return_false');
    function the_content_limit($max_char, $stripteaser = 0, $more_file = '') {
    $content = get_the_content($more_link_text, $stripteaser, $more_file);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    $content = strip_tags($content,'');

    if (strlen($_GET['p']) > 0) {
    echo "<p>";
    echo $content;
    echo "</p>";
    }
    else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char ))) {
    $content = mb_substr($content, 0, $espacio);
    $content = $content;
    echo "<p>";
    echo $content;
    echo "...";
    echo "</p>";
    }
    else {
    echo "<p>";
    echo $content;
    echo "</p>";
    }
    }
    //thumbnails
    if (function_exists('add_theme_support')) {

    add_theme_support('post-thumbnails');
    }
    //comments
    if ( $_GET['post_type'] != 'page' ) {add_post_meta($post_id, 'series', '', true);}
    function mytheme_comment($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment;
    extract($args, EXTR_SKIP);

    if ( 'div' == $args['style'] ) {
    $tag = 'div';
    $add_below = 'comment';
    } else {
    $tag = 'li';
    $add_below = 'div-comment';
    }
    ?>
    <<?php echo $tag ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ) ?> id="comment-<?php comment_ID() ?>">
    <?php if ( 'div' != $args['style'] ) : ?>
    <div class="reply">
    <?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
    </div>
    <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
    <?php endif; ?>
    <div id="comments_data">
    <?php printf( __( '<span class="">نظر</span> <cite class="fn">%s</cite>' ), get_comment_author_link() ); ?>
    <?php if ( $comment->comment_approved == '0' ) : ?>
    <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></em>
    <br />
    <?php endif; ?>

    <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>">
    <?php
    /* translators: 1: date, 2: time */
    printf( __('<span class="">در تاریخ: </span><br/> %1$s'), get_comment_date(),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), '  ', '' );
    ?>
    </div>
    </div>
    <?php comment_text(); ?>


    <!---<div class="comment-author vcard">
    <?php if ( $args['avatar_size'] != 0 ) echo get_avatar( $comment, $args['avatar_size'] ); ?>

    </div>--->
    <?php if ( 'div' != $args['style'] ) : ?>
    </div>
    <?php endif; ?>
    <?php
    }
    //woocommerce
    add_action( 'after_setup_theme', 'woocommerce_support' );
    function woocommerce_support() {
    add_theme_support( 'woocommerce' );
    }

    // Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php)
    add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
    function woocommerce_header_add_to_cart_fragment( $fragments ) {
    ob_start();
    ?>
    <a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>"><?php echo sprintf (_n( '%d محصول', '%d محصول', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> به ارزش <?php echo WC()->cart->get_cart_total(); ?></a>
    <?php

    $fragments['a.cart-contents'] = ob_get_clean();

    return $fragments;
    }

    add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_product_loop_tags', 5 );
    function woocommerce_product_loop_tags() {
    global $post, $product;
    $tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) );
    echo $product->get_tags( ', ', '<span class="tagged_as">' . _n( '', '', $tag_count, 'woocommerce' ) . ' ', '.</span>' );
    }

    add_action( 'woocommerce_single_product_summary', 'product_attribute_after_price', 15 );
    function product_attribute_after_price () {
    global $product;

    // HERE add your product attribute SLUG or taxonomy
    $attribute_slug = 'color';

    $taxonomy = strpos($url, 'blog') !== false ? $attribute_slug : 'pa_' . $attribute_slug;
    $attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;
    $term_name = $product->get_attribute( $taxonomy ); // The value

    if( empty($term_name) ) return; // Exit if empty value

    // If not empty we display it
    $output  = '<div class="product-attribute '.$taxonomy.'"><p>';
    $output .= '<strong> '.$attribute_name.'</strong>: ';
    $output .= '<span> '.$term_name.'</span>';
    echo $output . '</p></div>';
    }

    ?>

1 个答案:

答案 0 :(得分:1)

要显示产品属性,您可以在任何挂钩函数或Woocommerce templates中使用它:

// Get an instance of the product object
$_product = wc_get_product( get_the_id() );

if( $_product->has_attributes() ){
    // Initializing
    $attributes = array(); 

    // Loop through product attributes
    foreach( $_product->get_attributes() as $taxonomy => $attribute ){
        // The product attribute label name
        $attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;

        // Set each product attribute with its values in an array
        $attributes[] = '<strong>'.$attribute_name.'</strong>: '.$_product->get_attribute($taxonomy);
    }

    // Display (output)
    echo '<div class="product-attributes"><span>'. implode( '</span> <span>', $attributes ) . '</span></div>';

}
  

在这里,get_the_id()可以用动态$product_id变量(这是产品的帖子ID 自定义帖子类型)代替