• Resolved pmathbliss

    (@pmathbliss)


    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)
  • Thread Starter pmathbliss

    (@pmathbliss)

    Here is the path to the change
    woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php

    • This reply was modified 10 months ago by pmathbliss.
    Plugin Support Zubair Zahid (woo-hc)

    (@doublezed2)

    Hello pmathbliss

    Thank you for contacting WooCommerce support.

    I understand that you have pointed out a flaw in the logic about how sale prices and on-sale status are handled and recorded in the WooCommerce system. You are using version 8.4 of WooCommerce, and that the issue has been present for a while.

    You have pointed out that in the current setup, a product is marked as being on sale (wc_product_meta_lookup.onsale flag is set) only if the _sale_price (Sale Price) matches the _price (Final Price). This method means that the sale price can never be saved on the product and set for a future date to schedule a sale.

    Your suggested solution indicates that the correct way to determine if a product is on sale should be based on whether the final price (_price) is less than the regular price (_regular_price). So, a product should only be marked as on sale if its final selling price is lower than its regular price, irrespective of the sale price being equal to the final price.

    Please correct me if missed something.
    Looking forward to your response ??

    Thread Starter pmathbliss

    (@pmathbliss)

    Yes that all sounds correct.

    Hi there @pmathbliss ??

    Feel free to submit a Pull Request (PR) for the suggested edits on lines 2085 to 2110 of this file.

    We appreciate you being an active part of the community ??

    Have a wonderful day!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘On Sale Flag update to product lookup’ is closed to new replies.