Jump to content

Data feeds slow. Not sure how to use Hooks


Recommended Posts

Hi all,

I'm trying to display product pricing on my Wordpress website via WHMCS using data feeds, but because there's about 20 prices being displayed, it's incredibly slow.

I've seen it mentioned that one should rather use hooks, but I'm really not sure how to implement this. 

Could someone please point me in the right direction?

Thank you!

Link to comment
Share on other sites

  • 3 weeks later...
On 8/1/2024 at 9:51 PM, Grant S said:

Anyone? Am I missing something particularly obvious here?

Should I somehow be dynamically pulling the pricing from a single file elsewhere?

Thx

UNTESTED

Copy paste the below into an editor save it as whmcsdatafeed.php upload to your wordpress theme directory

change the yourdomain.com/whmcs to the required domain details

 

<?php
// Set the cache file path and cache time (in seconds)
$cache_file = 'whmcs_cache.json';
$cache_time = 3600; // 1 hour

// Check if the cache file exists and is still valid
if (file_exists($cache_file) && (time() - filemtime($cache_file) < $cache_time)) {
    // Read data from the cache file
    $data = json_decode(file_get_contents($cache_file), true);
} else {
    // Fetch data from WHMCS
    $product_ids = [1, 2, 3]; // Add your product IDs here
    $data = [];

    foreach ($product_ids as $pid) {
        $description = file_get_contents("https://yourdomain.com/whmcs/feeds/productsinfo.php?pid=$pid&get=description");
        $price = file_get_contents("https://yourdomain.com/whmcs/feeds/productsinfo.php?pid=$pid&get=price&billingcycle=monthly");
        $data[] = ['description' => $description, 'price' => $price];
    }

    // Save data to the cache file
    file_put_contents($cache_file, json_encode($data));
}

// Output the data
foreach ($data as $product) {
    echo '<div class="product">';
    echo '<p>' . $product['description'] . '</p>';
    echo '<p>Price: ' . $product['price'] . '</p>';
    echo '</div>';
}
?>

Add this to the page you want to show the data feeds on change file destination

 

<?php include 'path/to/your/php/file.php'; ?>

This script will fetch the product descriptions and pricing from WHMCS, cache the data for one hour, and display it on your WordPress site. This approach reduces the number of requests to WHMCS, speeding up the loading time

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated