最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Trying to get startend dates from a booking product in cart (WooCommerce) - Stack Overflow

programmeradmin3浏览0评论

Using the WooCommerce Booking plugin, I have a "WC_Product_Booking" product in cart. I am trying to get the start and end dates if possible from this $product object (called $product_data in the code, it is also a cart item).

I can call the "get_duration()" method just fine, but I would like to also get the other data, like start/end dates, and maybe booking date.

If I call get_start_date() on the $product_data I get a function/method does not exist error, I think it's for posts of the booking type.

<?php

add_filter('booking_display_cart_before_price', 'booking_display_booking_time', 10, 2);

function booking_display_booking_time($html, $product_data)
{
    if ($product_data && is_a($product_data, 'WC_Product_Booking')) {
        //echo $product_data->get_min_date();

        echo "<pre>";
        //  print_r($booking_relationship_item);
        echo "</pre>";

        // Get the booking duration in minutes
        $booking_duration = $product_data->get_duration(); // Get duration in minutes
        // $booking_start_date = $product_data->get_booking_date();
        $booking_start_date = "";
        /*
        $start_date = $product_data->get_start_date();
        $end_date = $product_data->get_end_date();
        echo $start_date . " " . $end_date;*/

        // Get the translated booking unit (hour, day, etc.)
        $booking_unit = sprintf(__($product_data->get_duration_unit(), 'woocommerce-bookings'));

        // Format the booking duration text
        $duration_text = sprintf(__('%d %s', 'woocommerce-bookings'), $booking_duration, $booking_unit);

        // Start date
        $html .= '<div class="booking-cart-product-extra-option">
                    <span class="booking-cart-product-extra-option-label">' . __('Booking Date', 'woocommerce-bookings') . ':</span>
                    <span class="booking-cart-product-extra-option-value">' . esc_html($booking_start_date) . '</span>
                  </div>';

        // Duration
        $html .= '<div class="booking-cart-product-extra-option">
                    <span class="booking-cart-product-extra-option-label">' . __('Duration', 'woocommerce-bookings') . ':</span>
                    <span class="booking-cart-product-extra-option-value">' . esc_html($duration_text) . '</span>
                  </div>';
    }
    return $html;
}

Tried calling several methods relating to the booking plugin on the $product_data object.

发布评论

评论列表(0)

  1. 暂无评论