skip to Main Content

I wish to include different types of Taxes in different classes to be assigned to the products. Unfortunately with the code below I put all the taxes at the standard rate and so I have no way to assign them later to the products.

enter image description here

The problem is that I am unable to create new Additional Tax Classes in a programmatic manner.

$tax_data = array(
   'tax_rate_country' => '*', 
   'tax_rate_state' => '*', 
   'tax_rate' => $tax_rate, 
   'tax_rate_name' => $tax_rate_name, 
   'tax_rate_priority' => 1, 
   'tax_rate_compound' => 0, 
   'tax_rate_shipping' => 1, 
   'tax_rate_order' => 0, 
   'tax_rate_class' => $tax_rate_class
);

$tax_rate_id = WC_Tax::_insert_tax_rate( $tax_data );
WC_Tax::_update_tax_rate_postcodes($tax_rate_id, wc_clean('*'));
WC_Tax::_update_tax_rate_cities($tax_rate_id, wc_clean('*'));

Can anybody help me?

2

Answers


  1. Chosen as BEST ANSWER

    I developed a function, which I reduced to a skeleton here, which works for me.

    $tax_classes = "";
    
    foreach( $taxes_list as $tax ) {
        $tax_information_id = $tax->KODE->__toString();
        $tax_classes .= sanitize_title( $tax_information_id ) . "n";
    }
    
    if ( get_option( "woocommerce_tax_classes" ) ) {
       update_option( "woocommerce_tax_classes", $tax_classes );
    }
    
    foreach( $taxes_list as $tax ) {
        $tax_data = array(
          'tax_rate_country' => '*',
          'tax_rate_state' => '*',
          'tax_rate' => $tax["vat"],
          'tax_rate_name' => $tax["name"],
          'tax_rate_priority' => 1,
          'tax_rate_compound' => 0,
          'tax_rate_shipping' => 1,
          'tax_rate_order' => 0,
          'tax_rate_class' => sanitize_title( $tax["code"] )
      );
    
       $tax_rate_id = WC_Tax::_insert_tax_rate( $tax_data );
       WC_Tax::_update_tax_rate_postcodes($tax_rate_id, wc_clean('*'));
       WC_Tax::_update_tax_rate_cities($tax_rate_id, wc_clean('*'));
     }
    

  2. $tax_class_name = 'GST-15';
    $tax_class_slug = 'gst-15';
    $tax_rate = 8;
    $tax_rate_name = 'SGST';
    
    // Create tax classs
    $tax_class = WC_Tax::create_tax_class($tax_class_name,$tax_class_slug);
    
    //Invalidate the cache
    
    WC_Cache_Helper::invalidate_cache_group( 'taxes' );
    WC_Cache_Helper::get_transient_version( 'shipping', true );
    
    //Attached the tax_rate to tax_class
    $tax_rate_data = array(
        'tax_rate_country' => '*',
        'tax_rate_state' => '*',
        'tax_rate' => $tax_rate,
        'tax_rate_name' => $tax_rate_name,
        'tax_rate_priority' => 1,
        'tax_rate_compound' => 0,
        'tax_rate_shipping' => 1,
        'tax_rate_order' => 0,
        'tax_rate_class' => $tax_class_slug
    );
    
    $tax_rate_id = WC_Tax::_insert_tax_rate( $tax_rate_data );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search