2017-02-15 5 views
0

Ich hatte etwa 10.000 Bestellungen auf einer Woo-Commerce-Website. Ungefähr 0.4% speicherten das erforderliche benutzerdefinierte Feld 'custom_location' nicht. Ich habe keine Ahnung, wie das möglich ist und kann keinen Weg finden, mich zu reproduzieren.WooCommerce Custom Field speichert nicht in DB ... manchmal

Außerdem verwende ich den ausgewählten Wert des Feldes, um einen sekundären Wert in den DB zu speichern. Der sekundäre Wert wird korrekt gespeichert, auch wenn 'custom_location' in der Datenbank null ist. Natürlich enthält $ _POST ['custom_location'] gültige Daten, aber es wird nicht gespeichert ... warum?

// Here I create the custom select field 
add_filter('woocommerce_checkout_fields' , 'custom_override_checkout_fields'); 
function custom_override_checkout_fields($fields) { 
    // <select> options 
    $args = array('post_type' => 'MY_CUSTOM_POST_TYPE', 
       'posts_per_page' => -1, 
       'order' => 'ASC', 
       'post_status' => 'publish', 
       'suppress_filters' => true); 
    $locations = get_posts($args); 
    $ops = array('default' => 'Select a location'); 
    foreach($locations as $l) { 
     $ops[$l->ID] = __($l->post_title, 'woocommerce'); 
    } 
    // Add 'custom_location' field to 'shipping' 
    $fields['shipping'] = array('custom_location' => array(
     'label'  => __('Location', 'woocommerce'), 
     'placeholder' => _x('', 'empty', 'woocommerce'), 
     'required' => true, 
     'clear'  => false, 
     'type'  => 'select', 
     'options'  => $ops 
     )) 
     + $fields['shipping']; 

    return $fields; 
} 
// Save related data 
add_action('woocommerce_checkout_update_order_meta', 'save_extra_data'); 
function save_extra_data($order_id) { 
    $loc = $_POST['custom_location']; 

    // This was saved correctly to DB even when 'custom_location' is null in DB! 
    $meta = get_post_meta($loc, 'extra-shipping-data', true); 
    update_post_meta($order_id, '_shipping_extra', $meta); 
} 
+0

Wo ist Ihre Variable '$ locations' definiert, die in der foreach-Schleife verwendet wird? – LoicTheAztec

+0

Ich habe dem Code die Variable $ locations hinzugefügt. Es ist ein Array von Posts, die ich über "get_posts()" erhalte. –

Antwort

0

Ihr Standort wird null gespeichert werden, wenn der Kunde Rechnungs- und Lieferadresse identisch ist. In Ihrem Code fehlt die Deklaration für locations. Mit einer kleinen Änderung können Sie den Code und Aufwand minimieren. Stellen Sie sicher, dass Sie ops durch Ihre Optionen ersetzen.

add_filter('woocommerce_checkout_fields' , 'custom_override_checkout_fields'); 
function custom_override_checkout_fields($fields) { 
    // <select> options 
    $ops = array('default' => 'Select a location', 'location1' => 'Location 1'); 

    // Add 'custom_location' field to 'shipping' 
    $fields['shipping'] = array('shipping_custom_location' => array(
     'label'  => __('Location', 'woocommerce'), 
     'placeholder' => _x('', 'empty', 'woocommerce'), 
     'required' => true, 
     'clear'  => false, 
     'type'  => 'select', 
     'options'  => $ops 
    )) 
    + $fields['shipping']; 

    return $fields; 
} 
+0

In dieser Anwendung unterscheidet sich die Lieferadresse immer von der Fakturierung, daher ist es nicht erlaubt, dass sie optional ist. Ich habe vor einiger Zeit die Deklaration der Locations hinzugefügt. Tut mir leid, dass du das verpasst hast. (Btw, $ ops muss von Administratoren bearbeitet werden können, deshalb ist der get_posts Code dort) –

+0

Haben Sie versucht, die ID von 'custom_location' in' shipping_custom_location' zu ändern? Es sollte funktionieren. – LearnWoo

Verwandte Themen