简体   繁体   English

在 Woocommerce 中添加到购物车操作时,将自定义选择的日期添加到购物车项目

[英]Add a custom chosen date to cart item on add to cart action in Woocommerce

I'm building a shop with Woocommerce where you are supposed to have the opportunity to order different menus on different days.我正在与 Woocommerce 建立一家商店,您应该有机会在不同的日子订购不同的菜单。

So I created an array with dates from where I with Javascript create a Tabbed menu where each tab has a data-attribute with the date to the relevant day.所以我创建了一个带有日期的数组,我使用 Javascript 创建了一个选项卡式菜单,其中每个选项卡都有一个data-attribute ,其中包含相关日期的日期。

So what I want to achieve is, that when I for example click the tab "thursday 6.sept" (then its active) and then choose an Item (in this case a menu) and click "add to cart" - I want to add the date to that Item and send it to the cart.所以我想要实现的是,例如,当我单击选项卡“thursday 6.sept”(然后是活动的)然后选择一个项目(在这种情况下是一个菜单)并单击“添加到购物车”时 - 我想将日期添加到该项目并将其发送到购物车。

How can I achieve that or is it even possible?我怎样才能做到这一点,或者甚至有可能吗?

In the code below, the first function will display a hidden input field inside the add to cart form.在下面的代码中,第一个函数将在添加到购物车表单中显示一个隐藏的输入字段。

Inside your existing javascript code, you will have to add some more JS code that will get your chosen date value and will set it in this hidden field (as you haven't provided any related code) .在您现有的 javascript 代码中,您必须添加更多 JS 代码来获取您选择的日期值并将其设置在此隐藏字段中(因为您尚未提供任何相关代码) This way when the product will be added to cart, the chosen date value will be posted.这样,当产品被添加到购物车时,所选的日期值将被发布。

Then all other functions will add the chosen date as custom cart item data and will display it (optionally) in cart and checkout pages.然后所有其他功能将添加所选日期作为自定义购物车项目数据,并将其(可选)显示在购物车和结帐页面中。 Then when the order will be placed, the chosen date will be saved in order items and will be displayed everywhere.然后当下订单时,选择的日期将保存在订单项目中并显示在任何地方。

// Add a hidden input field inside the add to cart form
add_action('woocommerce_single_product_summary','custom_single_product_summary', 2 ); // single product pages
function custom_single_product_summary(){
    global $product;

    if( $product->is_type('variable') ){
        add_action('woocommerce_before_single_variation','add_hidden_input_product_field', 30 );
    } else {
        add_action('woocommerce_before_add_to_cart_button','add_hidden_input_product_field', 30 );
    }
}
function add_hidden_input_product_field(){
    echo '<div class="hidden-field"><input type="hidden" id="chosen_date" name="chosen_date" value=""></div>';
}

// add the chosen date as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data_to_cart', 20, 3);
function add_custom_field_data_to_cart($cart_item_data, $product_id, $variation_id) {
    if (isset($_POST['chosen_date']) && !empty($_POST['chosen_date'])) {
        $cart_item_data['chosen_date'] = sanitize_text_field( $_POST['chosen_date'] ); // Add the date to cart item data
        $cart_item_data['unique_key'] = md5(microtime().rand()); // Make each item unique
    }
    return $cart_item_data;
}

// Display in cart items the chosen date
add_filter('woocommerce_get_item_data', 'display_custom_item_data', 10, 2);
function display_custom_item_data($cart_item_data, $cart_item) {
    if (isset($cart_item['chosen_date'])) {
        $cart_item_data[] = array(
            'name'  => __("Chosen date", "woocommerce"),
            'value' => date('Y-m-d', strtotime($cart_item['chosen_date'])), // Formatting date (optional)
        );
    }
    return $cart_item_data;
}

// Save and display chosen date in orders and email notifications (everywhere)
add_action('woocommerce_checkout_create_order_line_item', 'custom_fields_update_order_item_meta', 20, 4);

function custom_fields_update_order_item_meta($item, $cart_item_key, $values, $order) {
    if (isset($values['chosen_date'])) {
        $date = date('Y-m-d', strtotime($values['chosen_date'])); // Formatting date (optional)
        $item->update_meta_data(__('Chosen date', 'woocommerce'), $date); // Save the date
    }
}

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 URL 添加到购物车上的 WooCommerce 自定义购物车商品价格 - WooCommerce custom cart item price on add-to cart via the URL 将自定义购物车商品值添加到WooCommerce订单商品元数据 - Add a custom cart item value to WooCommerce order item meta data WooCommerce 添加<custom>产品到购物车</custom> - WooCommerce Add <custom> products to cart 在 WooCommerce 中添加自定义字段作为购物车项目元和订单项目元 - Add custom fields as cart item meta and order item meta in WooCommerce 在WooCommerce中将自定义产品添加到购物车 - Add custom product to cart in WooCommerce 将多个自定义数据数组添加到 WooCommerce 自定义 Ajax 上的购物车项目添加到购物车 - Add multiple custom data arrays to cart item on WooCommerce custom Ajax add to cart 在第一个项目上禁用ajax添加到购物车,并在Woocommerce上自定义重定向 - Disable ajax add to cart on first item and custom redirect it on Woocommerce 在 WooCommerce 订单和电子邮件中添加和显示自定义购物车项目数据 - Add and display custom cart item data in WooCommerce orders and emails 自定义Woocommerce购物车添加的商品计数有误 - Custom Woocommerce cart add has wrong item count 通过使用 Woocommerce 中的自定义元数据的 Get 请求将商品添加到购物车 - Add item to cart through a Get request with custom meta data in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM