maker->type === "order-products" ? $this->getOrderProductAttribute($post, $field['value'], $termMeta) : $this->getWoocommerceProductAttributeValueByName($post, $field['value'], $termMeta); } public function getName($field) { $termMeta = !empty($field['term_meta']) ? $field['term_meta'] : null; return $termMeta === null ? ucfirst($field['value']) : ""; } protected function getOrderProductAttribute($post, $taxonomyName, $termMeta = null, $isGlobal = true) { if (!isset($post->orderItem)) { return ''; } $metaKey = $isGlobal ? 'pa_' . $taxonomyName : strtolower($taxonomyName); if ($post->orderItem->meta_exists($metaKey)) { $value = $post->orderItem->get_meta($metaKey); } else { $value = $isGlobal ? $this->getWoocommerceProductAttributeValueByName($post, $taxonomyName, $termMeta) : $this->getWoocommerceCustomAttributeValueByName($post, $taxonomyName); } return !empty($value) ? $value : ''; } protected function getWoocommerceCustomAttributeValueByName($post, $taxonomyName) { $localAttributeValue = ''; $attributes = array(); if ('product' === $post->post_type) { $attributes = get_post_meta($post->ID, '_product_attributes', true); } elseif ('product_variation' === $post->post_type) { $attributes = get_post_meta($post->post_parent, '_product_attributes', true); } if (!empty($attributes)) { $taxonomy = null; foreach ($attributes as $slug => $attribute) { if ($taxonomyName === $attribute['name']) { $taxonomy = $slug; break; } } if (null === $taxonomy) { foreach ($attributes as $slug => $attribute) { if ($taxonomyName === $slug) { $taxonomy = $slug; break; } } } if (null !== $taxonomy) { if ('product' === $post->post_type) { $localAttributeValue = $this->getWcAttributesComaSeparatedString($attributes[$taxonomy]['value']); } if ('product_variation' === $post->post_type) { $localAttributeValue = get_post_meta($post->ID, "attribute_{$taxonomy}", true) ?: $this->getWcAttributesComaSeparatedString($attributes[$taxonomy]['value']); } } } return $localAttributeValue; } protected function getWoocommerceProductAttributeValueByName($post, $taxonomyName, $termMeta = null) { global $wpdb; $attrPriority = UserSettings::getOption('attrPriority', 'one'); $globalAttributeValue = $this->getWoocommerceProductTerms($post, $taxonomyName, $termMeta); if (empty($globalAttributeValue)) { $wc_attribute_taxonomy = $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE `attribute_label` = %s", $taxonomyName )); if (null !== $wc_attribute_taxonomy) { $taxonomy_slag = $wc_attribute_taxonomy->attribute_name; $globalAttributeValue = $this->getWoocommerceProductTerms($post, $taxonomy_slag, $termMeta); } else { $globalAttributeValue = null; } } if ('product' === $post->post_type) { $attributes = get_post_meta($post->ID, '_product_attributes', true); } if ('product_variation' === $post->post_type) { $attributes = get_post_meta($post->post_parent, '_product_attributes', true); } if (!empty($attributes)) { $taxonomy = null; foreach ($attributes as $slug => $attribute) { if ($taxonomyName === $attribute['name']) { $taxonomy = $slug; break; } } foreach ($attributes as $slug => $attribute) { if ($taxonomyName === $slug) { $taxonomy = $slug; break; } } if (null !== $taxonomy) { if ('product' === $post->post_type) { $localAttributeValue = $this->getWcAttributesComaSeparatedString($attributes[$taxonomy]['value']); } if ('product_variation' === $post->post_type) { $localAttributeValue = get_post_meta($post->ID, "attribute_{$taxonomy}", true) ?: $this->getWcAttributesComaSeparatedString($attributes[$taxonomy]['value']); } } else { $localAttributeValue = null; } } else { $localAttributeValue = null; } if ('global' === $attrPriority) { $attrValue = !empty($globalAttributeValue) ? $globalAttributeValue : ''; } elseif ('local' === $attrPriority) { $attrValue = !empty($localAttributeValue) ? $localAttributeValue : ''; } else { if (!empty($localAttributeValue)) { $attrValue = $localAttributeValue; } elseif (!empty($globalAttributeValue)) { $attrValue = $globalAttributeValue; } else { $attrValue = ""; } } return $attrValue; } protected function getWcAttributesComaSeparatedString($attributeValuesString) { $values = explode('|', $attributeValuesString); $values = array_map('trim', $values); return implode(', ', $values); } protected function getWoocommerceProductTerms($post, $taxonomy, $termMeta = null) { if ('product' === $post->post_type) { return $this->termsObjectsToString(get_the_terms($post, 'pa_' . $taxonomy), $termMeta); } if ('product_variation' === $post->post_type) { $term = get_term_by( 'slug', get_post_meta($post->ID, 'attribute_pa_' . $taxonomy, true), 'pa_' . $taxonomy ); return $term ? (empty($termMeta) ? $term->name : get_term_meta($term->term_id, $termMeta, true)) : ''; } } protected function termsObjectsToString($terms, $termMeta = null) { if ($terms && !is_wp_error($terms)) { $terms = array_map(function ($term) use ($termMeta) { return empty($termMeta) ? $term->name : get_term_meta($term->term_id, $termMeta, true); }, $terms); return implode(', ', $terms); } return null; } }