购物车中的自定义表单字段,并在Woocommerce上的结帐中获取数据 [英] Custom form fields in cart and get the data in checkout on Woocommerce

查看:100
本文介绍了购物车中的自定义表单字段,并在Woocommerce上的结帐中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在结帐之前在购物车页面中添加发件人,收件人和消息字段.我已经在cart.php文件中添加了一些代码,但是在添加了该代码之后,购物车页面显示为空白.

Adding From,To and Message fields in cart page before checkout.I have added some code in cart.php file but after adding that code the cart page is displaying blank.

/**
* Add the order_comments field to the cart
**/
 add_action('woocommerce_cart_collaterals', 
'order_comments_custom_cart_field');

 function order_comments_custom_cart_field() {
  echo '<div id="cart_order_notes">';
  ?>
 <div class="customer_notes_on_cart">
 <label for="customer_notes_text"><?php _e('Order notes','woocommerce'); ?> 
 </label>
 <textarea id="customer_notes_text"></textarea>
 </div>
 <?php
  }
  /**
  * Process the checkout and overwriting the normal button
  *
  */
function woocommerce_button_proceed_to_checkout() {
$checkout_url = wc_get_checkout_url();
?>
   <form id="checkout_form" method="POST" action="<?php echo $checkout_url; 
  ?>">
   <input type="hidden" name="customer_notes" id="customer_notes" value="">
   <a  href="#" onclick="document.getElementById('customer_notes').value=document.getElementById('customer_notes_text').value;document.getElementById('checkout_form').submit()" class="checkout-button button alt wc-forward">
   <?php _e( 'Proceed to checkout', 'woocommerce' ); ?></a>
   </form>
   <?php
   }
  // getting the values in checkout again
 add_action('woocommerce_checkout_before_customer_details',function(){
 ?>
 <script>
 jQuery( document ).ready(function() {
jQuery('#order_comments' ).val("<?php echo 
 sanitize_text_field($_POST['customer_notes']); ?>");
  });
 </script>
<?php 
 });

在cart.php中,我在关闭form标记之前以及在form标记之后在底部添加了此代码.但是在cart.php中添加了这段代码后,我得到了空白页面.

In cart.php i have added this code at the bottom before closing the form tag as well as after the form tag.But i am getting a blank page after adding this piece of code in cart.php.

我正试图以相同的格式从收件人",收件人"和消息"字段中获取这些消息.

In the same format i am trying to get those from,to and message fields.

推荐答案

以下代码将从购物车页面中的自定义textarea字段发布估算的文本值到结帐订单备注字段:

The following code will post from a custom textarea field in cart page the imputed text value to checkout order notes field:

// Add the order_comments field to the cart
add_action( 'woocommerce_cart_collaterals', 'order_comments_custom_cart_field' );
function order_comments_custom_cart_field() {
    ?>
    <div class="customer_notes_on_cart" style="clear:both;">
    <label for="customer_notes_text"><?php _e("Order notes", "woocommerce") ?></label>
    <textarea id="customer_notes_text"></textarea></div>
    <?php
}

// Process the checkout and overwriting the normal button
 add_action( 'woocommerce_proceed_to_checkout', 'change_proceed_to_checkout', 15 );
function change_proceed_to_checkout() {
    remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );

    ?>
    <form id="checkout_form" method="POST" action="<?php echo wc_get_checkout_url(); ?>">
        <input type="hidden" name="customer_notes" id="customer_notes" value="">
        <button type="submit" class="checkout-button button alt wc-forward" style="width:100%;"><?php
        esc_html_e( 'Proceed to checkout', 'woocommerce' ) ?></button>
    </form>
    <?php
}

// Jquery script for cart and checkout pages
add_action('wp_footer', 'customer_notes_jquery' );
function customer_notes_jquery() {
    ?>
    <script>
    jQuery(function($) {
        <?php // For cart
            if( is_cart() ) : ?>
            $('#customer_notes_text').on( 'blur', function(){
                $('#customer_notes').val($(this).val());
            });
        <?php // For checkout
            elseif( is_checkout() && ! is_wc_endpoint_url() ) : ?>
            $('#order_comments' ).val("<?php echo sanitize_text_field($_POST['customer_notes']); ?>");
        <?php endif; ?>
    });
    </script>
    <?php
}

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

Code goes in function.php file of your active child theme (or active theme). Tested and work.

这篇关于购物车中的自定义表单字段,并在Woocommerce上的结帐中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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