Jeffrey
Jeffrey

Reputation: 23

Wordpress doesn't sync WooCommerce taxonomies after $wpdb insert

I'm creating a custom product insertion plugin, but there doesn't seem to be built in functions for inserting new product taxonomies. Here's what I do:

function insert_product_terms( $taxonomy ){
  global $wpdb;
  $result = $wpdb->get_row( "SELECT * FROM wp_woocommerce_attribute_taxonomies WHERE attribute_name = '" .     sanitize_title_for_query( $taxonomy ) . "'" );
  if(empty($result)){
    $wpdb->insert(
      'wp_woocommerce_attribute_taxonomies',
      array(
        'attribute_name' => sanitize_title_for_query( $taxonomy ),
        'attribute_label' => $taxonomy,
        'attribute_type' => 'select',
        'attribute_orderby' => 'menu_order',
        'attribute_public' => '0'
      ),
      array('%s', '%s', '%s', '%s', '%s')
    );
  }
  return sanitize_title_for_query($taxonomy);
}

This does insert the taxonomy I want into the database, but for some reason if I go to the front-end Woo Commerce attributes page it doesn't show until I add another one through the front end. Does anyone know how to solve this? Thanks in advance.

Upvotes: 0

Views: 256

Answers (1)

Reigel Gallarde
Reigel Gallarde

Reputation: 65274

try adding this two lines before you do return...

    flush_rewrite_rules();
    delete_transient( 'wc_attribute_taxonomies' );

like this

function insert_product_terms( $taxonomy ){
  global $wpdb;
  $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = '" .     sanitize_title_for_query( $taxonomy ) . "'" );
  if(empty($result)){
    $wpdb->insert(
      $wpdb->prefix . 'woocommerce_attribute_taxonomies',
      array(
        'attribute_name' => sanitize_title_for_query( $taxonomy ),
        'attribute_label' => $taxonomy,
        'attribute_type' => 'select',
        'attribute_orderby' => 'menu_order',
        'attribute_public' => '0'
      ),
      array('%s', '%s', '%s', '%s', '%s')
    );
  }
    flush_rewrite_rules();
    delete_transient( 'wc_attribute_taxonomies' );
  return sanitize_title_for_query($taxonomy);
}

Upvotes: 1

Related Questions