Last active
May 24, 2021 18:28
-
-
Save pacotole/b63d70aefaf3185b4063ad80cf8bd955 to your computer and use it in GitHub Desktop.
Joinchat Agents filter examples of available agents by product category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Array filter items that field contains $value | |
function array_filter_field_contains( $array, $field, $values ) { | |
return array_filter( | |
$array, | |
function( $item ) use ( $field, $values ) { | |
foreach ( (array) $values as $value ) { | |
if ( false !== stripos( $item[ $field ], $value ) ) { | |
return true; | |
} | |
} | |
return false; | |
} | |
); | |
} | |
/** | |
* Add all 'joinchat_agents_list' filters that you need: | |
* | |
* add_filter( 'joinchat_agents_list', function( $agents, $all_agents, $obj ) { | |
* | |
* $categories = 'my-category'; // Category slug or id (or array of categories) | |
* $field = 'name'; // Agent field to check ('name', 'role' or 'phone') | |
* $values = 'Jhon'; // Value to compare (string or array of strings) | |
* | |
* if ( is_product() && has_term( $categories, 'product_cat', $obj ) ) { | |
* return array_filter_field_contains( $all_agents, $field, $values ); | |
* } | |
* | |
* return $agents; | |
* }, 10, 3 ); | |
*/ | |
// Ejemplo 1: Lámparas Quirúrgicas las atiende Arturo | |
add_filter( 'joinchat_agents_list', function( $agents, $all_agents, $obj ) { | |
$categories = 'lamparas-quirurgicas'; // Category slug or id (or array of categories) | |
$field = 'name'; // Agent field to check ('name', 'role' or 'phone') | |
$values = 'arturo'; // Value to compare (string or array of strings) | |
if ( is_product() && has_term( $categories, 'product_cat', $obj ) ) { | |
return array_filter_field_contains( $all_agents, $field, $values ); | |
} | |
return $agents; | |
}, 10, 3 ); | |
// Ejemplo 2: Camas Hospitalarias y Mesas Quirúrgicas las atienden Arturo y Adriana | |
add_filter( 'joinchat_agents_list', function( $agents, $all_agents, $obj ) { | |
$categories = ['camas-hospitalarias', 'mesas-quirurgicas']; // Category slug or id (or array of categories) | |
$field = 'name'; // Agent field to check ('name', 'role' or 'phone') | |
$values = ['arturo', 'adriana']; // Value to compare (string or array of strings) | |
if ( is_product() && has_term( $categories, 'product_cat', $obj ) ) { | |
return array_filter_field_contains( $all_agents, $field, $values ); | |
} | |
return $agents; | |
}, 10, 3 ); | |
// Ejemplo 3: Refacciones Originales las atiende Pepe | |
add_filter( 'joinchat_agents_list', function( $agents, $all_agents, $obj ) { | |
if ( is_product() && has_term( 'refacciones-originales', 'product_cat', $obj ) ) { | |
return array_filter_field_contains( $all_agents, 'name', 'Pepe' ); | |
} | |
return $agents; | |
}, 10, 3 ); | |
// Ejemplo 4: todos los ejemplos anteriores en un solo "filter" | |
// Contiene un array con la lista de categorías y los agentes que le corresponde | |
add_filter( 'joinchat_agents_list', function( $agents, $all_agents, $obj ) { | |
// Lista de categorías y agentes activos | |
$categories_agents = [ | |
'lamparas-quirurgicas' => 'Arturo', | |
'camas-hospitalarias' => ['arturo', 'adriana'], | |
'mesas-quirurgicas' => ['arturo', 'adriana'], | |
'refacciones-originales' => 'pepe', | |
]; | |
if ( is_product() ) { | |
foreach ( $categories_agents as $categories => $names ) { | |
if ( has_term( $categories, 'product_cat', $obj ) ) { | |
return array_filter_field_contains( $all_agents, 'name', $names ); | |
} | |
} | |
} | |
return $agents; | |
}, 10, 3 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment