Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Price for sorting is incorrect #434

Open
2 tasks
axi opened this issue May 22, 2024 · 3 comments · May be fixed by #460
Open
2 tasks

Price for sorting is incorrect #434

axi opened this issue May 22, 2024 · 3 comments · May be fixed by #460
Assignees
Labels
priority: low The issue/PR is low priority—not many people are affected or there’s a workaround, etc. type: bug The issue is a confirmed bug. type: good first issue The issue is a good candidate for the first community contribution/for a newcomer to the team.

Comments

@axi
Copy link

axi commented May 22, 2024

What I expected

I expect the orderby=price-asc order by to be working correctly

What happened instead

Values saved in lookup_table are wrong, resulting order is wrong

Steps to reproduce the issue

Note 1: It looks like this happen only after an "update", not on creation.
Note 2: this is the query I use in the following steps to get the values

SELECT
    pm.post_id,
    pml.min_price,
    pm2.meta_value as '_price',
    pm3.meta_value as '_wc_booking_min_duration'
FROM wp_postmeta pm
    LEFT JOIN wp_wc_product_meta_lookup pml ON pml.product_id = pm.post_id
    LEFT JOIN wp_postmeta pm2 ON pm.post_id = pm2.post_id AND pm2.meta_key = '_price'
    LEFT JOIN wp_postmeta pm3 ON pm.post_id = pm3.post_id AND pm3.meta_key = '_wc_booking_min_duration'
WHERE pm.post_id IN (MY_CREATED_PRODUCT_IDS)
GROUP BY pm.post_id
ORDER BY pm.post_id

Create an accommodation product with the following:

  • Name: product1
  • Minimum number of nights allowed in a booking: 2010
  • Standard room rate: 150
  • hit "publish" button
min_price _price _wc_booking_min_duration
0 150 2010
  • trigger a save with "update" button
min_price _price _wc_booking_min_duration
301500.0000 150 2010

Create another accommodation product with the following:

  • Name: product2
  • Minimum number of nights allowed in a booking: 10
  • Standard room rate: 200
  • hit "publish" button
min_price _price _wc_booking_min_duration
0 200 10
  • trigger a save with "update" button
min_price _price _wc_booking_min_duration
2000.0000 200 10

When using sorting by price asc, we want the cheapest first (Product1) but we have Product2 first.

This is because lookup_table gets a calculated value that take the min number of nights into account (see WC_Bookings_Cost_Calculation::calculated_base_cost())...

WC_Accommodation_Booking_Admin_Panels::save_product_data() tries to solve that l424:

public function save_product_data( $post_id ) {
    ....
    // Set price so filters work - using get_base_cost()
    $product = wc_get_product( $post_id );
    update_post_meta( $post_id, '_price', $product->get_base_cost() );
}

but at that time, price is already set in lookup table.


  • Issue assigned to next milestone.
  • Issue assigned a priority (will be assessed by maintainers).
@axi
Copy link
Author

axi commented May 22, 2024

My quickfix right before the closing of the WC_Accommodation_Booking_Admin_Panels::save_product_data() method

        // Quick Fix
        $wpdb->update(
            $wpdb->prefix . 'wc_product_meta_lookup',
            [
                'min_price' => $product->get_base_cost(),
                'max_price' => $product->get_base_cost(),
            ],
            [
                'product_id' => $post_id,
            ],
            '%d'
        );

@vikrampm1 vikrampm1 added priority: low The issue/PR is low priority—not many people are affected or there’s a workaround, etc. type: bug The issue is a confirmed bug. labels May 27, 2024
@jeffpaul jeffpaul added the type: good first issue The issue is a good candidate for the first community contribution/for a newcomer to the team. label May 28, 2024
@axi
Copy link
Author

axi commented Jul 18, 2024

v1.2.7 moved the WC_Accommodation_Booking_Admin_Panels::save_product_data() method to WC_Accommodation_Booking_REST_And_Admin::save_product_data()

@axi
Copy link
Author

axi commented Aug 28, 2024

I was trying to understand why this fix was needed a realized that the WC_Accommodation_Booking_REST_And_Admin::save_product_data() is kind of a fix that recompile & overwrite values that have been set in WC_Product_Booking_Data_Store_CPT...
That makes my quickfix (which does the same, fixing a previously set value) a fix :-)

Can anyone have a look ?

PS: I did update the first comment with more data

@axi axi linked a pull request Aug 28, 2024 that will close this issue
4 tasks
@vikrampm1 vikrampm1 added this to the Future Release milestone Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: low The issue/PR is low priority—not many people are affected or there’s a workaround, etc. type: bug The issue is a confirmed bug. type: good first issue The issue is a good candidate for the first community contribution/for a newcomer to the team.
Projects
None yet
3 participants