trường tùy biến (ACF) + gallery product
trường tùy biến (ACF)
// Add custom field to shop loop,ok với product archive mặt định của
add_action( ‘woocommerce_after_shop_loop_item_title’,‘ins_woocommerce_product_excerpt’,35,2);
function ins_woocommerce_product_excerpt() {
global $post;
if ( is_front_page() || is_shop() || is_product_category() || is_product_tag() ) {
echo ‘<span class=”excerpt”>’;
echo ‘<span style=”color:#0000ff;”><strong>’.get_post_meta( $post–>ID,‘cf_inverter’,true ).‘</strong></span>’.‘</br>’;
echo ‘Công suất:’.‘<span style=”color:#ff0000;”><strong>’.get_post_meta( $post–>ID,‘cf_ma_luc’,true ).‘</strong> </span>’.‘</br>’;
echo ‘Môi chất: ‘.get_post_meta( $post–>ID,‘cf_moi_chat’,true ).‘</br>’;
echo get_post_meta( $post–>ID,‘cf_bao_hanh’,true );
echo ‘</span>’;
}
}
Trong đó hàm get_post_meta( $post->ID,’cf_inverter’,true ) lấy giá trị acf filed có name là cf_inverter như hình bên dưới
Kết quả trang archive sản phẩm như sau

Nếu thêm ACF trên không được thì hãy dùng đoạn code này
function bang_gia() { ?>
<?php
$args = array(
‘post_type’ => ‘product’,
‘posts_per_page’ => 10,
);
?>
<?php $getposts = new WP_query( $args);?>
<?php global $wp_query; $wp_query->in_the_loop = true; ?>
<div class=”banggia”>
<table id=”customers”>
<tr>
<th>Kích thước</th>
<th>Kiểm định</th>
<th>Cấp màu</th>
<th>Tinh khiết</th>
<th>Huỳnh quang</th>
<th>Giá niêm yết</th>
<th>Giá ưu Đãi</th>
</tr>
<?php while ($getposts->have_posts()) : $getposts->the_post(); ?>
<?php global $product; $gia1=$product->get_regular_price();$gia2=$product->get_price(); ?>
<tr>
<td><?php echo the_field( ‘kich-thuoc’,$post->ID,true ) ?></td>
<td><?php echo the_field( ‘kiem-dinh’,$post->ID,true ) ?></td>
<td><?php echo the_field( ‘cap-mau’,$post->ID,true ) ?></td>
<td><?php echo the_field( ‘tinh-khiet’,$post->ID,true ) ?></td>
<td><?php echo the_field( ‘huynh-quang’,$post->ID,true ) ?></td>
<td class=”gach”><?php echo number_format($gia1) ?> đ</td>
<td class=”do”><?php echo number_format($gia2)?> đ</td>
</tr>
<?php endwhile; wp_reset_postdata();?>
</table>
</div>
<?php }
add_shortcode( ‘banggia’, ‘bang_gia’ );
Chúc bạn thành công
phân trang
function bang_gia4() {
$args = array(
‘post_type’ => ‘product’,
‘meta_key’ => $meta_key,
‘orderby’ => ‘meta_value’,
‘order’ => $order,
‘posts_per_page’ => 20,
‘paged’ => get_query_var(‘paged’),
‘tax_query’ => array(
array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => ‘kim-cuong-thien-nhien-roi’,
),
)
);
?>
<?php $getposts = new WP_query( $args);?>
<?php global $wp_query; $wp_query->in_the_loop = true; ?>
<div class=”banggia”>
<table id=”customers” class=”table-auto”>
<thead>
<tr>
<th>Kiểu dáng</th>
<th>Viên chủ</th>
<th>Viên tấm</th>
<th>Màu sắc</th>
<th>Giá niêm yết</th>
<th>Giá ưu Đãi</th>
</tr>
</thead>
<tbody>
<?php while ($getposts->have_posts()) : $getposts->the_post();
global $product;
$gia1 = $product->get_regular_price();
$gia2 = $product->get_price();
?>
<tr>
<td><?php if(!empty(get_field(‘kieu_dang’))){ ?><a href=”<?php the_permalink(); ?>”><?php echo the_field(‘kieu_dang’,$post->ID,true); ?></a><?php } ?></td>
<td><?php if(!empty(get_field(‘vien_chu’))){ ?><a href=”<?php the_permalink(); ?>”><?php echo the_field(‘vien_chu’,$post->ID,true); ?></a><?php } ?></td>
<td><?php if(!empty(get_field(‘vien_tam’))){ ?><a href=”<?php the_permalink(); ?>”><?php echo the_field(‘vien_tam’,$post->ID,true); ?></a><?php } ?></td>
<td><?php if(!empty(get_field(‘mau_sac’))){ ?><a href=”<?php the_permalink(); ?>”><?php echo the_field(‘mau_sac’,$post->ID,true); ?></a><?php } ?></td>
<td class=”gach”><?php if(!empty($gia1)){ echo number_format($gia1); ?> đ<?php } ?></td>
<td class=”do”><?php if(!empty($gia2)){ echo number_format($gia2); ?> đ<?php } ?></td>
</tr>
<?php endwhile;?>
</tbody>
</table>
<div class=”phantrang”>
<?php wp_pagenavi( array( ‘query’ => $getposts) ); wp_reset_postdata();?>
</div>
</div>
<?php }







Bình luận