WooCommerce订单状态在订单页面上显示两次

时间:2013-02-09 作者:Jeremiah Prummer

我修改了woocommerce\\u custom\\u order\\u columns函数,对woocommerce管理订单页面进行了一些更改。一切都很好,除了订单状态显示两次(但仅针对最新(顶级)订单)。我附上了一张图片和可能导致这种情况的代码。我不知道会发生什么。

enter image description here

我的代码:

add_action(\'manage_shop_order_posts_custom_column\', \'match_order_woocommerce_custom_order_columns\', 2);

function match_order_woocommerce_custom_order_columns( $column ) {

remove_action(\'manage_shop_order_posts_custom_column\', \'woocommerce_custom_order_columns\', 2);

global $post, $woocommerce;
$order = new WC_Order( $post->ID );

switch ($column) {
    case "order_status" :

        printf( \'<mark class="%s">%s</mark>\', sanitize_title($order->status), __($order->status, \'woocommerce\') );

    break;
    case "order_title" :

        if ($order->user_id) $user_info = get_userdata($order->user_id);

        if (isset($user_info) && $user_info) :

            $user = \'<a href="user-edit.php?user_id=\' . esc_attr( $user_info->ID ) . \'">\';

            if ($user_info->first_name || $user_info->last_name) $user .= $user_info->first_name.\' \'.$user_info->last_name;
            else $user .= esc_html( $user_info->display_name );

            $user .= \'</a>\';

        else :
            $user = __(\'Guest\', \'woocommerce\');
        endif;

        echo \'<a href="\'.admin_url(\'post.php?post=\'.$post->ID.\'&action=edit\').\'"><strong>\'.sprintf( __(\'Order %s\', \'woocommerce\'), $order->get_order_number() ).\'</strong></a> \' . __(\'made by\', \'woocommerce\') . \' \' . $user;

        if ($order->billing_email) :
            echo \'<small class="meta">\'.__(\'Email:\', \'woocommerce\') . \' \' . \'<a href="\' . esc_url( \'mailto:\'.$order->billing_email ).\'">\'.esc_html( $order->billing_email ).\'</a></small>\';
        endif;
        if ($order->billing_phone) :
            echo \'<small class="meta">\'.__(\'Tel:\', \'woocommerce\') . \' \' . esc_html( $order->billing_phone ) . \'</small>\';
        endif;

    break;
    case "billing_address" :
        if ($order->get_formatted_billing_address()) :

            echo \'<a target="_blank" href="\' . esc_url( \'http://maps.google.com/maps?&q=\'.urlencode( $order->get_billing_address() ).\'&z=16\' ) . \'">\'. preg_replace(\'#<br\\s*/?>#i\', \', \', $order->get_formatted_billing_address()) .\'</a>\';
        else :
            echo \'&ndash;\';
        endif;

        if ($order->payment_method_title) :
            echo \'<small class="meta">\' . __(\'Via\', \'woocommerce\') . \' \' . esc_html( $order->payment_method_title ) . \'</small>\';
        endif;

    break;
    case "shipping_address" :
        if ($order->get_formatted_shipping_address()) :

            echo \'<a target="_blank" href="\' . esc_url( \'http://maps.google.com/maps?&q=\'.urlencode( $order->get_shipping_address() ).\'&z=16\' ) .\'">\'. preg_replace(\'#<br\\s*/?>#i\', \', \', $order->get_formatted_shipping_address()) .\'</a>\';
        else :
            echo \'&ndash;\';
        endif;

        if ($order->shipping_method_title) :
            echo \'<small class="meta">\' . __(\'Via\', \'woocommerce\') . \' \' . esc_html( $order->shipping_method_title ) . \'</small>\';
        endif;
    break;
    case "total_cost" :
        echo $order->get_formatted_order_total();
    break;
    case "order_date" :

        if ( \'0000-00-00 00:00:00\' == $post->post_date ) :
            $t_time = $h_time = __( \'Unpublished\', \'woocommerce\' );
        else :
            $t_time = get_the_time( __( \'Y/m/d g:i:s A\', \'woocommerce\' ), $post );

            $gmt_time = strtotime($post->post_date_gmt);
            $time_diff = current_time(\'timestamp\', 1) - $gmt_time;

            if ( $time_diff > 0 && $time_diff < 24*60*60 )
                $h_time = sprintf( __( \'%s ago\', \'woocommerce\' ), human_time_diff( $gmt_time, current_time(\'timestamp\', 1) ) );
            else
                $h_time = get_the_time( __( \'Y/m/d\', \'woocommerce\' ), $post );
        endif;

        echo \'<abbr title="\' . $t_time . \'">\' . apply_filters( \'post_date_column_time\', $h_time, $post ) . \'</abbr>\';

    break;
    case "order_actions" :

        ?><p>
            <?php
                do_action( \'woocommerce_admin_order_actions_start\', $order );

                $actions = array();

                if ( in_array( $order->status, array( \'pending\', \'on-hold\' ) ) )
                    $actions[] = array(
                        \'url\'       => wp_nonce_url( admin_url( \'admin-ajax.php?action=woocommerce-mark-order-processing&order_id=\' . $post->ID ), \'woocommerce-mark-order-processing\' ),
                        \'name\'      => __( \'Processing\', \'woocommerce\' ),
                        \'action\'    => "processing"
                    );

                if ( in_array( $order->status, array( \'pending\', \'on-hold\', \'processing\' ) ) )
                    $actions[] = array(
                        \'url\'       => wp_nonce_url( admin_url( \'admin-ajax.php?action=woocommerce-mark-order-complete&order_id=\' . $post->ID ), \'woocommerce-mark-order-complete\' ),
                        \'name\'      => __( \'Complete\', \'woocommerce\' ),
                        \'action\'    => "complete"
                    );

                $actions[] = array(
                        \'url\'       => admin_url( \'post.php?post=\' . $post->ID . \'&action=edit\' ),
                        \'name\'      => __( \'View\', \'woocommerce\' ),
                        \'action\'    => "view"
                    );

                $actions = apply_filters( \'woocommerce_admin_order_actions\', $actions, $order );

                foreach ( $actions as $action )
                    printf( \'<a class="button tips" href="%s" data-tip="%s"><img src="%s" alt="%s" width="14" /></a>\', $action[\'url\'], $action[\'name\'], $woocommerce->plugin_url() . \'/assets/images/icons/\' . $action[\'action\'] . \'.png\', $action[\'name\'] );

                do_action( \'woocommerce_admin_order_actions_end\', $order );
            ?>
        </p><?php

    break;
    case "note" :
        if ( get_post_meta( $post->ID , \'match_order\' , TRUE) == 1 ) {
            echo \'<img src="\' . WP_PLUGIN_URL . \'/match_previous_order/note-red.png" alt="Match Previous Order" class="tips" data-tip="\'. __(\'Match Previous Order\', \'woocommerce\') .\'" width="14" height="14" />\';
        }
        else if ($order->customer_note) {
            echo \'<img src="\'.$woocommerce->plugin_url().\'/assets/images/note.png" alt="yes" class="tips" data-tip="\'. __(\'Yes\', \'woocommerce\') .\'" width="14" height="14" />\';
        }
        else {
            echo \'<img src="\'.$woocommerce->plugin_url().\'/assets/images/note-off.png" alt="no" class="tips" data-tip="\'. __(\'No\', \'woocommerce\') .\'" width="14" height="14" />\';
        }
    break;
    case "order_comments" :

        echo \'<div class="post-com-count-wrapper">
            <a href="\'. admin_url(\'post.php?post=\'.$post->ID.\'&action=edit\') .\'" class="post-com-count"><span class="comment-count">\'. $post->comment_count .\'</span></a>
            </div>\';
    break;
}
}
WooCommerce代码:

function woocommerce_custom_order_columns( $column ) {

global $post, $woocommerce;
$order = new WC_Order( $post->ID );

switch ($column) {
    case "order_status" :

        printf( \'<mark class="%s">%s</mark>\', sanitize_title($order->status), __($order->status, \'woocommerce\') );

    break;
    case "order_title" :

        if ($order->user_id) $user_info = get_userdata($order->user_id);

        if (isset($user_info) && $user_info) :

            $user = \'<a href="user-edit.php?user_id=\' . esc_attr( $user_info->ID ) . \'">\';

            if ($user_info->first_name || $user_info->last_name) $user .= $user_info->first_name.\' \'.$user_info->last_name;
            else $user .= esc_html( $user_info->display_name );

            $user .= \'</a>\';

        else :
            $user = __(\'Guest\', \'woocommerce\');
        endif;

        echo \'<a href="\'.admin_url(\'post.php?post=\'.$post->ID.\'&action=edit\').\'"><strong>\'.sprintf( __(\'Order %s\', \'woocommerce\'), $order->get_order_number() ).\'</strong></a> \' . __(\'made by\', \'woocommerce\') . \' \' . $user;

        if ($order->billing_email) :
            echo \'<small class="meta">\'.__(\'Email:\', \'woocommerce\') . \' \' . \'<a href="\' . esc_url( \'mailto:\'.$order->billing_email ).\'">\'.esc_html( $order->billing_email ).\'</a></small>\';
        endif;
        if ($order->billing_phone) :
            echo \'<small class="meta">\'.__(\'Tel:\', \'woocommerce\') . \' \' . esc_html( $order->billing_phone ) . \'</small>\';
        endif;

    break;
    case "billing_address" :
        if ($order->get_formatted_billing_address()) :

            echo \'<a target="_blank" href="\' . esc_url( \'http://maps.google.com/maps?&q=\'.urlencode( $order->get_billing_address() ).\'&z=16\' ) . \'">\'. preg_replace(\'#<br\\s*/?>#i\', \', \', $order->get_formatted_billing_address()) .\'</a>\';
        else :
            echo \'&ndash;\';
        endif;

        if ($order->payment_method_title) :
            echo \'<small class="meta">\' . __(\'Via\', \'woocommerce\') . \' \' . esc_html( $order->payment_method_title ) . \'</small>\';
        endif;

    break;
    case "shipping_address" :
        if ($order->get_formatted_shipping_address()) :

            echo \'<a target="_blank" href="\' . esc_url( \'http://maps.google.com/maps?&q=\'.urlencode( $order->get_shipping_address() ).\'&z=16\' ) .\'">\'. preg_replace(\'#<br\\s*/?>#i\', \', \', $order->get_formatted_shipping_address()) .\'</a>\';
        else :
            echo \'&ndash;\';
        endif;

        if ($order->shipping_method_title) :
            echo \'<small class="meta">\' . __(\'Via\', \'woocommerce\') . \' \' . esc_html( $order->shipping_method_title ) . \'</small>\';
        endif;
    break;
    case "total_cost" :
        echo $order->get_formatted_order_total();
    break;
    case "order_date" :

        if ( \'0000-00-00 00:00:00\' == $post->post_date ) :
            $t_time = $h_time = __( \'Unpublished\', \'woocommerce\' );
        else :
            $t_time = get_the_time( __( \'Y/m/d g:i:s A\', \'woocommerce\' ), $post );

            $gmt_time = strtotime($post->post_date_gmt);
            $time_diff = current_time(\'timestamp\', 1) - $gmt_time;

            if ( $time_diff > 0 && $time_diff < 24*60*60 )
                $h_time = sprintf( __( \'%s ago\', \'woocommerce\' ), human_time_diff( $gmt_time, current_time(\'timestamp\', 1) ) );
            else
                $h_time = get_the_time( __( \'Y/m/d\', \'woocommerce\' ), $post );
        endif;

        echo \'<abbr title="\' . $t_time . \'">\' . apply_filters( \'post_date_column_time\', $h_time, $post ) . \'</abbr>\';

    break;
    case "order_actions" :

        ?><p>
            <?php
                do_action( \'woocommerce_admin_order_actions_start\', $order );

                $actions = array();

                if ( in_array( $order->status, array( \'pending\', \'on-hold\' ) ) )
                    $actions[] = array(
                        \'url\'       => wp_nonce_url( admin_url( \'admin-ajax.php?action=woocommerce-mark-order-processing&order_id=\' . $post->ID ), \'woocommerce-mark-order-processing\' ),
                        \'name\'      => __( \'Processing\', \'woocommerce\' ),
                        \'action\'    => "processing"
                    );

                if ( in_array( $order->status, array( \'pending\', \'on-hold\', \'processing\' ) ) )
                    $actions[] = array(
                        \'url\'       => wp_nonce_url( admin_url( \'admin-ajax.php?action=woocommerce-mark-order-complete&order_id=\' . $post->ID ), \'woocommerce-mark-order-complete\' ),
                        \'name\'      => __( \'Complete\', \'woocommerce\' ),
                        \'action\'    => "complete"
                    );

                $actions[] = array(
                        \'url\'       => admin_url( \'post.php?post=\' . $post->ID . \'&action=edit\' ),
                        \'name\'      => __( \'View\', \'woocommerce\' ),
                        \'action\'    => "view"
                    );

                $actions = apply_filters( \'woocommerce_admin_order_actions\', $actions, $order );

                foreach ( $actions as $action )
                    printf( \'<a class="button tips" href="%s" data-tip="%s"><img src="%s" alt="%s" width="14" /></a>\', $action[\'url\'], $action[\'name\'], $woocommerce->plugin_url() . \'/assets/images/icons/\' . $action[\'action\'] . \'.png\', $action[\'name\'] );

                do_action( \'woocommerce_admin_order_actions_end\', $order );
            ?>
        </p><?php

    break;
    case "note" :
        if ($order->customer_note)
            echo \'<img src="\'.$woocommerce->plugin_url().\'/assets/images/note.png" alt="yes" class="tips" data-tip="\'. __(\'Yes\', \'woocommerce\') .\'" width="14" height="14" />\';
        else
            echo \'<img src="\'.$woocommerce->plugin_url().\'/assets/images/note-off.png" alt="no" class="tips" data-tip="\'. __(\'No\', \'woocommerce\') .\'" width="14" height="14" />\';

    break;
    case "order_comments" :

        echo \'<div class="post-com-count-wrapper">
            <a href="\'. admin_url(\'post.php?post=\'.$post->ID.\'&action=edit\') .\'" class="post-com-count"><span class="comment-count">\'. $post->comment_count .\'</span></a>
            </div>\';
    break;
}
}

add_action(\'manage_shop_order_posts_custom_column\', \'woocommerce_custom_order_columns\', 2);
如您所见,我在这里所做的唯一一件事就是删除默认值woocommerce_custom_order_columns 函数,并将其替换为match_order_woocommerce_custom_order_columns 作用其他一切都一样。

1 个回复
最合适的回答,由SO网友:Jeremiah Prummer 整理而成

解决了它。我使用了plugins\\u加载的挂钩,如下所示:

add_action(\'plugins_loaded\',\'example_function\'); 

function example_function(){

remove_action(\'manage_shop_order_posts_custom_column\', \'woocommerce_custom_order_columns\', 2);
add_action(\'manage_shop_order_posts_custom_column\', \'match_order_woocommerce_custom_order_columns\', 2);

}

结束

相关推荐

Per theme plugins?

我正在构建一个插件,它可以对博客页面的评论部分进行大量修改。它认为我不能用一种适用于所有主题的方式来实现这一点——根据使用的特定主题,必须调整一些CSS。我猜在将来,插件还会在数据库中存储一些数据。所以我想它应该是一个插件,而不是一个主题。(?)因此,这些问题:有没有可能以某种方式表明插件只能处理这个主题和那个主题?然后发布一些相当简单的插件版本,每个版本都针对一组特定的主题进行了调整?或者插件应该与。。。是否存在所有主题?除了按主题插件和/或分叉和修改现有主题之外,你能想出其他解决方案吗?也许是儿童主题