根据Woocommerce中购物车项目的数量添加自定义结帐字段 [英] Add a custom checkout field based on cart items quantity in Woocommerce

查看:61
本文介绍了根据Woocommerce中购物车项目的数量添加自定义结帐字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终,当客户的结帐量超过一定数量时,我试图在woocommerce中为结帐添加一个字段.在此示例中,我使用500,只是想让一条消息显示在结帐底部.

我一直在尝试各种代码变体,这就是我到目前为止所要做的.

  add_action('woocommerce_after_checkout_form','woocommerce_add_quantity_message',12);函数woocommerce_add_quantity_message(){全球$ 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> p大于500 WOAHHHH</p</div>';} 

任何指针将不胜感激.我也尝试过修改此处找到的代码,但没有运气

该代码基于Woocommerce官方文档:添加自定义特殊结帐字段

因此,您将在该文档中看到如何在订单中保存此自定义字段以及如何也可以显示它……


如果需要,可以在末尾添加自定义通知,并使用以下代码:

  add_action('woocommerce_after_checkout_form','woocommerce_add_quantity_message',20);函数woocommerce_add_quantity_message(){if(WC()->购物车-> get_cart_contents_count()> = 500){wc_print_notice(__(超过500个WOAHHHH!","woocommerce"),'notice');}} 

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并可以正常工作.

Ultimately I am trying to add a field to checkout in woocommerce when a customer has over a certain quantity in their checkout. For this example I am using 500 and just trying to get a message to show at the bottom of checkout.

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

解决方案

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). Tested and works.

The code is based on Woocommerce official documentation: Adding a custom special checkout field

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). tested and works.

这篇关于根据Woocommerce中购物车项目的数量添加自定义结帐字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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