【问题标题】:Get pricing data for a bookable product with date ranges in WooCommerce在 WooCommerce 中获取具有日期范围的可预订产品的定价数据
【发布时间】:2017-11-26 21:56:06
【问题描述】:

我想根据日期范围向客户显示价格。

在我具有日期范围定价的可预订产品中:

  • 底价:首日100美元,
  • 在基价基础上增加 60 美元(在一个日期范围内),为期两天,
  • 在三天的基本价格上增加 90 美元(在一个日期范围内)。

等等……

这是在 woocommerce 可预订产品编辑设置页面的后端完成的。

我现在的问题是,我在数据库中的任何地方都找不到这些添加 (60, 90)。与:

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

它只返回基本价格(100 美元)。

感谢您在数据库中某处查找这些日期范围设置或 woocommerce 将在默认情况下计算此日期范围设置的任何帮助。

【问题讨论】:

    标签: php mysql wordpress woocommerce woocommerce-bookings


    【解决方案1】:

    对于 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>';
    

    【讨论】:

    • 我在激活 gTranslate 时遇到了另一个问题,因为每个翻译都存在 postmeta 条目,所有翻译的价格都会相加。
    【解决方案2】:

    我的最终解决方案如下所示:

            $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'];
                        }
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2019-12-20
      • 2019-02-16
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      相关资源
      最近更新 更多