Different attribute order for header and rows
-
We noticed that product variations table on our site shows values of wrong attributes for some products. Further investigation uncovered that the table header and table rows have different orders of attributes and the reason for that is that PVT plugin is using methods of different classes to get attribute lists for the header and rows:
\WC_Product_Variable::get_variation_attributesand\WC_Product_Variation::get_variation_attributesrespectively, which are not obliged to return attributes in the same order. In fact they both read the same _product_attributes meta field but the product class uses\WC_Product::set_attributesmethod, which sorts attributes by “position” field at wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php:1165:uasort( $attributes, 'wc_product_attribute_uasort_comparison' );At the same time variation class method
\WC_Product_Variation::set_attributesdoesn’t have such a sorting.This could be a WooCommerce bug actually and we are not sure at the moment, what causes product attributes have the position field not matching the physical order of attributes in the meta field. Saving a product reorders the attributes and the problem goes away. Anyway, we came up with a workaround by reordering attributes displayed in a row via
pvtfw_table_attributesfilter:add_filter(
'pvtfw_table_attributes',
function (array $result,\WC_Product_Variation $variation): array {
/**
* @var \WC_Product_Variable $product
*/
$product = wc_get_product($variation->get_parent_id());
if (!$product) {
throw new \RuntimeException(
"Could not load product #{$variation->get_parent_id()}"
);
}
$sorted = [];
foreach (array_keys($product->get_variation_attributes()) as $key) {
$sorted[$key] = $result[$key];
}
return $sorted;
},
10,
2
);Any chance you could integrate this workaround into next releases?
The topic ‘Different attribute order for header and rows’ is closed to new replies.
