【问题标题】:Highlight Woocommerce admin orders list when order contains a regular product当订单包含常规产品时突出显示 Woocommerce 管理员订单列表
【发布时间】:2021-12-13 19:35:13
【问题描述】:

我正在使用 Woocommerce 和 YITH 订阅插件来销售订阅产品。但是当人们还订购常规产品时,我经常忽略这一点(约 95% 的订单是订阅)。

当订单还包含常规产品时,我是否可以在管理订单列表中突出显示/设置相应行的样式,以免被忽略?

【问题讨论】:

    标签: woocommerce styles


    【解决方案1】:

    您可以使用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 );
    

    【讨论】:

    • 哇,这真的很有帮助。谢谢你!问题是也有订阅和常规产品的订单。我想强调的主要是这些。我怎样才能使这些也被突出显示?这不适用于 $has_subscription = false;
    • 仍然不适用于同时包含订阅和常规产品的订单。并且突出显示仅常规的订单不再起作用。
    • 可以给只包含订阅和简单和订阅的订单截图吗?您还想强调哪个?
    • 屏幕截图:ibb.co/cFqfPR9 #5313 是一个带有订阅和常规产品的订单,但由于它有一个订阅 ID,因此无法在订单列表中识别它 (#5314)仅产品没有订阅号并且更容易识别,但我也希望它们突出显示。
    • 您只想突出显示既有订阅又有常规产品的产品,还是还想突出常规产品?
    猜你喜欢
    • 2023-01-25
    • 2021-12-19
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2018-09-05
    • 1970-01-01
    相关资源
    最近更新 更多