简体   繁体   English

在Woocommerce中根据购物车数量添加自定义结帐字段

[英]Add a custom checkout field based on cart items quantity in Woocommerce

Ultimately I am trying to add a field to checkout in woocommerce when a customer has over a certain quantity in their checkout. 最终,当我的客户在结账时超过一定数量时,我会尝试在woocommerce中添加一个结帐字段。 For this example I am using 500 and just trying to get a message to show at the bottom of checkout. 对于这个例子,我使用的是500,只是尝试在结帐的底部显示一条消息。

I have been trying various variations of code and this is what I have so far. 我一直在尝试各种代码变体,这是我到目前为止所拥有的。

add_action( 'woocommerce_after_checkout_form', 'woocommerce_add_quantity_message', 12 );

function woocommerce_add_quantity_message() {
global $woocommerce;
$total_products = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) 
{
$total_products += $values['quantity'];
}

if($total_products >=500 )
{
echo '<div class="card_discount_message"><p>MORE THAN 500 WOAHHHH</p></div>';
}

Any pointers would be greatly appreciated. 任何指针都将非常感激。 I have also tried modifying the code found here with no luck woocommerce add fee to cart based on quantity 我也尝试修改这里找到的代码没有运气woocommerce根据数量添加到购物车的费用

Updated 更新

You can not set a custom checkout field outside of the checkout form. 您无法在结帐表单之外设置自定义结帐字段。

So this custom fields can only be at the end inside the checkout form, which is after the order notes. 因此,这个自定义字段只能位于结帐表单的末尾,这是在订单备注之后。

Try the following code that will display a custom checkout field once specific cart item quantity is reached (after order notes) : 尝试以下代码,一旦达到特定购物车商品数量(订单备注后) ,将显示自定义结帐字段:

add_action( 'woocommerce_after_order_notes', 'add_field_based_quantity', 20, 1 );
function add_field_based_quantity( $checkout ) {
    if( WC()->cart->get_cart_contents_count() >= 500 ) {
        echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

        woocommerce_form_field( 'my_field_name', array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Fill in this field'),
            'placeholder'   => __('Enter something'),
        ), $checkout->get_value( 'my_field_name' ));

        echo '</div>';
    }
}

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

在此输入图像描述

The code is based on Woocommerce official documentation: Adding a custom special checkout field 该代码基于Woocommerce官方文档: 添加自定义特殊结帐字段

So you will see in that documentation how to save this custom field in the Order and how you will be able to display it too… 因此,您将在该文档中看到如何在订单中保存此自定义字段以及您将如何显示它...


You can add a custom notice at the end if you wish with the following code: 如果您愿意,可以使用以下代码在末尾添加自定义通知:

add_action( 'woocommerce_after_checkout_form', 'woocommerce_add_quantity_message', 20 );
function woocommerce_add_quantity_message() {
    if( WC()->cart->get_cart_contents_count() >= 500 ) {
        wc_print_notice( __("MORE THAN 500 WOAHHHH !", "woocommerce"), 'notice');
    }
}

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

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

相关问题 根据自定义字段和数量阈值更改 WooCommerce 购物车商品价格 - Change WooCommerce cart item price based on a custom field and quantity threshold 在Woocommerce购物车和结帐项目中显示自定义字段的价值 - Display Custom Field's Value in Woocommerce Cart & Checkout items Woocommerce在结帐页面上获取购物车中特定商品的数量 - Woocommerce get quantity of specific items in cart on checkout page 自定义添加到购物车按钮,将多个产品添加到购物车中,数量:woocommerce - Custom add to cart button to add multiple product into cart with quantity :woocommerce 设置自定义添加到购物车默认数量(Woocommerce 购物车页面除外) - Set custom Add to cart default quantity except on Woocommerce cart page 根据 Woocommerce 中的产品类别添加自定义结帐字段 - Add custom checkout field based on product category in Woocommerce 根据自定义字段值隐藏添加到购物车按钮-woocommerce - Hide add to cart button based on custom field value - woocommerce WooCommerce:根据单个商品数量添加折扣 - WooCommerce: Add a discount based on individual items quantity Woocommerce - 将数量为 0 的商品添加到购物车? - Woocommerce - Items with a quantity of 0 are added to the cart? 将数量字段添加到 WooCommerce 商店页面上的 Ajax 添加到购物车按钮 - Add a quantity field to Ajax add to cart button on WooCommerce shop page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM