Performance improvements
-
Hello,
I’m optimizing a site and identified some slow code in this plugin, which could be improved. Would you be able to optimized the code below?
The inefficient code is in yith-pre-order-for-woocommerce/includes/class-yith-pre-order.php file on line #267 with the below function:
$product = wc_get_product( $product );
This is called within the following function:
public static function is_pre_order_product( $product ) { $return = false; // Use the WC product factory function to be sure $product is a WC_Product object. $product = wc_get_product( $product ); if ( $product instanceof WC_Product ) { if ( $product->is_type( 'simple' ) || $product->is_type( 'variation' ) ) { if ( 'yes' === $product->get_meta( '_ywpo_preorder' ) ) { $return = true; } } } return apply_filters( 'ywpo_is_pre_order_product', $return, $product ); }
The product object is already available (I don’t see how it needs to be retrieved via wc_get_product with the same object data), perhaps ID was originally used here to retrieve the object data? From a stack trace this was called from get_price_html in woocommerce, so it should be safe to assume it is a product.
Removing the wc_get_product line saves .5s on a shop page here and .1s+ on the product page.
Would it be possible for this line to be removed? The instanceof check could still be kept.
Thanks for your help.
- The topic ‘Performance improvements’ is closed to new replies.