I
I
Ilya2021-02-17 22:26:36
WordPress
Ilya, 2021-02-17 22:26:36

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);
    }


I need this look:
Dresses for women with buttons

I noticed that if "getLabel" is replaced with "init", then my task is solved. But here's the catch - not for all attributes I need to remove the attribute headers.

As a result, the code should be something like this:
If the attribute = Gender or Fastener, then do not display the attribute headers, otherwise display it.

Can you tell me something I'm not catching up at all.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question