在管理面板订购列表和用户的我的帐户订购列表中添加产品名称

时间:2020-06-28 作者:ashik4u

如何在上添加产品名称Admin Panel Order listUser\'s My-Account Order List?

enter image description here

1 个回复
最合适的回答,由SO网友:Sabbir Hasan 整理而成

将此代码添加到函数中。php这将在列表中添加一个额外的列,其中包含产品名称。喜欢1 x Netflix Premium. 如果有多个,也将可见。

add_filter(\'manage_edit-shop_order_columns\', \'_order_items_column\' );
function _order_items_column( $order_columns ) {
    $order_columns[\'order_products\'] = "Product Name";
    return $order_columns;
}
 

add_action( \'manage_shop_order_posts_custom_column\' , \'_order_items_column_cnt\' );
function _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[\'quantity\'] .\' × <a href="\' . admin_url(\'post.php?post=\' . $order_item[\'product_id\'] . \'&action=edit\' ) . \'">\'. $order_item[\'name\'] .\'</a><br />\';
                // you can also use $order_item->variation_id parameter
                // by the way, $order_item[\'name\'] will display variation name too
 
            }
        }
 
    }
 
}
现在,通常会将该列添加为最后一列。我们必须重新安排列顺序。让我们修改_order_items_column 类似波纹管的功能使其在所需位置移动。

function _order_items_column( $order_columns ) {

    $new_columns = array();
    foreach ( $order_columns as $key => $name ) {

        $new_columns[ $key ] = $name;

        // add ship-to after order status column
        if ( \'order_number\' === $key ) {
            $new_columns[\'order_products\'] = __( \'Product Name\', \'textdomain\' );
        }
    }

    return $new_columns;
}
现在它应该出现在订单ID之后。

要在我的订单列表中添加此数据,请使用以下代码。

add_filter( \'woocommerce_my_account_my_orders_columns\', \'additional_my_account_orders_column\', 10, 1 );
function additional_my_account_orders_column( $columns ) {
    $new_columns = [];

    foreach ( $columns as $key => $name ) {
        $new_columns[ $key ] = $name;

        if ( \'order-number\' === $key ) {
            $new_columns[\'order-items\'] = __( \'Product Name\', \'woocommerce\' );
        }
    }
    return $new_columns;
}

add_action( \'woocommerce_my_account_my_orders_column_order-items\', \'additional_my_account_orders_column_content\', 10, 1 );
function additional_my_account_orders_column_content( $order ) {
    $details = array();

    foreach( $order->get_items() as $item )
        $details[] = $item->get_quantity() . \'&nbsp;&times;&nbsp;\' . $item->get_name();

    echo count( $details ) > 0 ? implode( \'<br>\', $details ) : \'&ndash;\';
}