skip to Main Content

I’ve been searching all over the internet for hours how to automatically assign a category to new products. When you open the new product that the category is selected.

I have two product categories (Books and FIlms) and I would like that every time we open a new product, Books are selected. In this way it will directly show the custom fields of that product category and we will speed up the creation process.

It’s possible?
thanks


UPDATE

I found a code that seems to work with the posts. Any idea how I can adapt the code for products and where should I put the category I care about?

https://wordpress.stackexchange.com/questions/243462/automatically-select-categories-on-new-post-based-on-get-value

2

Answers


  1. In product categories click the Make default action of your desired category to make it as default category. For more details refer the image:

    Product Category

    You can visit this page using this URL (replace the domain with your domain):
    www.example.com/wp-admin/edit-tags.php?taxonomy=product_cat&post_type=product

    Login or Signup to reply.
  2. When it comes to the checkbox that should already be checked when creating a new product, you can use jQuery

    Replace in-product_cat-15 with in-product_cat-{YOUR-CAT-ID}

    // Define the admin_head callback 
    function action_admin_head() {
        global $post, $pagenow;
    
        // Only on new product pages    
        if( $pagenow != 'post-new.php' ) return;
        ?>
        <script>    
        jQuery(function($){
            $( '#in-product_cat-15' ).attr( 'checked', true );
        });
        </script>
        <?php
    }
    add_action( 'admin_head', 'action_admin_head', 10, 0 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search