代码之家  ›  专栏  ›  技术社区  ›  JapeNZ

Wooccommerce将列添加到管理订单列表中,显示具有特定元的已订购产品

  •  0
  • JapeNZ  · 技术社区  · 3 年前

    我想在WooCommerce管理订单列表中添加带有一些元数据的自定义列。我找到了它,并根据我的需要对其进行了修改,并将其放在了我的functions.php中:

    add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
    function MY_COLUMNS_FUNCTION( $columns ) {
        $new_columns = ( is_array( $columns ) ) ? $columns : array();
        unset( $new_columns[ 'order_actions' ] );
        
        //edit this for your column(s)
        //all of your columns will be added before the actions column
        $new_columns['dname'] = 'Dogs Name';
        $new_columns['additional_allergies'] = 'Allergies';
        
        //stop editing
        $new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
        return $new_columns;
    }
    

    结账时,我收集了这两个元密钥:

    • dname
    • 附加配件

    然而,我当前的代码只显示空列,有什么建议添加meta datea吗?

    0 回复  |  直到 4 年前
        1
  •  2
  •   7uc1f3r    4 年前

    这个 manage_shop_order_posts_custom_column 钩子可以用来添加页眉。

    // Add a Header
    function custom_shop_order_column( $columns ) {
        // Add new columns
        $columns['dogs_name'] = __( 'Dogs Name', 'woocommerce' );
        $columns['additional_allergies'] = __( 'Allergies', 'woocommerce' );
        
        return $columns;
    }
    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 10, 1 );
    

    这个 manage_shop_order_posts_custom_column 将需要钩子来填充列。

    • 注: 确定是否是属于 $order 对象,或者是否是属于订单的元数据 $items 在此基础上,你必须使用下面两个答案中的一个。

    1a: 如果元数据属于 $order 对象,您可以使用:

    //  Populate the Column
    function custom_shop_order_list_column_content( $column, $post_id ) {
        // Get order object
        $order = wc_get_order( $post_id );
    
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Compare column name
            if ( $column == 'dogs_name' ) {
                // Get meta, use the correct meta key!
                $dogs_name = $order->get_meta( 'dname' );
                
                // NOT empty
                if ( ! empty( $dogs_name ) ) {
                    // Output
                    echo $dogs_name;
                } else {
                    // Output
                    echo __( 'Meta key is wrong or not found for this order', 'woocommerce' );
                }
            }
    
            // Compare column name
            if ( $column == 'additional_allergies' ) {
                // Get meta, use the correct meta key!
                $allergies = $order->get_meta( 'additional_allergies' );
                
                // NOT empty
                if ( ! empty( $allergies ) ) {
                    // Output
                    echo $allergies;
                } else {
                    // Output
                    echo __( 'Meta key is wrong or not found for this order', 'woocommerce' );
                }       
            }
        }
    }
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );
    

    1b: 但是,当元数据属于订单时 $个项目 那么在上面的回答中将需要进行调整,因为 $order 可以由几个组成 $个项目 ,我们将循环通过 $order 具有的对象 foreach

    //  Populate the Column
    function custom_shop_order_list_column_content( $column, $post_id ) {
        // Get order object
        $order = wc_get_order( $post_id );
    
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Initialize
            $dogs_name_arr = array();
            $allergies_arr = array();
            
            // Loop trough order items
            foreach ( $order->get_items() as $item_key => $item ) {
                // Get meta, use the correct meta key!
                $dogs_name = $item->get_meta( 'dname' );
                
                // NOT empty
                if ( ! empty ( $dogs_name ) ) {
                    // Push to array
                    $dogs_name_arr[] = $dogs_name;
                }
                
                // Get meta, use the correct meta key!
                $allergies = $item->get_meta( 'additional_allergies' );
                
                // NOT empty
                if ( ! empty ( $allergies ) ) {
                    // Push to array
                    $allergies_arr[] = $allergies;
                }
            }
            
            // Compare column name
            if ( $column == 'dogs_name' ) {
                // NOT empty
                if ( ! empty ( $dogs_name_arr ) ) {
                    // Output
                    echo '<ul>';
                    echo '<li>' . implode( '</li><li>', $dogs_name_arr ) . '</li>';
                    echo '</ul>';
                } else {
                    // Output
                    echo __( 'Meta key is wrong or not found for the order items', 'woocommerce' );
                }
            }
    
            // Compare column name
            if ( $column == 'additional_allergies' ) {
                // NOT empty
                if ( ! empty ( $allergies_arr ) ) {
                    // Output
                    echo '<ul>';
                    echo '<li>' . implode( '</li><li>', $allergies_arr ) . '</li>';
                    echo '</ul>';
                } else {
                    // Output
                    echo __( 'Meta key is wrong or not found for the order items', 'woocommerce' );
                }
            }
        }
    }
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );