繁体   English   中英

购物车中的自定义表单字段,并在Woocommerce上的结帐中获取数据

[英]Custom form fields in cart and get the data in checkout on Woocommerce

在结帐之前在购物车页面中添加“从”,“到”和“消息”字段。我已经在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中,我在关闭form标签之前和form标签之后在底部添加了此代码。但是在cart.php中添加了这段代码后,我得到了空白页面。

在此处输入图片说明

我试图以相同的格式将那些从,到和消息字段中获取。

以下代码将从购物车页面中的自定义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文件中。 经过测试和工作。

我认为最好不要进行“结帐”表单更改,最好将字段中的数据更改时将vars存储在localstorage中,然后在用户处于结帐表单时获取它。

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>
    <script>
       jQuery(document).ready(function (jQuery) { 
         jQuery("#customer_notes_text").on('change', function () {
           localStorage.setItem(jQuery(this).attr('id'), this.val());
         });
       });
    </script>
 <?php
  }

然后你可以得到它

LocalStore.getItem(项目);

别忘了在获得元素后销毁它,

LocalStorage.removeItem(项目);

暂无
暂无

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

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