简体   繁体   English

在Woo Commerce中保存动态自定义结帐字段

[英]Saving dynamic custom checkout fields in Woo commerce

I'm new to PHP and I ordinarily search the net for articles and tutorials when I have a coding issue. 我是PHP的新手,通常在遇到编码问题时在网上搜索文章和教程。 Unfortunately I have been searching for a couple of days while trying my own fixes without luck, so I hope someone can help. 不幸的是,我一直在寻找自己的修补程序而没有运气的几天,所以希望有人能提供帮助。

I am building a site with Wordpress and Woo commerce. 我正在用Wordpress和Woo电子商务建立一个网站。 I have followed some tutorials like this ( http://www.portmanteaudesigns.com/blog/2015/02/04/woocommerce-custom-checkout-fields-email-backend/ ) to add custom fields to the woo commerce checkout. 我遵循了一些类似这样的教程( http://www.portmanteaudesigns.com/blog/2015/02/04/woocommerce-custom-checkout-fields-email-backend/ ),将自定义字段添加到woo Commerce Checkout。

I need to add a field to the checkout for every item in the cart, so that information can be collected per item (so if quantity of items in the cart is 6, there will be 6 fields for information to be provided). 我需要为购物车中的每个商品的结帐栏添加一个字段,以便可以收集每个商品的信息(因此,如果购物车中的商品数量为6,将提供6个字段供您提供信息)。

Using the below code, for every item in the cart, a field is added: 使用以下代码,为购物车中的每个商品添加一个字段:

/**
 * Add the field to the checkout
 **/
add_action( 'woocommerce_after_order_notes', 'wordimpress_custom_checkout_field' );

function wordimpress_custom_checkout_field( $checkout ) {

 //Check if Shoe in Cart (UPDATE WITH YOUR PRODUCT ID)
 $shoe_in_cart = wordimpress_is_conditional_product_in_cart();

 //Shoe is in cart so show additional fields
 if ( $shoe_in_cart === true ) {
 echo '<div id="my_custom_checkout_field">';

 foreach ($_SESSION['value'] as $key => $value) {
    $n = 1;
    $itemQuantity = $value['quantity'];
    $ProductID = $value['product_id'];
    $_product = $value['data']->post;


        for($QuantityCheck = 0; $QuantityCheck < $itemQuantity; $QuantityCheck++) {
            $LabelTitle = ($_product->post_title. ' '.$n.' - shoe name');
            woocommerce_form_field( 'inscription_textbox'.$ProductID.$n, array(
                'type'  => 'text',
                'required'  => true,    
                'placeholder'   => __('Shoe name'),     
                'class' => array( 'inscription-text form-row-wide' ),
                'label' => __( $LabelTitle ),
                ), $checkout->get_value( 'inscription_textbox'.$ProductID.$n ) );
            $checkoutFieldName = ('inscription_textbox'.$ProductID.$n);
            $_SESSION['checkoutFields'] [] = $checkoutFieldName;
            $n ++;
        }

 }

 echo '</div>';

}
}

I tried to attach a picture of the result but my reputation isn't high enough (the code works and the dynamic additional fields are added). 我尝试附加结果的图片,但是我的声誉不够高(代码有效,并且添加了动态附加字段)。

My problem lies in saving these dynamic custom fields to the database or make them show on the order confirmation page. 我的问题在于将这些动态自定义字段保存到数据库中或使它们显示在订单确认页面上。

This is the latest code I used to try and save the field information to the database: 这是我用来尝试将字段信息保存到数据库的最新代码:

add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {

    foreach($_SESSION['checkoutFields'] as $key => $value){
    if( isset($_POST[$value]) ) {
        add_post_meta($order_id, $value, $_POST[$value]);
    }
}

}

Please help. 请帮忙。

Thanks in anticipation 谢谢你的期待

It has been a while since the question was posted, but I am sure this can help others so let me answer on how to do it. 自问题发布以来已经有一段时间了,但是我确信这可以帮助其他人,所以让我回答如何做。

This is what you're doing: 这是您正在做的:

function my_custom_checkout_field_update_order_meta( $order_id ) {

foreach($_SESSION['checkoutFields'] as $key => $value){
if( isset($_POST[$value]) ) {
    add_post_meta($order_id, $value, $_POST[$value]);
}}}

But guess what? 但猜猜怎么了? You don't have to do that. 您不必这样做。 You don't even need to use foreach WordPress have a core function called maybe_serialize Which automatically checks whether the value is an array. 您甚至不需要使用foreach WordPress就有一个名为maybe_serialize的核心功能,该功能会自动检查该值是否为数组。

So you would do it like this: 因此,您可以这样做:

function my_custom_checkout_field_update_order_meta( $order_id ) {    
    if( isset($_POST[$value]) ) {
        add_post_meta($order_id, $value, $_POST[$value]);
    }}

Here is another amazing answer that describes the answer in great details. 这是另一个令人惊奇的答案,详细描述了答案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM