Reputation: 11
I created some code to display the stock availability on woocommerce>>orders. see pics (specialnotering).
The problem is that if I change custom field text then all previous order is updated with new custom field text.
For example: one customer confirmed an order for a product on January 2021 where custom field text was "Product will be available on March 2021". 2nd customer confirmed the same product order on march 2021 where new custom field text is "Product will be available on May 2021".
Both order is showing the last custom field text "Product will be available on May 2021".
I want to display the old order with old custom field text and new order with new custom field text. If any body can solve it or can give me some idea, it will help full for me.
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'display_order_data_in_admin' );
function display_order_data_in_admin( $order ){
?>
<h4><?php _e( 'Speciellnotering', 'woocommerce' ); ?></h4>
<?php
foreach ( $order->get_items() as $item_id => $item ) {
//fetch text from custom_field
$product = $item->get_product();
$availability = $product->get_availability();
$quantity = $item->get_quantity();
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$sku = get_post_meta( $variation_id, '_sku', true );
$custom_text_message = get_post_meta( $variation_id, 'custom_field', true );
if($product->is_type('variable')){
if($custom_text_message && $quantity<=-1){
$availability['availability'] = $custom_text_message;
}elseif(!$custom_text_message && $quantity<=-1){
$availability['availability'] = __('Right now is not available', 'woocommerce');
}else{
$availability['availability'] =__('Finns i lager', 'woocommerce');
}
?>
<?php echo __( 'Artnr' ).' ('.$sku.')' .': '.$availability['availability'].'<br>';
//for simple product
}else{
$product = $item->get_product();
$sku=$product->get_sku();
$availability = $product->get_availability();
$custom_stock_message=get_post_meta( $product_id, 'custom_stock_message', true );
$quantity = $item->get_quantity();
if($product->is_type('simple')){
if($custom_stock_message && $quantity<=0){
$availability['availability'] = $custom_stock_message;
}elseif(!$custom_stock_message && $quantity<=0){
$availability['availability'] = __('Right now is not available', 'woocommerce');
}else{
$availability['availability'] =__('Finns i lager', 'woocommerce');
}
}
?>
<?php echo __( 'Artnr' ).' ('.$sku.')' .': '.$availability['availability'].'<br>';
}
}?>
<?php
}
Upvotes: 1
Views: 455
Reputation: 254448
Updated
Instead you should first save your product custom fields as custom order item meta data, to avoid changes when your product custom field changes.
I have simplified, and optimized your initial code. Also $item->get_quantity()
value is always higher than zero. So in the code below I use the product stock quantity instead:
// Save product custom availiability text as custom hidden order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'save_custom_image_id_to_order_item', 10, 4 );
function save_custom_image_id_to_order_item( $item, $cart_item_key, $values, $order ) {
$product = $item->get_product();
// Product variation type
if ( $item->get_variation_id() > 0 ) {
$availability = $product->get_meta('custom_field');
}
// Other product types
else {
$availability = $product->get_meta('custom_stock_message');
}
if ( empty($availability) ) {
// save as hidden order item meta data
$item->update_meta_data('_availability', array($availability) );
}
}
// Display order item availability in admin orders
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'display_order_item_availability_in_admin_orders' );
function display_order_item_availability_in_admin_orders( $order ){
echo '<h4>' . __( 'Speciellnotering', 'woocommerce' ) . '</h4>';
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();
$quantity = $item->get_quantity();
$stock_qty = $product->get_stock_quantity();
$sku = $product->get_sku();
$availability = $item->get_meta('_availability'); // Get order item custom field
$availability = reset($availability); // Convert array to string
if ( $stock_qty < 0 ) {
if ( empty($availability) ) {
$availability = __('Inte tillgänglig just nu', 'woocommerce');
}
} else {
$availability = __('Finns i lager', 'woocommerce');
}
echo __('Artnr', 'woocommerce') . ' (' . $sku . ')' . ': ' . $availability . '<br>';
}
}
Code goes in functions.php file of the active child theme (or active theme). It should work.
Upvotes: 1