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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度