简体   繁体   English

在 WooCommerce 管理订单页面中保留并显示产品自定义字段值

[英]Keep and display product custom field value in WooCommerce Admin Order pages

I created some code to display the stock availability on woocommerce>>orders.我创建了一些代码来显示 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".例如:一位客户在 2021 年 1 月确认了产品订单,其中自定义字段文本为“产品将于 2021 年 3 月上市”。 2nd customer confirmed the same product order on march 2021 where new custom field text is "Product will be available on May 2021".第二位客户在 2021 年 3 月确认了相同的产品订单,其中新的自定义字段文本为“产品将于 2021 年 5 月上市”。

Both order is showing the last custom field text "Product will be available on May 2021".两个订单都显示最后一个自定义字段文本“产品将于 2021 年 5 月上市”。

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
}

点击查看图片

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. $item->get_quantity()值也总是大于零。 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).代码进入活动子主题(或活动主题)的functions.php文件。 It should work.它应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 管理订单页面上将产品自定义字段显示为订单项元数据 - Display product custom field as order item meta on WooCommerce admin order pages 在 WooCommerce 订单管理页面 (ACF) 显示产品自定义字段 - Display product custom field in WooCommerce Order admin Page (ACF) 在结帐中添加自定义字段并在 WooCommerce 管理订单页面中显示 - Add custom field in checkout and display it in WooCommerce admin order pages 在 Woocommerce 管理订单页面中显示自定义结帐字段值 - Displaying custom checkout field value in Woocommerce admin order pages 在Woocommerce管理员电子邮件通知中显示产品自定义字段值 - Display a product custom field value in Woocommerce admin email notifications 通过钩子在 WooCommerce 单个产品页面中显示自定义字段值 - Display custom field value in WooCommerce single product pages via hook 在Woocommerce管理员订单列表自定义列中显示结帐字段值 - Display checkout field value in Woocommerce admin order list custom column 在Woocommerce单个产品页面中显示高于产品价格的自定义字段 - Display a custom field above product price in Woocommerce single product pages 显示自定义字段吹产品标题Woocommerce产品单页 - Display custom field blow product title Woocommerce product single pages 在 Woocommerce 管理订单页面中保存订单项目自定义字段 - Save Order item custom field in Woocommerce Admin order pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM