woocommerce-在“查看订单”页面中显示产品

时间:2018-07-21 01:44:15

标签: wordpress woocommerce

我正在像这样在客户帐户页面的“查看订单”页面中显示订单产品 enter image description here

我们可以这样做吗?

1 个答案:

答案 0 :(得分:1)

将此添加到主题的“ functions.php”中。

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

        // add products after order total column
        if ( 'order-total' === $key ) {
            $new_columns['order-products'] = __( 'Products', 'textdomain' );
        }
    }
    return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'add_products_my_account_orders_column' );

function add_products_data_my_account_orders_column( $order ) {
    //loop through products of the order
    foreach( $order->get_items() as $item_id => $item ) {
        $product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );

        $is_visible        = $product && $product->is_visible();
        $product_permalink = apply_filters( 'woocommerce_order_item_permalink', $is_visible ? $product->get_permalink( $item ) : '', $item, $order );

        echo apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '<p><a href="%s">%s</a>', $product_permalink, $item['name'] ) : $item['name'], $item, $is_visible );
        echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '&times; %s', $item['qty'] ) . '</strong></p>', $item );
    }
}
add_action( 'woocommerce_my_account_my_orders_column_order-products', 'add_products_data_my_account_orders_column' );