F
F
foxayb2020-06-10 16:19:40
WooCommerce
foxayb, 2020-06-10 16:19:40

How to optimize wc_get_attribute_taxonomies function?

wc_get_attribute_taxonomies function:

spoiler
// /wp-content/plugins/woocommerce/includes/wc-attribute-functions.php
function wc_get_attribute_taxonomies() {

        $prefix      = WC_Cache_Helper::get_cache_prefix( 'woocommerce-attributes' );
        $cache_key   = $prefix . 'attributes';
        $cache_value = wp_cache_get( $cache_key, 'woocommerce-attributes' );

        if ( $cache_value ) {
                return $cache_value;
        }

        $raw_attribute_taxonomies = get_transient( 'wc_attribute_taxonomies' );

        if ( false === $raw_attribute_taxonomies ) {
                global $wpdb;

                $raw_attribute_taxonomies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name != '' ORDER BY attribute_name ASC;" );

                set_transient( 'wc_attribute_taxonomies', $raw_attribute_taxonomies );
        }

        /**
         * Filter attribute taxonomies.
         *
         * @param array $attribute_taxonomies Results of the DB query. Each taxonomy is an object.
         */
        $raw_attribute_taxonomies = (array) array_filter( apply_filters( 'woocommerce_attribute_taxonomies', $raw_attribute_taxonomies ) );

        // Index by ID for easer lookups.
        $attribute_taxonomies = array();

        foreach ( $raw_attribute_taxonomies as $result ) {
                $attribute_taxonomies[ 'id:' . $result->attribute_id ] = $result;
        }

        wp_cache_set( $cache_key, $attribute_taxonomies, 'woocommerce-attributes' );

        return $attribute_taxonomies;

}

during its operation, a large selection of data from the database occurs, which greatly affects the speed of loading the site.
What can be done about it, how to optimize it? I have a large number of attributes, somewhere around 2000.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pychev Anatoly, 2020-06-10
@pton

As an option, enable object caching on the hosting (Redis or MemCache). Then there will be only one, the first, request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question