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);
}
Wo ist Ihre Variable '$ locations' definiert, die in der foreach-Schleife verwendet wird? – LoicTheAztec
Ich habe dem Code die Variable $ locations hinzugefügt. Es ist ein Array von Posts, die ich über "get_posts()" erhalte. –