【发布时间】:2021-12-13 19:35:13
【问题描述】:
我正在使用 Woocommerce 和 YITH 订阅插件来销售订阅产品。但是当人们还订购常规产品时,我经常忽略这一点(约 95% 的订单是订阅)。
当订单还包含常规产品时,我是否可以在管理订单列表中突出显示/设置相应行的样式,以免被忽略?
【问题讨论】:
标签: woocommerce styles
我正在使用 Woocommerce 和 YITH 订阅插件来销售订阅产品。但是当人们还订购常规产品时,我经常忽略这一点(约 95% 的订单是订阅)。
当订单还包含常规产品时,我是否可以在管理订单列表中突出显示/设置相应行的样式,以免被忽略?
【问题讨论】:
标签: woocommerce styles
您可以使用post_class 过滤钩子将类添加到表格行。
首先,您必须检查get_current_screen() 以确保您正在向订单列表页面添加一个类。
现在您必须获取订单商品并且需要检查产品是订阅还是未使用产品元_ywsbs_subscription
function add_custom_class( $classes, $class, $post_id ){
// Check current screen and make sure you are only doing in order list page.
$currentScreen = get_current_screen();
if( $currentScreen->id === "edit-shop_order" ) {
// Getting an instance of the WC_Order object from a defined ORDER ID
$order = wc_get_order( $post_id );
$has_regular = false;
// Iterating through each "line" items in the order
foreach ($order->get_items() as $item_id => $item ) {
$product = $item->get_product();
if( $product && $product->is_type('simple') ){
if( get_post_meta( $product->get_id(), '_ywsbs_subscription', true ) == 'no' || get_post_meta( $product->get_id(), '_ywsbs_subscription', true ) == '' ){
$has_regular = true;
break;
}
}
}
if( $has_regular ){
$classes[] = 'order-has-simple';
}
}
return $classes;
}
add_filter( 'post_class', 'add_custom_class', 10, 3 );
现在您必须使用 admin_head 钩子来帮助您在管理端添加 CSS。
function add_custom_admin_css(){
$currentScreen = get_current_screen();
if( $currentScreen->id === "edit-shop_order" ) {
?>
<style type="text/css">
.order-has-simple{
background-color: #adf !important; // here you have to your own color
}
</style>
<?php
}
}
add_action( 'admin_head', 'add_custom_admin_css', 10, 1 );
经过测试并且有效。
根据 OP 要求
按类别检查产品
function add_custom_class( $classes, $class, $post_id ){
// Check current screen and make sure you are only doing in order list page.
$currentScreen = get_current_screen();
if( $currentScreen->id === "edit-shop_order" ) {
// Getting an instance of the WC_Order object from a defined ORDER ID
$order = wc_get_order( $post_id );
$has_regular = false;
// Iterating through each "line" items in the order
foreach ($order->get_items() as $item_id => $item ) {
$product = $item->get_product();
if( $product && has_term( 'vergangene-ausgaben', 'product_cat', $product->get_id() ) ) {
$has_regular = true;
break;
}
}
if( $has_regular ){
$classes[] = 'order-has-simple';
}
}
return $classes;
}
add_filter( 'post_class', 'add_custom_class', 10, 3 );
【讨论】: