我想根据日期范围向客户显示价格。
在我的一个具有日期愤怒定价的可预订产品中,我有:
- 基本价格:第一天 100 美元,
- 将两天(在日期范围内)的基本价格增加 60 美元,
- 在三天(日期范围内)的基础价格上加 90 美元。
等等……
这是在 woocommerce 可预订产品编辑设置页面的后端完成的。
我现在的问题是,我在数据库中的任何地方都找不到这些添加项 (60, 90)。与:
$product = wc_get_product( $product_id );
$price = $product->get_price();
它只返回基本价格(100 美元)。
如果您能帮助我们在数据库中的某处找到这些日期范围设置,或者 woocommerce 会在默认情况下计算这些日期范围设置,我们将不胜感激。
最佳答案
对于 WooCommerce 预订,您在 WC_Product_Booking
对象中拥有所需的一切:
// 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>';
现在在数据库中,您可以在 _wc_booking_pricing
meta_key
下的 wp_post_meta
表中找到此数据...因此您也可以从产品 ID 访问它与:
$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>';
关于php - 在 WooCommerce 中获取具有日期范围的可预订产品的定价数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47501425/