[英]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.