i have a custom attribute where products have different pickup times. i want to add this dates as a custom field to my wc orders, to notify my customers via cronjob that the ord
It's better to loop through cart items and save the date in this custom hooked function, once order is placed before saving data:
add_action( 'woocommerce_checkout_create_order', 'wc_checkout_create_order_action_callback' );
function wc_checkout_create_order_action_callback( $order ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
$lieferfrequenz = $cart_item['data']->get_attribute( 'pa_lieferfrequenz' );
if ( ! empty( $lieferfrequenz ) ) {
// Save the date as custom order meta data
$order->update_meta_data( 'lwb_pickup_time_email_notification', date('d-m-Y', strtotime("+ '.$lieferfrequenz.'") ) );
break; /// stop the loop
}
}
}
Code goes in functions.php file of your active child theme (or active theme). It should works.
But if there is many different cart items on an order, you should re-think things differently, as this code will take the first cart item delivery date (or the last one, if you remove
break;
).
Related: Get Order items and WC_Order_Item_Product in WooCommerce 3