GetFieldById($conditionLine->FieldId); if($field==null) return true; $text=$field->ToText(); $value=Sanitizer::SanitizeString($conditionLine->Value,''); $result = $this->CompareText($text, $value, $conditionLine->Comparison); // For ProductName fields, also check against the parent product name if this is a variation if($conditionLine->FieldId == 'ProductName') { $parentName = $this->GetParentProductName($retriever); if($parentName !== null) { $parentResult = $this->CompareText($parentName, $value, $conditionLine->Comparison); // For positive comparisons: pass if either variation or parent matches if(!$result && in_array($conditionLine->Comparison, ['Equal', 'Contains'])) return $parentResult; // For negative comparisons: fail if parent matches the value if($result && in_array($conditionLine->Comparison, ['NotEqual', 'NotContains'])) return $parentResult; } } return $result; } /** * Gets the parent product name if the current order line item is a variation. * @param $retriever OrderRetriever * @return string|null */ private function GetParentProductName($retriever) { $lineItem = $retriever->GetCurrentOrderLine(); if($lineItem == null) return null; $product = $lineItem->GetProduct(); if($product == null) return null; if($product->is_type('variation')) { $parentId = $product->get_parent_id(); if($parentId > 0) { $parentProduct = wc_get_product($parentId); if($parentProduct != null) return $parentProduct->get_name(); } } return null; } private function CompareText($text, $value, $comparison) { switch ($comparison) { case 'Equal': return $text==$value; case "NotEqual": return $text!=$value; case "IsEmpty": return $text==''; case "IsNotEmpty": return $text!=''; case "Contains": return strpos($text,$value)!==false; case "NotContains": return strpos($text,$value)===false; } return true; } }