Answer the question
In order to leave comments, you need to log in
How to change Woocommerce title based on selected attributes?
Hello!
Please help me to solve my problem. There is a product page title, which consists of the category name and the selected attributes. Selected attributes are often not displayed logically. I need to supplement the formula - add a condition for which attributes to register both the name of the attribute and the value of the attribute, and for which only the value of the attribute.
I hope I explained it clearly =)
This code makes the title look something like this:
"Category + Attribute title 1 (value 1, value 2, ...)", Attribute title 2 (value 1, value 2, ...)":
Dresses Gender (For women), Closure (With buttons)
protected function formatAttributes($attributes)
{
$attributeStrings = [];
/** @var FilterInterface $attribute */
foreach ($attributes as $attribute) {
$attribute->init();
$items = $attribute->getActiveItems();
$names = array_column($items, 'title');
if (count($names)) {
$attributeStrings[] = $attribute->getLabel() . ' (' . implode(', ', $names) . ')';
}
}
return implode(', ', $attributeStrings);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question