Woocommerce: assegnare un "autore" a un prodotto

27 nov 2012, 12:41:15
Visualizzazioni: 18.6K
Voti: 11

Sto sviluppando il mio primo tema per Woocommerce.

Ho bisogno di poter assegnare "autori" (in realtà "designer") ai prodotti woocommerce. È fattibile? Stavo pensando di utilizzare l'utente "autore" integrato in WordPress, ma l'interfaccia di modifica di un prodotto non fornisce la casella "autore", a differenza dell'interfaccia di modifica di un "post".

0
Tutte le risposte alla domanda 1
3
17

Basta usare 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' );
}

Utente assegnato con ruolo personalizzato

ruolo utente


Autori abilitati nel tipo di post Prodotti

prodotti WooCommerce con autore abilitato


Un'altra opzione, di cui non sono sicuro della sua correttezza, è agganciarsi a woocommerce_register_post_type* e registrare prima il tipo di post. Questa è una copia della funzione originale più le variabili necessarie, con author aggiunto all'argomento supports.

* /wp-content/plugins/woocommerce/woocommerce.php, linea 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
        )
    );
}
29 nov 2012 16:04:34
Commenti

L'ultimo codice di WooCommerce include filtri per ogni tipo di post personalizzato registrato apply_filters( 'woocommerce_register_post_type_product', quindi il filtraggio è una soluzione accettabile.

Anagio Anagio
20 gen 2014 12:05:12

Cosa significa 999 nel metodo add_action()?

Atul Chavan Atul Chavan
29 gen 2019 16:50:43

@Atul, quella è la priorità per l'esecuzione dell'azione

brasofilo brasofilo
29 gen 2019 18:33:05