• Anonymous User 20447015

    (@anonymized-20447015)


    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Anonymous User 20447015

    (@anonymized-20447015)

    Also found wc_get_product on the following lines adding about 1s to the shop page:

    wc_get_product() yith-pre-order-for-woocommerce/includes/functions-pre-order.php:37
    wc_get_product() yith-pre-order-for-woocommerce/includes/class-yith-pre-order.php:267
    • This reply was modified 2 years, 4 months ago by Anonymous User 20447015.

    Hi @wpcharged
    Thank you for your feedback! We will give it a check.

    Thread Starter Anonymous User 20447015

    (@anonymized-20447015)

    There was one issue I found with these optimizations when editing a product and the wc_get_product() function was removed in yith-pre-order-for-woocommerce/includes/class-yith-pre-order.php on line #267 a fatal error would be returned as the ID was passed to this function from class-yith-pre-order-edit-product-page.php:248

    The product object is already retrieved within the class-yith-pre-order-edit-product-page.php file so changing the post_id from:

    $pre_order = ywpo_get_pre_order( $post_id );

    to:

    $pre_order = ywpo_get_pre_order( $product );

    Will fix this.

    Hopefully the developers can go through and remove the unnecessary wc_get_product look ups as that will really help with the load time and performance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Performance improvements’ is closed to new replies.