Modify DataTables Options

Filter & Hooks Viewed: 1340

If you want to modify the settings of a datatable for a specific product you can use the “woocommerce_variations_table_datatables_options” filter like below.

In this example for the product ID 581 we disable the searching, filtering and buttons functionality for the variations table.

function datatable_options($options) {
	global $product;
	$product_id = $product->get_id();

	if($product_id == 581) {
		$options['searching'] = false;
		$options['filtering'] = false;
		$options['buttons'] = array();
	}
    return $options;
}
add_filter( 'woocommerce_variations_table_datatables_options', 'datatable_options', 10, 1 );

Reference of Options:

array(12) {
  ["paging"]=>
  bool(false)
  ["ordering"]=>
  bool(true)
  ["info"]=>
  bool(false)
  ["stateSave"]=>
  bool(true)
  ["searching"]=>
  bool(true)
  ["scrollY"]=>
  bool(false)
  ["scrollCollapse"]=>
  bool(true)
  ["responsive"]=>
  bool(true)
  ["filtering"]=>
  string(1) "1"
  ["sDom"]=>
  string(17) "W<"clear">Blfrtip"
  ["language"]=>
  array(1) {
    ["url"]=>
    string(59) "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/English.json"
  }
  ["buttons"]=>
  array(5) {
    [0]=>
    array(2) {
      ["extend"]=>
      string(5) "print"
      ["exportOptions"]=>
      array(1) {
        ["stripHtml"]=>
        bool(false)
      }
    }
    [1]=>
    array(2) {
      ["extend"]=>
      string(9) "copyHtml5"
      ["exportOptions"]=>
      array(1) {
        ["stripHtml"]=>
        bool(false)
      }
    }
    [2]=>
    array(2) {
      ["extend"]=>
      string(10) "excelHtml5"
      ["exportOptions"]=>
      array(1) {
        ["stripHtml"]=>
        bool(true)
      }
    }
    [3]=>
    array(2) {
      ["extend"]=>
      string(8) "csvHtml5"
      ["exportOptions"]=>
      array(1) {
        ["stripHtml"]=>
        bool(true)
      }
    }
    [4]=>
    array(2) {
      ["extend"]=>
      string(8) "pdfHtml5"
      ["exportOptions"]=>
      array(1) {
        ["stripHtml"]=>
        bool(true)
      }
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *