无法从woocommerce中的订单的购物车页面到结帐页面获取自定义字段数据

时间:2018-12-01 13:21:51

标签: php woocommerce

我已经创建了一些自定义文本字段以添加到购物车页面的名称字段和自定义消息字段。因此,一旦用户进入购物车页面(如果他填写了数据)并单击继续进行结帐,这些数据应显示在结帐页面中,一旦客户下订单,这些自定义字段数据也应存储在admin的订单页面中。

到目前为止,对于订单记录来说,它工作正常并且To字段不起作用。这是我累了的代码。我在管理面板中仅将To作为标题显示,但未在to字段中显示名称在管理面板中。

// 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 个答案:

答案 0 :(得分:0)

通过这种方式解决了

class ProductFilter(django_filters.FilterSet):
    price = django_filters.NumberFilter()
    price__gt = django_filters.NumberFilter(name='price', lookup_expr='gt')
    price__lt = django_filters.NumberFilter(name='price', lookup_expr='lt')

    release_year = django_filters.NumberFilter(name='release_date', lookup_expr='year')
    release_year__gt = django_filters.NumberFilter(name='release_date', lookup_expr='year__gt')
    release_year__lt = django_filters.NumberFilter(name='release_date', lookup_expr='year__lt')

    manufacturer__name = django_filters.CharFilter(lookup_expr='icontains')

    class Meta:
        model = Product