通过URL添加到购物车预订产品 - WooCommerce预订 [英] Add to cart bookable product by URL - WooCommerce Bookings

查看:193
本文介绍了通过URL添加到购物车预订产品 - WooCommerce预订的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Woocommerce预订

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

How to add to cart bookable product by URL?

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

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

How can I pass an avaiable date with variations?

谢谢

Thanks

推荐答案

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

I ran into the same problem recently but I discovered a solution.

无法添加可预订产品只需在URL末尾添加正确的变量,因为WooCommerce Bookings希望通过 POST 方法发送数据,就像您在插件源处看到的那样:

It's not possible to add a bookable product just by adding the right variables to the end of the URL because WooCommerce Bookings expects data to be sent via the POST method as you can see at the plugin's source:

// 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 );



and:

// 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'],
    );

取而代之:

Instead of this:

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

您需要使用添加到购物车按钮创建一个带有多个隐藏字段的表单/链接作为提交按钮。

You need to create a form with a number of hidden fields with the 'Add to cart' button/link as the submit button.

类似这样:

Something like this:

<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>

这篇关于通过URL添加到购物车预订产品 - WooCommerce预订的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆