php [MarketPress] - 管理产品列表。在管理产品表中添加选择选项“无类别”。这应该显示所有产品

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [MarketPress] - 管理产品列表。在管理产品表中添加选择选项“无类别”。这应该显示所有产品相关的知识,希望对你有一定的参考价值。

<?php
/**
* Plugin Name: [MarketPress] - Admin Products List
* Plugin URI: https://premium.wpmudev.org/
* Description: Adds a select option "No category" in admin products table. This should display all products that have no product category set.
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'WPMUDEV_MP_Admin_List_Table' ) ) {
    
    class WPMUDEV_MP_Admin_List_Table {
        private static $_instance = null;
        private static $_products_taxonomy = null;
        
        public static function get_instance() {
            if( is_null( self::$_instance ) ){
                self::$_instance = new WPMUDEV_MP_Admin_List_Table();
            }
            return self::$_instance;
            
        }

        private function __construct() {

            self::$_products_taxonomy = 'product_category';
            
            add_filter( 'wp_dropdown_cats', array( $this, 'no_term_option' ) );
            add_action( 'parse_query', array( $this, 'no_term_filter' ), 999 );
        }

        public function no_term_option( $html ){

            global $pagenow, $wp_query;
            $qv = &$wp_query->query_vars;

            if ( ( 'edit.php' === $pagenow ) && ( 'product' === $qv['post_type'] ) ) {
                $html = str_replace( '</select>', '<option value="none" '. selected( $_GET['cat'], 'none', false ) .'>No category</option></select>', $html );
            }

            return $html;

        }

        public function no_term_filter( $query ){

            global $pagenow;
            $qv = &$query->query_vars;
            
            //$qv['cat'] has been unset in class-mp-products-admin.php. Lets use $_GET instead
            if ( ( 'edit.php' === $pagenow )  && ( 'product' === $qv['post_type'] ) && ( ! empty( $_GET['cat'] ) ) && ( $_GET['cat'] == 'none' ) ) {
                $qv['tax_query'] = array(
                    array(
                        'taxonomy' => self::$_products_taxonomy,
                        'field' => 'term_id',
                        'terms'    => get_terms( self::$_products_taxonomy, [ 'fields' => 'ids'  ] ),
                        'operator' => 'NOT IN'
                    ),
                );

            }
        }

    }  

    add_action( 'plugins_loaded', function(){
        $GLOBALS['WPMUDEV_MP_Admin_List_Table'] = WPMUDEV_MP_Admin_List_Table::get_instance();
    }, 10 );

}

以上是关于php [MarketPress] - 管理产品列表。在管理产品表中添加选择选项“无类别”。这应该显示所有产品的主要内容,如果未能解决你的问题,请参考以下文章

php [MarketPress] - 重复产品

php [MarketPress - Upfront] - 产品搜索

php [SmartCrawl]通过为MarketPress和WooCommerce产品生成和呈现产品类型OpenGraph数据来补充SmartCrawl OpenGraph

php [MarketPress] - 预览订单通知

php [MarketPress] - 结帐自定义字段

php [MarketPress] - 出口订单