如何编辑此功能以提高Wordpress的性能?

时间:2017-10-16 18:41:57

标签: php wordpress

我正在使用名为" invogue"的wordpress / woocommerce主题。这个主题包含一些使用AJAX调用的ajax函数,但它们超级慢,每次5秒。

有没有办法编辑这个功能来加快速度?

以下内容获取购物车商品

#GET CART NAV DATA
public function htheme_get_nav_cart_data(){

    #GLOBALS
    global $wpdb, $hmenu_helper, $woocommerce;

    if ( class_exists( 'WooCommerce' ) ) {

        #VARAIBLES
        $cart_count = $woocommerce->cart->cart_contents_count;
        $cart_link = esc_url(get_permalink(get_option('woocommerce_cart_page_id')));
        $cart = $woocommerce->cart->get_cart();
        $total_quantity = 0;

        #ARRAY OF ITEMS
        $cart_items = [];

        $cart_count = 1;

        #FOREACH CART ITEM
        foreach($cart as $item){

            $image = wp_get_attachment_image_src ( get_post_thumbnail_id ( $item['product_id'] ), 'full' );

            $cart_items[] = array(
                'id' => $item['product_id'],
                'title' => esc_html($item['data']->post->post_title),
                'quantity' => $item['quantity'],
                'total' => $item['line_subtotal'],
                'link' => get_permalink($item['product_id']),
                'price' => wc_get_price_decimals(),
                'image' => $image[0],
                'price_html' => $this->htheme_return_price_html($item['product_id']),
                'qty' => esc_html__('Qty', 'invogue'),
            );

            $total_quantity += $item['quantity'];

            $cart_count++;

        }

        #ECHO JSON
        echo json_encode(array(
            'status' => 'active',
            'count' => $total_quantity,
            'url' => $cart_link,
            'cart' => $cart_items,              
            'symbol' => get_woocommerce_currency_symbol(get_option('woocommerce_currency')),
            'total' => $woocommerce->cart->get_cart_total(),
        ));
        exit();

    } else {

        #NOT ACTIVE
        echo json_encode(array(
            'status' => 'not'
        ));
        exit();

    }

}

以下内容获取了愿望清单

public function htheme_get_nav_wishlist_data(){

    #GLOBALS
    global $wpdb, $hmenu_helper, $woocommerce;

    if ( class_exists( 'WooCommerce' ) ) {

        #GET USER ID
        $user_ID = get_current_user_id();

        #GET USER WISHLIST
        $wishlist = esc_attr( get_the_author_meta( 'user_wishlist', $user_ID ) );

        #ARGS
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'offset' => 0,
            'include' => explode(',', $wishlist)
        );

        #PRODUCTS
        $products = get_posts($args);

        #ECHO JSON
        echo json_encode($products);
        exit();

    } else {

        #NOT ACTIVE
        echo json_encode(array(
            'status' => 'not'
        ));

        exit();

    }

}

0 个答案:

没有答案