default_cat_id = get_option( 'default_product_cat', 0 ); // Category/term ordering. add_action( 'create_term', array( $this, 'create_term' ), 5, 3 ); add_action( 'delete_product_cat', array( $this, 'maybe_assign_default_product_cat' ) ); // Add form. add_action( 'product_cat_add_form_fields', array( $this, 'add_category_fields' ) ); add_action( 'product_cat_edit_form_fields', array( $this, 'edit_category_fields' ), 10 ); add_action( 'created_term', array( $this, 'save_category_fields' ), 10, 3 ); add_action( 'edit_term', array( $this, 'save_category_fields' ), 10, 3 ); // Add columns. add_filter( 'manage_edit-product_cat_columns', array( $this, 'product_cat_columns' ) ); add_filter( 'manage_product_cat_custom_column', array( $this, 'product_cat_column' ), 10, 3 ); // Add row actions. add_filter( 'product_cat_row_actions', array( $this, 'product_cat_row_actions' ), 10, 2 ); add_filter( 'admin_init', array( $this, 'handle_product_cat_row_actions' ) ); // Taxonomy page descriptions. add_action( 'product_cat_pre_add_form', array( $this, 'product_cat_description' ) ); add_action( 'after-product_cat-table', array( $this, 'product_cat_notes' ) ); $attribute_taxonomies = wc_get_attribute_taxonomies(); if ( ! empty( $attribute_taxonomies ) ) { foreach ( $attribute_taxonomies as $attribute ) { add_action( 'pa_' . $attribute->attribute_name . '_pre_add_form', array( $this, 'product_attribute_description' ) ); } } // Maintain hierarchy of terms. add_filter( 'wp_terms_checklist_args', array( $this, 'disable_checked_ontop' ) ); // Admin footer scripts for this product categories admin screen. add_action( 'admin_footer', array( $this, 'scripts_at_product_cat_screen_footer' ) ); } /** * Order term when created (put in position 0). * * @param mixed $term_id Term ID. * @param mixed $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ public function create_term( $term_id, $tt_id = '', $taxonomy = '' ) { if ( 'product_cat' != $taxonomy && ! taxonomy_is_product_attribute( $taxonomy ) ) { return; } $meta_name = taxonomy_is_product_attribute( $taxonomy ) ? 'order_' . esc_attr( $taxonomy ) : 'order'; update_term_meta( $term_id, $meta_name, 0 ); } /** * When a term is deleted, delete its meta. * * @deprecated 3.6.0 No longer needed. * @param mixed $term_id Term ID. */ public function delete_term( $term_id ) { wc_deprecated_function( 'delete_term', '3.6' ); } /** * Assigns default product category. This is done when the product * has no assgined product category. * * @since 5.3 * @return void */ public function maybe_assign_default_product_cat() { /* * When a product category is deleted, we need to check * if the product has no categories assigned. Then assign * it a default category. */ _wc_maybe_assign_default_product_cat(); } /** * Category thumbnail fields. */ public function add_category_fields() { ?>
term_id, 'display_type', true ); $thumbnail_id = absint( get_term_meta( $term->term_id, 'thumbnail_id', true ) ); if ( $thumbnail_id ) { $image = wp_get_attachment_thumb_url( $thumbnail_id ); } else { $image = wc_placeholder_img_src(); } ?>
array() ) ); } /** * Add some notes to describe the behavior of the default category. */ public function product_cat_notes() { $category_id = get_option( 'default_product_cat', 0 ); $category = get_term( $category_id, 'product_cat' ); $category_name = ( ! $category || is_wp_error( $category ) ) ? _x( 'Uncategorized', 'Default category slug', 'woocommerce' ) : $category->name; ?>


' . esc_html( $category_name ) . '' ); ?>


Note: Deleting a term will remove it from all products and variations to which it has been assigned. Recreating a term will not automatically assign it back to products.', 'woocommerce' ) ), array( 'p' => array() ) ); } /** * Thumbnail column added to category admin. * * @param mixed $columns Columns array. * @return array */ public function product_cat_columns( $columns ) { $new_columns = array(); if ( isset( $columns['cb'] ) ) { $new_columns['cb'] = $columns['cb']; unset( $columns['cb'] ); } $new_columns['thumb'] = __( 'Image', 'woocommerce' ); $columns = array_merge( $new_columns, $columns ); $columns['handle'] = ''; return $columns; } /** * Adjust row actions. * * @param array $actions Array of actions. * @param object $term Term object. * @return array */ public function product_cat_row_actions( $actions, $term ) { $default_category_id = absint( get_option( 'default_product_cat', 0 ) ); if ( $default_category_id !== $term->term_id && current_user_can( 'edit_term', $term->term_id ) ) { $actions['make_default'] = sprintf( '%s', wp_nonce_url( 'edit-tags.php?action=make_default&taxonomy=product_cat&post_type=product&tag_ID=' . absint( $term->term_id ), 'make_default_' . absint( $term->term_id ) ), /* translators: %s: taxonomy term name */ esc_attr( sprintf( __( 'Make “%s” the default category', 'woocommerce' ), $term->name ) ), __( 'Make default', 'woocommerce' ) ); } return $actions; } /** * Handle custom row actions. */ public function handle_product_cat_row_actions() { if ( isset( $_GET['action'], $_GET['tag_ID'], $_GET['_wpnonce'] ) && 'make_default' === $_GET['action'] ) { // WPCS: CSRF ok, input var ok. $make_default_id = absint( $_GET['tag_ID'] ); // WPCS: Input var ok. if ( wp_verify_nonce( $_GET['_wpnonce'], 'make_default_' . $make_default_id ) && current_user_can( 'edit_term', $make_default_id ) ) { // WPCS: Sanitization ok, input var ok, CSRF ok. update_option( 'default_product_cat', $make_default_id ); } } } /** * Thumbnail column value added to category admin. * * @param string $columns Column HTML output. * @param string $column Column name. * @param int $id Product ID. * * @return string */ public function product_cat_column( $columns, $column, $id ) { if ( 'thumb' === $column ) { // Prepend tooltip for default category. $default_category_id = absint( get_option( 'default_product_cat', 0 ) ); if ( $default_category_id === $id ) { $columns .= wc_help_tip( __( 'This is the default category and it cannot be deleted. It will be automatically assigned to products with no category.', 'woocommerce' ) ); } $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); if ( $thumbnail_id ) { $image = wp_get_attachment_thumb_url( $thumbnail_id ); } else { $image = wc_placeholder_img_src(); } // Prevent esc_url from breaking spaces in urls for image embeds. Ref: https://core.trac.wordpress.org/ticket/23605 . $image = str_replace( ' ', '%20', $image ); $columns .= '' . esc_attr__( 'Thumbnail', 'woocommerce' ) . ''; } if ( 'handle' === $column ) { $columns .= ''; } return $columns; } /** * Maintain term hierarchy when editing a product. * * @param array $args Term checklist args. * @return array */ public function disable_checked_ontop( $args ) { if ( ! empty( $args['taxonomy'] ) && 'product_cat' === $args['taxonomy'] ) { $args['checked_ontop'] = false; } return $args; } /** * Admin footer scripts for the product categories admin screen * * @return void */ public function scripts_at_product_cat_screen_footer() { if ( ! isset( $_GET['taxonomy'] ) || 'product_cat' !== $_GET['taxonomy'] ) { // WPCS: CSRF ok, input var ok. return; } // Ensure the tooltip is displayed when the image column is disabled on product categories. wc_enqueue_js( "(function( $ ) { 'use strict'; var product_cat = $( 'tr#tag-" . absint( $this->default_cat_id ) . "' ); product_cat.find( 'th' ).empty(); product_cat.find( 'td.thumb span' ).detach( 'span' ).appendTo( product_cat.find( 'th' ) ); })( jQuery );" ); } } $wc_admin_taxonomies = WC_Admin_Taxonomies::get_instance();