添加到购物车可预订的产品URL - WooCommerce Bookings

时间:2016-11-25 09:24:10

标签: wordpress get woocommerce hook-woocommerce woocommerce-bookings

我正在使用Woocommerce Bookings

如何通过网址添加到购物车预订产品?

/?add-to-cart=[n.product]

如何通过变体传递可用日期?

由于

1 个答案:

答案 0 :(得分:0)

我最近遇到了同样的问题,但我发现了一个解决方案。

仅仅通过在URL的末尾添加正确的变量来添加可预订的产品是不可能的,因为WooCommerce Bookings期望通过POST方法发送数据,如插件的源代码所示:

// woocommerce-bookings/includes/class-wc-booking-cart-manager.php : ln 256
$booking_form                       = new WC_Booking_Form( $product );
$cart_item_meta['booking']          = $booking_form->get_posted_data( $_POST );
$cart_item_meta['booking']['_cost'] = $booking_form->calculate_booking_cost( $_POST );

// Create the new booking
$new_booking = $this->create_booking_from_cart_data( $cart_item_meta, $product_id );

// woocommerce-bookings/includes/class-wc-booking-cart-manager.php : ln 276
private function create_booking_from_cart_data( $cart_item_meta, $product_id, $status = 'in-cart' ) {
    // Create the new booking
    $new_booking_data = array(
        'product_id'    => $product_id, // Booking ID
        'cost'          => $cart_item_meta['booking']['_cost'], // Cost of this booking
        'start_date'    => $cart_item_meta['booking']['_start_date'],
        'end_date'      => $cart_item_meta['booking']['_end_date'],
        'all_day'       => $cart_item_meta['booking']['_all_day'],
    );

而不是:

<a href="/?add-to-cart=product_id">Add to cart</a>

您需要创建一个包含多个隐藏字段的表单,并将“添加到购物车”按钮/链接作为提交按钮。

这样的事情:

<form method="post" action="/">
    <input type="hidden" name="add-to-cart" value="product_id">
    <input type="hidden" name="wc_bookings_field_start_date_year" value="booking_start_date_year">
    <input type="hidden" name="wc_bookings_field_start_date_month" value="booking_start_date_month">
    <input type="hidden" name="wc_bookings_field_start_date_day" value="booking_start_date_day">
    <button type="submit">Add to cart</button>
</form>