Description
openedon 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).