正在从购物车页面到结账页面获取自定义字段数据

时间:2018-12-02 作者:tester

通过添加他们的姓名和个人信息,添加了一些文本字段,以添加我们为谁发送礼物。对于个人消息,如果我添加到字段并单击继续结帐,则工作正常。如果我在管理面板订单部分下单,则在结帐页面中,它不会显示我在购物车页面中添加的收件人字段名称。它显示的标题是TO,显示的是blank,而不是我输入的名称。这是我在Functions中编写的cde。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() {
?>
<div class="customer_notes_on_cart" style="clear:both;">
<?php
 woocommerce_form_field(\'to_notes_text\', array(
\'placeholder\'   => __(\'To\'),
\'class\' => array(\'form-row-last\'),
\'clear\' => true,
 ), \'\'); 
 ?></div><?php
 }
 // PHP: Remove "(optional)" from non required fields
 add_filter( \'woocommerce_form_field\' , 
\'remove_checkout_optional_fields_label\', 10, 4 );
 function remove_checkout_optional_fields_label( $field, $key, $args, $value 
 ) {
// Only on cart page
if( is_cart() ) {
$optional = \'&nbsp;<span class="optional">(\' . esc_html__( \'optional\', \'woocommerce\' ) . \')</span>\';
$field = str_replace( $optional, \'\', $field );
}
return $field;
}
// 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="to_notes" id="to_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() ) : ?>            
    $(\'#to_notes_text\').on( \'blur\', function(){
        $(\'#to_notes\').val($(this).val());
    });

<?php // For checkout
    elseif( is_checkout() && ! is_wc_endpoint_url() ) : ?>
    $(\'#to_comments\' ).val("<?php echo sanitize_text_field($_POST[\'to_notes\']); ?>");           
<?php endif; ?>
 });
 </script>
 <?php
 }
 /**
 * Update the order meta with field value
 */
 add_action( \'woocommerce_checkout_update_order_meta\', 
\'my_custom_checkout_field_update_order_meta\' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
 ?>
 <?php
 if ( ! empty( $_POST[\'to_notes_text\'] ) ) {
update_post_meta( $order_id, \'To\', sanitize_text_field( 
 $_POST[\'to_notes_text\'] ) );
  }
  }
  /**
  * Display field value on the order edit page
  */
  add_action( \'woocommerce_admin_order_data_after_billing_address\', 
 \'my_custom_checkout_field_display_admin_order_meta\', 10, 1 );
 function my_custom_checkout_field_display_admin_order_meta($order){
?>
<?php
echo \'<p><strong>\'.__(\'TO\').\':</strong> \' . get_post_meta( $order->id, \'TO\', true ) . \'</p>\';
     }

1 个回复
最合适的回答,由SO网友:tester 整理而成

通过添加此代码解决

/**
 * Update the order meta with field value
**/ 
add_action( \'woocommerce_checkout_update_order_meta\', 
\'my_custom_checkout_field_update_order_meta\' );

 function my_custom_checkout_field_update_order_meta( $order_id ) {
 ?>
 <?php
 if ( ! empty( $_POST[\'from_comments\'] ) ) {
update_post_meta( $order_id, \'FROM\', sanitize_text_field( 
 $_POST[\'from_comments\'] 
 ) );
 }   
 }
 /**
 * Update the order meta with field value
 **/ 
 add_action( \'woocommerce_checkout_update_order_meta\', 
\'my_customs_checkout_field_update_order_meta\' );
 function my_customs_checkout_field_update_order_meta( $order_id ) {
  ?>
 <?php
 if ( ! empty( $_POST[\'to_comments\'] ) ) {
update_post_meta( $order_id, \'TO\', sanitize_text_field( 
 $_POST[\'to_comments\'] ) 
 );
 }    
 }
 /**
 * Display field value on the order edit page
 **/
 add_action( \'woocommerce_admin_order_data_after_billing_address\', 
 \'my_custom_checkout_field_display_admin_order_meta\', 10, 1 );
  function my_custom_checkout_field_display_admin_order_meta($order){
  ?>
  <?php
  echo \'<p><strong>\'.__(\'FROM\').\':</strong> \' . get_post_meta( $order->id, \'FROM\', true 
   ) . \'</p>\';
   echo \'<p><strong>\'.__(\'TO\').\':</strong> \' . get_post_meta( $order->id, \'TO\', true ) 
   . \'</p>\';
   }
   /**
   * Add the field to the checkout
   */
   add_action( \'woocommerce_after_order_notes\', \'my_custom_checkout_field\' );

    function my_custom_checkout_field( $checkout ) {
     ?>
    <?php
    echo \'<div id="my_custom_checkout_field"><h2>\' . __(\'TO \') . \'</h2>\';
    woocommerce_form_field( \'from_comments\', array(
   \'type\'          => \'text\',
   \'class\'         => array(\'my-field-class form-row-wide\'),
    \'placeholder\'   => __(\'FROM\'),
     ), $checkout->get_value( \'from_comments\' ));

   woocommerce_form_field( \'to_comments\', array(
\'type\'          => \'text\',
\'class\'         => array(\'my-field-class form-row-wide\'),
\'placeholder\'   => __(\'TO\'),
), $checkout->get_value( \'to_comments\' ));
   echo \'</div>\';
    }

相关推荐

无法在WordPress中通过PHP显示图像

我有一个奇怪的问题,当我尝试使用PHP输入图像标记的源代码时,它会在检查器中显示以下错误<img src=(unknown) alt=\"\"> 这个代码片段为我提供了正确的url,通过查看CPanel并粘贴和复制地址进行检查,但当我尝试通过php输入它时,不会显示图像$image = wp_get_attachment_image_src( $post_thumbnail_id); //echo($image[0]); ?> <img src=