Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 购物车中的自定义表单字段,并在Woocommerce上的签出中获取数据_Php_Jquery_Wordpress_Woocommerce_Cart - Fatal编程技术网

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

Php 购物车中的自定义表单字段,并在Woocommerce上的签出中获取数据,php,jquery,wordpress,woocommerce,cart,Php,Jquery,Wordpress,Woocommerce,Cart,签出前在购物车页面中添加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()

签出前在购物车页面中添加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 
 });
/**
*将订单注释字段添加到购物车
**/
添加行动('woocommerce\u cart\u抵押品',
“订单\注释\自定义\购物车\字段”);
功能订单\注释\自定义\购物车\字段(){
回声';
?>
jQuery(文档).ready(函数(){
jQuery('#order_comments').val(“”);
});

以下代码将从购物车页面的自定义textarea字段将输入的文本值发布到checkout order notes字段:

// 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
}
//将订单注释字段添加到购物车
添加操作('woocommerce\u cart\u collaterals'、'order\u comments\u custom\u cart\u field');
功能订单\注释\自定义\购物车\字段(){
?>
$(“#客户_注释_文本”)。在('blur',function()上{
$('#customer_notes').val($(this.val());
});
$(“#订单注释”).val(“”);
});

我认为最好不要更改“继续签出”表单,最好在字段中的数据更改时将变量存储在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
  }
function order\u comments\u custom\u cart\u field(){
回声';
?>
jQuery(文档).ready(函数(jQuery){
jQuery(“#客户#注释#文本”)。on('change',function(){
setItem(jQuery(this.attr('id'),this.val());
});
});

在function.php文件中添加了代码,它在购物车页面中显示订单注释部分。一旦我添加文本并单击checkout,它不会在checkout页面中显示任何订单注释文本,也不会在admin orders页面中显示,以及在显示为@tester的url中显示。您肯定遇到了麻烦,一个c您已经完成的自定义、主题的自定义或插件。回答得好!我如何获取订单的值并将其发送到action=地址?@Kerim抱歉,我不明白…不清楚您在问什么。