• 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_attributes and \WC_Product_Variation::get_variation_attributes respectively, 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_attributes method, 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_attributes doesn’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_attributes filter:

    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?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author WPXtension

    (@wpxteam)

    Hi,

    I hope you are doing well today.

    I have reviewed both arrays in my environment and have attached a screenshot for your reference: https://paste.pics/c1ac819534a61e6cd7821db327a276e4.

    As you can see, the keys and their respective positions are identical. We are using these array keys to display the table header.

    Given that both the keys and positions align, we use the key values to display the table row data, and we have not encountered this issue in the past.

    To help us replicate the problem on our end, could you kindly provide a screencast (you can use https://loom.com) or a set of steps that leads to the issue?

    It’s possible that the problem may be related to transients. You can try clearing the transient by following the instructions in this link: https://paste.pics/5ca83ca8c7a124718d35493232ab5b75.

    Additionally, we have a new version ready for release. Once we are able to replicate the issue, we will include a fix in the upcoming update.

    Please feel free to share any further details, and let me know your thoughts.

    Best regards

    Thread Starter pumka

    (@pumka)

    This happens only if the physical order of array elements in _product_attributes meta field doesn’t match their “position” values, because the “position” is used for extra sorting. At the moment I’m not sure what might cause the order of “position” values to differ because regular product save reorders the elements. Yet something did that, we just don’t know what.

    To reproduce the issue, you can edit the _product_attributes meta of the product directly in the database and adjust the “position” values to not match the physical order of the array elements. They usually go in natural order like 0, 1, 2… but in our case they sometimes differ:

    You can use this tool to edit a serialized array value: https://sciactive.com/phpserialeditor.php.

    • This reply was modified 1 year, 6 months ago by pumka.
    Plugin Author WPXtension

    (@wpxteam)

    Hi,

    Thank you for providing such a detailed explanation. Based on our review, this appears to be an exceptional case.

    Could you confirm whether you are syncing product data from an external source, or if the order is being manually adjusted for a specific reason?

    Under normal circumstances, WooCommerce inserts products in a standard order. For cases like this, we have implemented the pvtfw_table_attributes filter to address the issue.

    If the above code works for you, please use the filter in your current theme’s functions.php. We will continue to monitor the situation for further investigation.

    Once again, thank you for the thorough details you’ve shared.

    Best Regards

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Different attribute order for header and rows’ is closed to new replies.