Skip to content

Instantly share code, notes, and snippets.

@chrono-meter
Created February 5, 2019 09:45
Show Gist options
  • Save chrono-meter/5292f126c6399adb4e4d2a8cd3f33949 to your computer and use it in GitHub Desktop.
Save chrono-meter/5292f126c6399adb4e4d2a8cd3f33949 to your computer and use it in GitHub Desktop.
FIX: WooCommerce doesn't support attribute taxonomy creation on non startup.
<?php
/**
* @wordpress-plugin
* Plugin Name: FIX: WooCommerce doesn't support attribute taxonomy creation on non startup.
* Depends: WooCommerce
* Plugin URI:
* Description:
* Version: 1.0.0
* Author: chrono-meter@gmx.net
* Author URI: mailto:chrono-meter@gmx.net
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* @see WC_Product_Importer::get_attribute_taxonomy_id()
* @see WC_Admin_Importers::post_importer_compatibility()
* @see wc_create_attribute()
* @see WC_Post_Types::register_taxonomies()
* @see WC_Product_Attribute::get_terms()
* @see taxonomy_exists()
*/
defined( 'ABSPATH' ) or exit;
add_action( 'woocommerce_attribute_added', function( $id, $data ) {
global $wc_product_attributes;
$permalinks = wc_get_permalink_structure();
$tax = (object) $data;
$name = wc_attribute_taxonomy_name( $tax->attribute_name );
if ( !taxonomy_exists( $name ) && $name ) {
$tax->attribute_public = absint( isset( $tax->attribute_public ) ? $tax->attribute_public : 1 );
$label = ! empty( $tax->attribute_label ) ? $tax->attribute_label : $tax->attribute_name;
$wc_product_attributes[ $name ] = $tax;
$taxonomy_data = array(
'hierarchical' => false,
'update_count_callback' => '_update_post_term_count',
'labels' => array(
/* translators: %s: attribute name */
'name' => sprintf( _x( 'Product %s', 'Product Attribute', 'woocommerce' ), $label ),
'singular_name' => $label,
/* translators: %s: attribute name */
'search_items' => sprintf( __( 'Search %s', 'woocommerce' ), $label ),
/* translators: %s: attribute name */
'all_items' => sprintf( __( 'All %s', 'woocommerce' ), $label ),
/* translators: %s: attribute name */
'parent_item' => sprintf( __( 'Parent %s', 'woocommerce' ), $label ),
/* translators: %s: attribute name */
'parent_item_colon' => sprintf( __( 'Parent %s:', 'woocommerce' ), $label ),
/* translators: %s: attribute name */
'edit_item' => sprintf( __( 'Edit %s', 'woocommerce' ), $label ),
/* translators: %s: attribute name */
'update_item' => sprintf( __( 'Update %s', 'woocommerce' ), $label ),
/* translators: %s: attribute name */
'add_new_item' => sprintf( __( 'Add new %s', 'woocommerce' ), $label ),
/* translators: %s: attribute name */
'new_item_name' => sprintf( __( 'New %s', 'woocommerce' ), $label ),
/* translators: %s: attribute name */
'not_found' => sprintf( __( 'No &quot;%s&quot; found', 'woocommerce' ), $label ),
),
'show_ui' => true,
'show_in_quick_edit' => false,
'show_in_menu' => false,
'meta_box_cb' => false,
'query_var' => 1 === $tax->attribute_public,
'rewrite' => false,
'sort' => false,
'public' => 1 === $tax->attribute_public,
'show_in_nav_menus' => 1 === $tax->attribute_public && apply_filters( 'woocommerce_attribute_show_in_nav_menus', false, $name ),
'capabilities' => array(
'manage_terms' => 'manage_product_terms',
'edit_terms' => 'edit_product_terms',
'delete_terms' => 'delete_product_terms',
'assign_terms' => 'assign_product_terms',
),
);
if ( 1 === $tax->attribute_public && sanitize_title( $tax->attribute_name ) ) {
$taxonomy_data['rewrite'] = array(
'slug' => trailingslashit( $permalinks['attribute_rewrite_slug'] ) . sanitize_title( $tax->attribute_name ),
'with_front' => false,
'hierarchical' => true,
);
}
register_taxonomy( $name, apply_filters( "woocommerce_taxonomy_objects_{$name}", array( 'product' ) ), apply_filters( "woocommerce_taxonomy_args_{$name}", $taxonomy_data ) );
}
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment