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_term', array( $this, 'delete_term' ), 5 );
// 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
* @param mixed $tt_id
* @param string $taxonomy
*/
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_woocommerce_term_meta( $term_id, $meta_name, 0 );
}
/**
* When a term is deleted, delete its meta.
*
* @param mixed $term_id
*/
public function delete_term( $term_id ) {
global $wpdb;
$term_id = absint( $term_id );
if ( $term_id && get_option( 'db_version' ) < 34370 ) {
$wpdb->delete( $wpdb->woocommerce_termmeta, array( 'woocommerce_term_id' => $term_id ), array( '%d' ) );
}
}
/**
* Category thumbnail fields.
*/
public function add_category_fields() {
?>
term_id, 'display_type', true );
$thumbnail_id = absint( get_woocommerce_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();
}
?>
|
|
|
|
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' ) );
}
/**
* Thumbnail column added to category admin.
*
* @param mixed $columns
* @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 = array(), $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&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'] ) {
$make_default_id = absint( $_GET['tag_ID'] );
if ( wp_verify_nonce( $_GET['_wpnonce'], 'make_default_' . $make_default_id ) && current_user_can( 'edit_term', $make_default_id ) ) {
update_option( 'default_product_cat', $make_default_id );
}
}
}
/**
* Thumbnail column value added to category admin.
*
* @param string $columns
* @param string $column
* @param int $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_woocommerce_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 .= '';
}
if ( 'handle' === $column ) {
$columns .= '';
}
return $columns;
}
/**
* Maintain term hierarchy when editing a product.
*
* @param array $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'] ) {
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 );
");
}
}
new WC_Admin_Taxonomies();