php - 在 WooCommerce 管理订单列表的自定义列中显示每个订单项目的库存数量

标签 php wordpress woocommerce product orders

我尝试在 WooCommerce 管理订单列表的自定义列中显示每种产品的剩余库存,但没有成功。

我想将其显示在“数量”旁边。

Ej。产品A x 1 (3)

我的代码尝试:

add_filter('manage_edit-shop_order_columns', 'new_order_items_column' );
function new_order_items_column( $order_columns ) {
    $order_columns['order_products'] = "Productos";
    return $order_columns;
}

add_action( 'manage_shop_order_posts_custom_column' , 'new_order_items_column_cnt' );
function new_order_items_column_cnt( $colname ) {
    global $the_order; // the global order object

    if( $colname == 'order_products' ) {

    // get items from the order global object
    $order_items = $the_order->get_items();

    if ( !is_wp_error( $order_items ) ) {
        foreach( $order_items as $order_item ) {

            echo '▪️ ' . $order_item['name'] .' × <a href="' . admin_url('post.php?post=' . $order_item['product_id'] . '&action=edit' ) . '">'. $order_item['quantity'] .'</a><br />';
            
           }
        }

    }
}

有什么建议吗?

最佳答案

您可以使用get_stock_quantity() ,请注意,有变化的产品和不含库存的产品也被考虑在内

所以你得到:

// Add a Header
function filter_manage_edit_shop_order_columns( $columns ) {
    // Add new column
    $columns['order_products'] = __( 'Products', 'woocommerce' );

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

// Populate the Column
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {        
    // Compare
    if ( $column == 'order_products' ) {
        // Get an instance of the WC_Order object from an Order ID
        $order = wc_get_order( $post_id );
        
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            foreach( $order->get_items() as $item ) {
                // Product ID
                $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
                
                // Get product
                $product = wc_get_product( $product_id );
                
                // Get stock quantity
                $get_stock_quantity = $product->get_stock_quantity();
                
                // NOT empty
                if ( ! empty ( $get_stock_quantity ) ) {
                    $stock_output = ' (' . $get_stock_quantity . ')';
                } else {
                    $stock_output = '';
                }
                
                // Output
                echo '▪ <a href="' . admin_url( 'post.php?post=' . $item->get_product_id() . '&action=edit' ) . '">'.  $item->get_name() . '</a> × ' . $item->get_quantity() . $stock_output . '<br />';
           }
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );

关于php - 在 WooCommerce 管理订单列表的自定义列中显示每个订单项目的库存数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68099142/

相关文章:

java - Android HTTPPOST 执行 PHP 脚本两次

javascript - 通过ajax发送javascript数组而不使用jquery

wordpress - 如何将我的整个 WordPress 安装置于 git 控制之下?

使用 WPML 的多语言 WordPress 多站点

javascript - 为什么我的猫头鹰 slider 不显示?

php - Woocommerce 添加到购物车 Hook (在产品成功添加到购物车后)

php - 带 Supervisord 的 Laravel 队列监听器会导致核心转储

javascript - 如何从 JavaScript 中的字符串中删除反斜杠

php - 获取 Woocommerce 文件中当前产品类别的子类别

css - Wordpress - 图像容器太大