get_operators_for_new_order();
} elseif($trigger_type == 'low_stock' || $trigger_type == 'out_of_stock' || $trigger_type == 'backorder') {
return $this->get_operators_for_low_stock();
}
return array();
}
public function get_operators_for_new_order() {
return array(
'in' => __('Contains product from categories', 'advance-notification-for-woocommerce'),
'not_in' => __('Does not contain product from categories', 'advance-notification-for-woocommerce'),
);
}
public function get_operators_for_low_stock() {
return array(
'in' => __('Product from this category', 'advance-notification-for-woocommerce'),
'not_in' => __('Product not from this category', 'advance-notification-for-woocommerce'),
);
}
/**
* Get HTML for value field
*
* @param string $html Current HTML.
* @param string $condition_id Condition ID.
* @param string $unused_param Unused parameter.
* @param string $current_value Current value.
* @return string
*/
public function get_value_field($html, $condition_id, $unused_param, $current_value) {
$categories = get_terms(array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
));
ob_start();
?>
is_match_for_new_order($return, $data, $trigger_type, $operator, $value);
}
if($trigger_type == 'low_stock' || $trigger_type == 'out_of_stock' || $trigger_type == 'backorder') {
return $this->is_match_for_low_stock($return, $data, $trigger_type, $operator, $value);
}
return false;
}
public function is_match_for_low_stock($return, $data, $trigger_type, $operator, $value) {
// For low stock trigger, we assume the value is a product ID or an array of product IDs
if (empty($value)) {
return false;
}
// Ensure we have an array of product IDs
$triggering_low_stock_for_product = $data['product'] && is_object($data['product']) ? $data['product']->get_id() : null;
if( !$triggering_low_stock_for_product ) {
return false;
}
$product_categories = wc_get_product_term_ids( $triggering_low_stock_for_product, 'product_cat');
if (empty($product_categories)) {
return false;
}
$category_ids = !empty($value) && is_array($value) ? $value : array();
if(empty($category_ids)) return false;
$intersect = array_intersect($product_categories, $category_ids);
// Check if the product belongs to the specified categories
switch ($operator) {
case 'in':
return count($intersect) > 0 ? true : false;
case 'not_in':
return count($intersect) == 0 ? true : false;
default:
return false;
}
}
public function is_match_for_new_order($return, $data, $trigger_type, $operator, $value) {
$order = isset($data['order']) ? $data['order'] : null;
if (!$order || !is_a($order, 'WC_Order') || empty($value)) {
return false;
}
$items = $order->get_items();
if (empty($items)) {
return false;
}
// Always handle values as comma-separated list
$category_ids = !empty($value) && is_array($value) ? $value : array();
$found_categories = array();
foreach ($items as $item) {
$product_id = $item->get_product_id();
$product_categories = wc_get_product_term_ids($product_id, 'product_cat');
// Check each product category against our list of categories
foreach ($product_categories as $product_cat) {
if (in_array($product_cat, $category_ids)) {
$found_categories[] = $product_cat;
}
}
}
$contains_category = !empty($found_categories);
switch ($operator) {
case 'in':
return $contains_category;
case 'not_in':
return !$contains_category;
default:
return false;
}
}
}