douguang9014 2018-06-27 03:33
浏览 55
已采纳

显示Woocommerce产品定制循环的价格

I'm using Woocommerce Bookings with only a base cost filled. No other rules. How do I display product price in a loop like this?

<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
ID is: <?php the_ID(); ?>
Title is: <?php the_title(); ?>
Price: <?php echo esc_html( get_post_meta( get_the_ID(), '_regular_price', true ) ); ?>

<?php endwhile; wp_reset_query(); ?>

The price field didn't show anything

UPDATE

another code I tried:

<?php
global $woocommerce;
$product = new WC_Product_Booking($productID);
$base_price = $product->get_price();
$product_data = $product->get_data();
$product_pricing = get_post_meta( $product_id, '_wc_booking_pricing', true);
?>

<?php echo $product_pricing; ?> 
<?php echo $base_price;?>

both also return the value of zero

ANOTHER TRY

<?php
global $woocommerce;
$product = new WC_Product_Booking($productID);
$product = wc_get_product( $product_id );

$base_price = $product->get_price();
$product_data = $product->get_data();

$product_pricing = $product_data['pricing'];
foreach($product_pricing as $key => $princing ){
   $pricing_type           = $princing['type'];
   $pricing_base_cost      = $princing['base_cost'];
   $pricing_base_modifier  = $princing['base_modifier'];
   $pricing_cost           = $princing['cost']; 
   $pricing_modifier       = $princing['modifier'];
   $pricing_from           = $princing['from'];
   $pricing_to             = $princing['to'];
}

$pricing_data = get_post_meta( $product_id, '_wc_booking_pricing', false); ?>

<?php echo $pricing_data; ?>

nothing works :( they're all either showing blanks or showing zero. Can anyone help point in the right direction?

many thanks

  • 写回答

2条回答 默认 最新

  • duanjing1276 2018-06-27 08:22
    关注

    Try (from the docs):

    $product = new WC_Product_Booking($productID);
    //don't know if you need 'echo'
    echo $product->get_price_html()
    

    If not, I think the logic of 2nd snippet it's ok, but probably productID != postID, so another solution would be:

    • find the postID for that productID
    • call get_post_meta( postID, '_wc_booking_pricing', true);
    • print the price

    EDIT


    My answer's too long for being posted in the comments.

    Are you sure that you are set correctly the product from wp-admin?

    In order to do the second suggestion, you need access to the database. Get the ID of the post(product) in wp-admin when you create/modify it, you should have in the url something like {URL}/wp-admin/id=123.

    Once you have the ID go in wp_post_meta table in the DB and look for all the fields with that post_id, and check if you can find the correct price.

    Once you find it, you can call get_post_meta(correctID,correctKey)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计