Recent Posts:

WordPress – Admin Columns Pro – Make any column editable (Brand Editable)

27 Nov WordPress – Admin Columns Pro – Make any column editable (Brand Editable)

We have developed an element of this plugin to allow the brand for a product to be editable on the product listing page.

And actually you can add more than one brand per product with this code.

The code is :

function get_product_brands() {

$brands = get_terms( ‘yith_product_brand’, array(‘orderby’ => ‘name’) );
$brands_array = array();
foreach ( $brands as $key => $brand ) {

$brand_id = $brand->term_id;
$brand_name = $brand->name;
$brands_array[$brand_id] = $brand_name;

}
return $brands_array;

}

// Set brand column as editable
add_filter( ‘cac/editable/is_column_editable/column=taxonomy-yith_product_brand’, ‘__return_true’ );

// Set the editable properties
function my_plugin_column_editable_settings( $editable_data, $model) {

$editable_data[‘taxonomy-yith_product_brand’][‘default_column’] = true;
$editable_data[‘taxonomy-yith_product_brand’][‘type’] = ‘select2_dropdown’;
$editable_data[‘taxonomy-yith_product_brand’][‘multiple’] = true;
$editable_data[‘taxonomy-yith_product_brand’][‘advanced_dropdown’] = true;
$editable[‘ajax_populate’] = true;
$editable_data[‘taxonomy-yith_product_brand’][‘options’] = get_product_brands();
return $editable_data;

}
add_filter( ‘cac/editable/editables_data’, ‘my_plugin_column_editable_settings’, 10, 2 );

// Retrieve the value that will be used for editing
function my_plugin_column_get_value( $value, $column, $id, $model ) {

$terms = get_the_terms( $id , ‘yith_product_brand’ );
if(sizeof($terms) > 0) {

foreach($terms AS $term) {

$value[] = $term->term_id;

}

}
return $value;

}
add_filter( ‘cac/editable/column_value/column=taxonomy-yith_product_brand’, ‘my_plugin_column_get_value’, 10, 4 );

// Store the editable column value to the database
function my_plugin_column_save( $result, $column, $id, $value, $model ) {

// Store the value that has been entered with inline-edit
global $wpdb;
$brands_query = “SELECT term_taxonomy_id FROM wp_term_taxonomy WHERE taxonomy = ‘yith_product_brand'”;
$brands = $wpdb->get_results($brands_query);
// Delete current brand associations for product
foreach($brands AS $brand) {

$brand_query = “DELETE FROM wp_term_relationships WHERE object_id = ” . $id . ”
AND term_taxonomy_id = ” . $brand->term_taxonomy_id;
$wpdb->query($brand_query);

}
// Add all selected brand associations for product
foreach($value AS $value_single) {

wp_set_post_terms( $id, array( $value_single ), ‘yith_product_brand’, true );

}

}
add_filter( ‘cac/editable/column_save/column=taxonomy-yith_product_brand’, ‘my_plugin_column_save’, 10, 5 );

// Update brand names on product listing page against relevant amended product
function my_plugin_column_value_ajax( $value, $column, $id) {

if($column->properties->type == ‘taxonomy-yith_product_brand’) {

$terms = get_the_terms( $id , ‘yith_product_brand’ );
$values_temp = ”;
if(sizeof($terms) > 0) {

foreach($terms AS $term) {

$values_temp .= $term->name . ‘,<br>’;

}

}

$value = rtrim($values_temp, “,<br>”);

}

return $value;

}
add_filter( ‘cac/editable/after_ajax_column_save/value’, ‘my_plugin_column_value_ajax’, 10, 4 );

 

The code is provided without warranty. Before making any changes backup your website files and database (twice).

google_pluslinkedinmailgoogle_pluslinkedinmail
Paramount Bod
No 1 Bod