Woocommerce:为产品指定“作者”


11

我正在为woocommerce开发我的第一个主题。

我需要能够为woocommerce产品分配“作者”(真的是“设计师”)。那可行吗?我曾考虑使用wordpress内置的“作者”用户,但是与“发布”编辑界面不同,产品编辑界面不提供“作者”框。

Answers:


15

只需使用add_post_type_support

add_action('init', 'wpse_74054_add_author_woocommerce', 999 );

function wpse_74054_add_author_woocommerce() {
    add_post_type_support( 'product', 'author' );
}

为用户分配了自定义角色

用户角色


在“产品”帖子类型中启用了作者

woo产品已启用作者


我不确定其正确性的另一种选择是,它挂入woocommerce_register_post_type*并首先注册帖子类型。这是原始函数加上所需变量的副本,author并附加到supports参数中。

* /wp-content/plugins/woocommerce/woocommerce.php,第885行

add_action( 'woocommerce_register_post_type', 'wpse_74054_override_register_product_type' );
function wpse_74054_override_register_product_type()
{
    $shop_page_id = woocommerce_get_page_id('shop');
    $base_slug = ( $shop_page_id > 0 && get_page( $shop_page_id ) ) ? get_page_uri( $shop_page_id ) : 'shop';
    $product_base = ( get_option('woocommerce_prepend_shop_page_to_products') == 'yes' ) ? trailingslashit($base_slug) : trailingslashit(_x('product', 'slug', 'woocommerce'));

    register_post_type( "product",
        array(
            'labels' => array(
                    'name'                  => __( 'Products', 'woocommerce' ),
                    'singular_name'         => __( 'Product', 'woocommerce' ),
                    'menu_name'             => _x( 'Products', 'Admin menu name', 'woocommerce' ),
                    'add_new'               => __( 'Add Product', 'woocommerce' ),
                    'add_new_item'          => __( 'Add New Product', 'woocommerce' ),
                    'edit'                  => __( 'Edit', 'woocommerce' ),
                    'edit_item'             => __( 'Edit Product', 'woocommerce' ),
                    'new_item'              => __( 'New Product', 'woocommerce' ),
                    'view'                  => __( 'View Product', 'woocommerce' ),
                    'view_item'             => __( 'View Product', 'woocommerce' ),
                    'search_items'          => __( 'Search Products', 'woocommerce' ),
                    'not_found'             => __( 'No Products found', 'woocommerce' ),
                    'not_found_in_trash'    => __( 'No Products found in trash', 'woocommerce' ),
                    'parent'                => __( 'Parent Product', 'woocommerce' )
                ),
            'description'           => __( 'This is where you can add new products to your store.', 'woocommerce' ),
            'public'                => true,
            'show_ui'               => true,
            'capability_type'       => 'post',
            'capabilities' => array(
                'publish_posts'         => 'manage_woocommerce_products',
                'edit_posts'            => 'manage_woocommerce_products',
                'edit_others_posts'     => 'manage_woocommerce_products',
                'delete_posts'          => 'manage_woocommerce_products',
                'delete_others_posts'   => 'manage_woocommerce_products',
                'read_private_posts'    => 'manage_woocommerce_products',
                'edit_post'             => 'manage_woocommerce_products',
                'delete_post'           => 'manage_woocommerce_products',
                'read_post'             => 'manage_woocommerce_products'
            ),
            'publicly_queryable'    => true,
            'exclude_from_search'   => false,
            'hierarchical'          => false, // Hierarcal causes memory issues - WP loads all records!
            'rewrite'               => array( 'slug' => $product_base, 'with_front' => false, 'feeds' => $base_slug ),
            'query_var'             => true,
            'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', 'custom-fields', 'page-attributes', 'author' ),
            'has_archive'           => $base_slug,
            'show_in_nav_menus'     => true
        )
    );
}

WooCommerce的最新代码包括针对正在注册的每种自定义帖子类型的过滤器,apply_filters( 'woocommerce_register_post_type_product',因此过滤是一个好的解决方案。
Anagio

999在add_action()方法中意味着什么?
Atul Chavan

@Atul,这是运行该动作的优先级
brasofilo
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.