0

I want to remove the editor and set up only the excerpt to a custom product type (WooCommerce) in my theme, is this possible?

This is how i add my custom product type to the WooCommerce product type selector

<?php
defined('ABSPATH') || exit;

class Custom_Product_Type(){
    public function __construct() {
        add_filter('product_type_selector', array($this, 'product_type_selector'));
    }

    function product_type_selector($types) {
        $types['custom_product_type'] = 'Custom product type';
        return $types;
    }
}
new Custom_Product_Type();
9
  • What's a "custom product type"? May 21, 2019 at 15:19
  • Product type from WooCommerce, I edited the text to inform that is from WooCommerce May 21, 2019 at 15:22
  • What, like Simple or Variable? May 21, 2019 at 15:24
  • Yes, but a custom product type created from another plugin May 21, 2019 at 15:26
  • how did you registered your custom product type… Could you share some code in your question please? without that your question is just too vague and unclear. May 21, 2019 at 16:24

2 Answers 2

1

Yes, You can disable the default editor by removing 'editor' from the supports attribute array for register_post_type( 'product', $args )

'supports'  => array( 'title', 'editor', 'thumbnail', 'comments', 'custom-fields' ),

Change this to somthing like this

'supports'  => array( 'title', 'thumbnail', 'comments', 'custom-fields' ),

You can read wordpress function reference for register_post_type to learn more about custom post type arguments.

Update:

Method 1:

To change register_post_type args via "register_post_type_args" filter hook.

add_filter( 'register_post_type_args', 'product_support_args', 10, 2 );
function product_support_args( $args, $post_type ){

    $post_ids_array = array(2119, 2050); // Add Your Allowed Post IDs Here

    if ( 'product' === $post_type && is_admin()){

        $post_id = $_GET['post']; // Get Post ID from URL

        // Check if the post ID is whitelisted
        if(in_array($post_id, $post_ids_array)){
            $args['supports'] = array( 'title', 'thumbnail', 'comments', 'custom-fields' ); // Remove Editor from supports args array
        } 

    }

    return $args;

}

Method 2:

By using remove_post_type_support() function. You can pass allowed post ids array in the same way we did above if required.

add_action( 'current_screen', 'remove_editor_support' );
function remove_editor_support() {

    $get_screen = get_current_screen();
    $current_screen = $get_screen->post_type;
    $post_type = 'product'; // change post type here

    if ($current_screen == $post_type ) {   
        remove_post_type_support( $current_screen, 'editor' ); // remove editor from support argument
    }   

}
13
  • But this will change for all products, no? May 21, 2019 at 15:23
  • Yes, It will for all posts. If you want to do this for particular posts, You can pass additional condition checks. May 21, 2019 at 15:26
  • Conditional checks for product type? How? May 21, 2019 at 15:27
  • I have updated the answer. May 21, 2019 at 15:46
  • I didn't know about this filter, now instead of veryfing IDs, just need to verify the product type, and all good May 21, 2019 at 15:49
0

I've figured how to hide the editor only when the product type is selected because if i remove the support:

1) The product type is the hidden and then change the product type to another the editor doesn't appear back

2) The product type is not the hidden and then change the product type to hidden the editor dont dissappear

function admin_footer() {
    if ( 'product' !== get_post_type() ) :
        return;
    endif;
    ?><script type='text/javascript'>
        jQuery( document ).ready( function() {
            $("#postdivrich").addClass('hide_if_custom_product_type');
            $("#product-type").trigger('change');
        });
    </script><?php
}
add_action( 'admin_footer', array($this, 'admin_footer'));

This way, every time it selects or deselects the product type, it will hide or show using WooCommerce methods

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.