如何在Woocommerce购物车页面中添加订单注释字段? [英] How to add an order notes field in Woocommerce cart page?

查看:96
本文介绍了如何在Woocommerce购物车页面中添加订单注释字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Woocommerce购物车优惠券区域下的Woocommerce购物车页面中添加注释字段.此字段应类似于Woocommerce结帐页面的订单注释"字段,客户可以在其中添加一些注释.

I would like to add a notes field in the Woocommerce cart page under Woocommerce cart coupon area. This field should be something similar to Woocommerce checkout page "Order Notes" field where the customer can add some notes.

到目前为止,我已经获得了指示我想要的位置的这段代码:

So far I have this code that indicates my desired location:

add_action ('woocommerce_after_cart_table','add_content_below_cart_coupon');
function add_content_below_cart_coupon () {
echo 'this will show below the cart cuopon';
}

如何在此区域添加注释字段,以便这些客户注释也将显示在结帐页面的订单详细信息中?

How can I add a notes field in this area so these customer notes would also appear in order details at checkout page?

谢谢!

推荐答案

我解决了这个问题,但是有点棘手,我建议将其放在插件中

I solved this problem but a little hacky, I suggest to put it in a plugin

/**
 * 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购物车页面中添加订单注释字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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