首页 > 解决方案 > 购物车中的自定义表单字段并在 Woocommerce 上结帐时获取数据

问题描述

结帐前在购物车页面中添加 From、To 和 Message 字段。我在 cart.php 文件中添加了一些代码,但添加该代码后购物车页面显示为空白。

/**
* 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 中,我在关闭表单标记之前和表单标记之后在底部添加了这段代码。但是在 cart.php 中添加这段代码后,我得到了一个空白页面。

在此处输入图像描述

以相同的格式,我试图获取那些 from、to 和 message 字段。

标签: phpjquerywordpresswoocommercecart

解决方案


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

// 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 文件中。测试和工作。


推荐阅读