On Sale Flag update to product lookup
-
I’m on 8.4 with this code, but its been around for a while.
The wc_product_meta_lookup.onsale flag on sets products on sale when the _sale_price(Sale Price) matches the _price(Final Price).
This is not exactly right. This means the sale price can never be saved on the product and set the date in the future to schedule a sale. A product is on sale when the final price is less than _regular_price(Regular Price).
Here is the update to WC_Product_Data_Store_CPT::get_data_for_lookup_table :/** * Get data to save to a lookup table. * * @since 3.6.0 * @param int $id ID of object to update. * @param string $table Lookup table name. * @return array */ protected function get_data_for_lookup_table( $id, $table ) { if ( 'wc_product_meta_lookup' === $table ) { $price_meta = (array) get_post_meta( $id, '_price', false ); $manage_stock = get_post_meta( $id, '_manage_stock', true ); $stock = 'yes' === $manage_stock ? wc_stock_amount( get_post_meta( $id, '_stock', true ) ) : null; $regular_price = wc_format_decimal( get_post_meta( $id, '_regular_price', true ) ); $price = wc_format_decimal( get_post_meta( $id, '_price', true ) ); $sale_price = wc_format_decimal( get_post_meta( $id, '_sale_price', true ) ); return array( 'product_id' => absint( $id ), 'sku' => get_post_meta( $id, '_sku', true ), 'virtual' => 'yes' === get_post_meta( $id, '_virtual', true ) ? 1 : 0, 'downloadable' => 'yes' === get_post_meta( $id, '_downloadable', true ) ? 1 : 0, 'min_price' => reset( $price_meta ), 'max_price' => end( $price_meta ), 'onsale' => $regular_price && floatval($regular_price) > floatval($price) ? 1 : 0, 'stock_quantity' => $stock, 'stock_status' => get_post_meta( $id, '_stock_status', true ), 'rating_count' => array_sum( array_map( 'intval', (array) get_post_meta( $id, '_wc_rating_count', true ) ) ), 'average_rating' => get_post_meta( $id, '_wc_average_rating', true ), 'total_sales' => get_post_meta( $id, 'total_sales', true ), 'tax_status' => get_post_meta( $id, '_tax_status', true ), 'tax_class' => get_post_meta( $id, '_tax_class', true ), ); } return array(); }
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘On Sale Flag update to product lookup’ is closed to new replies.