Get pricing data for a bookable product with date

2019-07-22 15:23发布

I would like to show to customers the price according to a date range.

In on of my bookable product with date rage pricing I have:

  • the base price: 100$ for the first day,
  • adds $60 to the base price for two days (in a date range),
  • adds 90$ to the base price for three days (in a date range).

And so on…

This is done in the backend of the woocommerce bookable product edit settings page.

My problem is now, that I cannot find those additions (60, 90) anywhere in the database. With:

$product    = wc_get_product( $product_id );
$price      = $product->get_price();

It only return the base price (100$).

Any help in finding those date range settings somewhere in the database or where woocommerce would calculate this per default is appreciated.

Woocommerce Bookable Product Date Range Settings

2条回答
在下西门庆
2楼-- · 2019-07-22 15:59

For WooCommerce Bookings you have everything needed in the WC_Product_Booking object:

// Get an instance of the WC_Product object (Here a WC_Product_Booking object)
$product = wc_get_product( $product_id );

// Get the base price
$base_price = $product->get_price();

// Get all related protected data in an array
$product_data = $product->get_data();

// Get the additional pricing data for this bookable product (array)
$product_pricing = $product_data['pricing'];

// iterating through each pricing row
foreach($product_pricing as $key => $princing ){
    $pricing_type           = $princing['type'];
    $pricing_base_cost      = $princing['base_cost']; // <= this is the price you are looking for
    $pricing_base_modifier  = $princing['base_modifier'];
    $pricing_cost           = $princing['cost']; 
    $pricing_modifier       = $princing['modifier'];
    $pricing_from           = $princing['from'];
    $pricing_to             = $princing['to'];
}

// Raw pricing data output (for tests)
echo '<pre>'; print_r($product_pricing); echo '</pre>';

Now in the database you can find this data in wp_post_meta table under _wc_booking_pricing meta_key… So from the product ID you can access it too with:

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

// Raw pricing data output (for tests)
echo '<pre>'; print_r($product_pricing); echo '</pre>';
查看更多
做个烂人
3楼-- · 2019-07-22 16:17

My final solution looks like this:

        $product_pricing = get_post_meta( $product_id, '_wc_booking_pricing', false);
        foreach ($product_pricing as $pricing) {
            if (is_array($pricing)){
                foreach ($pricing as $price) {
                    $cost += $price['base_cost'];
                    }
                }
            }
        }
查看更多
登录 后发表回答