Ring ring
Hướng dẫn hiển thị các thuộc tính của sản phẩm. - Zayquara1.Cf, Website giải trí,thông tin game
ZAYQUARA1.CF
Icon 1

Hướng dẫn hiển thị các thuộc tính của sản phẩm.

Trước thấy có bạn hỏi cách để hiển thị các thuộc tính sản phẩm ra ngoài trang sản phẩm, cũng lâu rồi chưa chia sẻ cái gì mới, nên hôm nay mình sẽ share cho các bạn một đoạn function để có thể hiện thị được các thuộc tính sản phẩm nhé.

LƯU Ý: CÁC BẠN PHẢI CHỌN THUỘC TÍNH Ở CHẾ ĐỘ LƯU TRỮ.

Hiển thị các thuộc tính trên trang sản phẩm.

Các bạn chỉ cần thêm đoạn function này vào file function của mình là được nhé:

function thuoctinh_pttuan(){
   global $product;
   $attributes = $product->get_attributes();
   if ( ! $attributes ) {
       return;
   }
   $display_result = '';
   foreach ( $attributes as $attribute ) {
       if ( $attribute->get_variation() ) {
           continue;
       }
       $name = $attribute->get_name();
       if ( $attribute->is_taxonomy() ) {
           $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
           $cwtax = $terms[0]->taxonomy;
           $cw_object_taxonomy = get_taxonomy($cwtax);
           if ( isset ($cw_object_taxonomy->labels->singular_name) ) {
               $tax_label = $cw_object_taxonomy->labels->singular_name;
           } elseif ( isset( $cw_object_taxonomy->label ) ) {
               $tax_label = $cw_object_taxonomy->label;
               if ( 0 === strpos( $tax_label, 'Product ' ) ) {
                   $tax_label = substr( $tax_label, 8 );
               }
           }
           $display_result .= '<span>'.$tax_label . ': ';
           $tax_terms = array();
           foreach ( $terms as $term ) {
               $single_term = '<a href="'.get_term_link($term->slug, $term->taxonomy).'">'.esc_html( $term->name ).'</a>';
               array_push( $tax_terms, $single_term );
           }
           $display_result .= implode(', ', $tax_terms) . '</span>';
       } else {
           $display_result .= $name . ': ';
           $display_result .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br>';
       }
   }
   echo $display_result;
}
add_action('woocommerce_product_meta_end','thuoctinh_pttuan',11);

Hook mình sử dụng là hook chỗ hiển thị danh mục sản phẩm, nếu như các bạn có ẩn nó đi thì có thể hook vào vị trí khác nhé. Hoặc hook vào bất kì đâu các bạn thích trong trang sản phẩm là được.

Chỉ cần thay đoạn này bằng hook mà mình mong muốn

add_action('woocommerce_product_meta_end','thuoctinh_pttuan',11);

Back to posts
Comments:

Post a comment

TAGS - SEO
Tags: http://zayquara1.xtgem.com/blog
SEO : Bạn đến từ :