如何在 WooCommerce "My account" 订单 table 中插入订单的前 3 个产品图片
How to insert the first 3 product images of an order in WooCommerce "My account" orders table
有人知道如何将订单的前 3 张图片插入 WooCommerce 我的订单 table 吗?
此外,我想添加一个“和更多”按钮,link使用 if 条件编辑到特定订单(与订单号相同 link)。
提示:为了保持更灵活,如果可能的话,获得一个没有钩子的解决方案会很好。
我想编辑更多并稍后通过子主题覆盖 my-orders.php。
这是我目前使用的解决方案。
<div class="order-status">
<span>Order Status</span>
<?php
elseif ( 'order-status' === $column_id ) :
$order_color_check = $order->get_status();
if($order_color_check=="completed") :
echo '<span class="order-status-value" style="color: green;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
else :
echo '<span class="order-status-value" style="color: red;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
endif; ?>
</div>
- 知道 myaccount/my-orders.php 自 WC 2.6.0 起已被弃用
- 我的答案是通过钩子,但是通过模板文件编辑应该通过 myaccount/orders.php
- html 的输出将需要一些 CSS 样式(取决于主题)
// Adds a new column to the "My Orders" table in the account.
function filter_woocommerce_account_orders_columns( $columns ) {
// Add a new column
$new_column['order-products'] = __( 'Products', 'woocommerce' );
// Return new column as first
return $new_column + $columns;
}
add_filter( 'woocommerce_account_orders_columns', 'filter_woocommerce_account_orders_columns', 10, 1 );
// Adds data to the custom "order-products" column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_order( $order ) {
$count = 0;
// Loop through order items
foreach ( $order->get_items() as $item_key => $item ) {
// Count + 1
$count++;
// First 3
if ( $count <= 3 ) {
// The WC_Product object
$product = wc_get_product( $item['product_id'] );
// Instanceof
if ( $product instanceof WC_Product ) {
// Get image - thumbnail
$thumbnail = $product->get_image( array(50, 50) );
// Output
echo '<div class="product-thumbnail" style="display:inline-block;padding:2px;"><a href="' . $product->get_permalink() . '">' . $thumbnail . '</a></div>';
}
} elseif ( $count == 4 ) {
// Output "read more" button
echo '<span><a href="' . $order->get_view_order_url() . '">'. __( 'Read more', 'woocommerce') . '</a></span>';
break;
}
}
}
add_action( 'woocommerce_my_account_my_orders_column_order-products', 'filter_woocommerce_my_account_my_orders_column_order', 10, 1 );
有人知道如何将订单的前 3 张图片插入 WooCommerce 我的订单 table 吗?
此外,我想添加一个“和更多”按钮,link使用 if 条件编辑到特定订单(与订单号相同 link)。
提示:为了保持更灵活,如果可能的话,获得一个没有钩子的解决方案会很好。
我想编辑更多并稍后通过子主题覆盖 my-orders.php。
这是我目前使用的解决方案。
<div class="order-status">
<span>Order Status</span>
<?php
elseif ( 'order-status' === $column_id ) :
$order_color_check = $order->get_status();
if($order_color_check=="completed") :
echo '<span class="order-status-value" style="color: green;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
else :
echo '<span class="order-status-value" style="color: red;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
endif; ?>
</div>
- 知道 myaccount/my-orders.php 自 WC 2.6.0 起已被弃用
- 我的答案是通过钩子,但是通过模板文件编辑应该通过 myaccount/orders.php
- html 的输出将需要一些 CSS 样式(取决于主题)
// Adds a new column to the "My Orders" table in the account.
function filter_woocommerce_account_orders_columns( $columns ) {
// Add a new column
$new_column['order-products'] = __( 'Products', 'woocommerce' );
// Return new column as first
return $new_column + $columns;
}
add_filter( 'woocommerce_account_orders_columns', 'filter_woocommerce_account_orders_columns', 10, 1 );
// Adds data to the custom "order-products" column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_order( $order ) {
$count = 0;
// Loop through order items
foreach ( $order->get_items() as $item_key => $item ) {
// Count + 1
$count++;
// First 3
if ( $count <= 3 ) {
// The WC_Product object
$product = wc_get_product( $item['product_id'] );
// Instanceof
if ( $product instanceof WC_Product ) {
// Get image - thumbnail
$thumbnail = $product->get_image( array(50, 50) );
// Output
echo '<div class="product-thumbnail" style="display:inline-block;padding:2px;"><a href="' . $product->get_permalink() . '">' . $thumbnail . '</a></div>';
}
} elseif ( $count == 4 ) {
// Output "read more" button
echo '<span><a href="' . $order->get_view_order_url() . '">'. __( 'Read more', 'woocommerce') . '</a></span>';
break;
}
}
}
add_action( 'woocommerce_my_account_my_orders_column_order-products', 'filter_woocommerce_my_account_my_orders_column_order', 10, 1 );