最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Fetch Product information in WooCommerce

programmeradmin1浏览0评论

I am using below function to display product information in Shop Page.

function woocommerce_template_loop_product_title() {
        echo '<h5 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . ' </h5>'; 
        echo '<p>' . get_the_excerpt() . '</p>';
    }

I can fetch product shot description using get_the_excerpt(). But I would like to fetch all product information like size, price, color.

I can see below information in Product page.

I would like to fetch these information in Shop page with all products.

How can I fetch all product information ?

I am using below function to display product information in Shop Page.

function woocommerce_template_loop_product_title() {
        echo '<h5 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . ' </h5>'; 
        echo '<p>' . get_the_excerpt() . '</p>';
    }

I can fetch product shot description using get_the_excerpt(). But I would like to fetch all product information like size, price, color.

I can see below information in Product page.

I would like to fetch these information in Shop page with all products.

How can I fetch all product information ?

Share Improve this question edited Oct 24, 2020 at 0:18 abu abu asked Oct 23, 2020 at 3:13 abu abuabu abu 2031 silver badge9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Product Attributes without Variation Don't Display in Shop page & Product Page

Add Attribute and Variations in Product ( Refer blog )

Note : Product variations is Used Only Variable Product.

And Then after put this code in function.php

   add_action( 'woocommerce_after_shop_loop_item_title', 'bbloomer_echo_stock_variations_loop' );

function bbloomer_echo_stock_variations_loop(){
    global $product;
    if ( $product->get_type() == 'variable' ) {
        foreach ( $product->get_available_variations() as $key ) {
            $attr_string = array();
            foreach ( $key['attributes'] as $attr_name => $attr_value ) {
                $attr_string[] = $attr_value;
                $arr_names[] = $attr_name;
            }

            $attr_color = $arr_names[0];
            $attr_size = $arr_names[1];
            $strle = 'attribute_pa_';

            $attr_name_a  = substr($attr_color, strlen($strle));
            $attr_name_a2  = substr($attr_size, strlen($strle));

           
            if ( $key['max_qty'] > 0 ) { 
              echo '<div><p>' .$attr_name_a.' : '.$attr_string[0].' , '.$attr_name_a2.' : '.$attr_string[1].' , ' . $key['max_qty'] . ' in stock</p></div>'; 
            } else { 
              echo '<div><p>' .$attr_name_a.' : '. $attr_string[0].',' .$attr_name_a2.' : '.$attr_string[1].'   out of stock</p></div>'; 
            }
        }
    }
}

Look Like Below image :

The Complete woocommerce Product Description add in shop page.

  add_action( 'woocommerce_after_shop_loop_item_title', 
   'wc_add_short_description' );

  function wc_add_short_description() {
    global $product;

    ?>
        <div itemprop="description">
            <?php echo apply_filters( 'woocommerce_short_description', $product->post-> post_excerpt ) ?>
        </div>
    <?php
}

This code add in function.php file

发布评论

评论列表(0)

  1. 暂无评论