2

我在用着Woocommerce Bookings

如何通过 URL 添加到购物车可预订产品?

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

我怎样才能通过变化的可用日期?

谢谢

4

1 回答 1

1

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

仅通过在 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>
于 2017-05-07T18:21:30.617 回答