【问题标题】:Show woocommerce product description next to product name in orders deatils page at the backend and make it searchable在后端的订单详细信息页面中的产品名称旁边显示 woocommerce 产品描述并使其可搜索
【发布时间】:2019-12-18 00:09:59
【问题描述】:

我需要帮助,我想在后端管理员的订单详细信息页面中的产品名称旁边显示 woocommerce 产品描述仪表板

所以如果产品名称是:Mobile 产品说明:键入iPhone

所以我希望结果是:Mobile Type iPhone,而不是 Mobile

我尝试实现并搜索了互联网,但无法到达目的地。

我通过以下代码实现了这一点:

add_filter( 'woocommerce_order_item_get_name', 'filter_order_item_get_name', 10, 2 );
function filter_order_item_get_name( $item_name, $order_item ) {
    if ( is_admin() && $order_item->is_type('line_item') ) {
        $product = $order_item->get_product();

        if( $description = $product->get_name() . " - " . $product->get_description() ) {
            $item_name = $description;
        }
    }
    return $item_name;
}

但仍然无法按产品描述搜索,因为我想通过产品描述本身的值进行搜索,而不仅仅是名称以显示带有此产品描述的订单结果。 p>

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    不幸的是,没有解决此问题的钩子,并且您无法轻松覆盖管理模板。这样做的预期方法是使用\wp-content\plugins\woocommerce\includes\admin\meta-boxes\views\html-order-item.php 中的woocommerce_admin_order_item_headers 并通过回显类似的内容添加一列

    add_action('woocommerce_admin_order_item_headers', function(){
        echo '<th class="sortable" data-sort="string-ins" style="max-width: 200px">Description</th>';
    });
    

    并使用\wp-content\plugins\woocommerce\includes\admin\meta-boxes\views\html-order-item.php中的woocommerce_admin_order_item_values在每一行中添加描述

    add_action('woocommerce_admin_order_item_values', function($product){
        if( $product )
            echo '<td style="max-width: 200px">'.esc_html($product->get_description()).'</td>';
    });
    

    如果你真的想将它添加到标题中,你可能会更好地使用 JavaScript。

    更新

    如果您想搜索具有相似简短描述的产品,您可以使用标签。此代码将标签添加到项目行。如果单击它们,您将转到存档页面。

    add_action('woocommerce_after_order_itemmeta', function($item_id, $item, $product){
        if( !$product )
            return;
    
        $tags = get_the_terms($product->get_id(), 'product_tag');
        if( $tags ){
            $html = '';
            foreach($tags as $tag)
                $html .= ($html ? ', ' : '').'<a href="/wp-admin/edit.php?post_type=product&product_tag='.esc_attr($tag->slug).'">'.esc_html($tag->name).'</a>';
            echo ($html ? 'tags: '.$html : $html);
        }
    }, 10, 3);
    

    【讨论】:

    • 你能检查我更新的答案吗?我已经做到了,但仍然是一个小问题
    • @MagedMohamed:我已经更新了我的答案。如果您想按描述进行排序,您确实需要一个单独的列来单击。除非我错过了你想要做的事情。
    • 想用描述而不是排序搜索它会发生这种情况吗?
    • @MagedMohamed:为什么不显示产品类别或标签列表?
    • 我在脑海中将每 3 个具有相同描述的产品分组,因此需要按描述搜索以列出包含这 3 个产品的所有订单
    猜你喜欢
    • 2018-07-11
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 2017-07-17
    • 1970-01-01
    • 2018-12-10
    相关资源
    最近更新 更多