WordPress.org

Ready to get started?Download WordPress

Forums

Importing variables e.g. Price into loads of posts (7 posts)

  1. richarduk
    Member
    Posted 6 years ago #

    Hi,

    I want to set up a blog with a load of products, each with a price. Then I want to update those prices as and when.

    The store I'll use can update prices from a CSV file.

    I've looked for plugins and the like, or updating of WP's SQL database, but nothing really fits. I also considered custom fields, but to update custom fields in the database I'll need to know post IDs and it all has the potential to go horribly wrong, given that I'll be adding and deleting products.

    So ..... I'm thinking of each product having an associated variable, which wouldn't be difficult from the CSV file e.g. in Excel if I have a product ID 12345 I'm sure I can easily churn out a column with that ID and associated price converted into a variable e.g $12345=£5 and then print it out

    Question: Let's say I have a thousand variables. What's the best way of including them in WP?

    As a novice PHP developer I'm thinking along these lines:

    1) Require Once and include the list of variables in the header of each page. Upload the list by FTP

    2) Make an array of the variables and then use foreach - don't know if that's got any advantages. Again, upload by FTP

    3) Create a field (row? column?) in the WP database and upload variables to it, then access appropriate value with a snippet of code. Advantage - MYSQL is a lot faster at the database end than PHP?

    4) Include all the variables in a post, which has the advantage of being easy to paste them in, and then access them from there. If that's possible. (Probably not :-) )

    Before I set off, can anyone point me in the right direction?

    Thanks!

  2. richarduk
    Member
    Posted 6 years ago #

    Any ideas on how I could update prices site-wide on WP?

  3. NC@WP
    Member
    Posted 6 years ago #

    Sounds like you need to write a custom plugin... In your post, you would type something like [showPrice: 1234]; the plugin would replace this with the current price of item number 1234.

  4. richarduk
    Member
    Posted 6 years ago #

    This is what I've come up with - the simple plugin:

    <?php
    /**
     Plugin Name: prices
     Author: Me
     */
    
    add_action('wp_head', 'prices');
    function prices() {
    GLOBAL $productid001;  $productid001 = 5;
    GLOBAL $productid002;  $productid002 = 6;
    GLOBAL $productid003;  $productid003 = 7;
    }
    
    ?>

    It uses the wp_head action hook.

    It works outside the post e.g. `
    <?php
    echo "$productid001";
    echo "$productid002";
    echo "$productid003";
    ?>`

    I've tried using it in the post with phpexec http://priyadi.net/archives/2005/03/02/wordpress-php-exec-plugin/ but without luck so far

    If I can get it to work would this be okay for 1000 or more products?

    Anyone with any better ideas?

    Editted: Could I use an include within the function, so that I could include (say) prices.txt or prices.inc, which would have a list of the prices like this? Just for ease of use, so I could print out a CSV file and upload it?

    GLOBAL $productid001; $productid001 = 5;
    GLOBAL $productid002; $productid002 = 6;
    GLOBAL $productid003; $productid003 = 7;

  5. NC@WP
    Member
    Posted 6 years ago #

    What you wrote won't work as a plugin, simply because your function has no argument (and because you're using the wrong hook). Here's a rough (but tested) alternative draft:

    <?php
    
    /*
    Plugin Name: Show Price
    Plugin URI: http://wordpress.org/support
    Description: Displays product price based on product ID.
    Version: 0.1
    Author: NC(at)WP
    Author URI: http://wordpress.org/support
    
    Usage: [showPrice: ID]
    
    */
    
    function showPrice_install() {
      return true;
    }
    
    function showPrice_display($content = '') {
      ob_start();
      $currencySymbol = '&_#36;'; // U.S. dollar; replace if necessary
      $prices = array(
        1 => 5,
        2 => 6,
        3 => 7
      );
      $matches = array();
      $match = preg_match_all('/\[showPrice:(.*)\]/Ui', $content, $matches);
      foreach ($matches[1] as $k => $match) {
        $match = (int) trim($match);
        if (isset($prices[$match])) {
          $price = $currencySymbol . number_format($prices[$match], 2);
        } else {
          $price = 'N/A';
        }
        $content = str_replace($matches[0][$k], $price, $content);
      }
      ob_end_clean();
      return $content;
    }
    
    // Plugin core
    
    if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
      add_action('init', 'showPrice_install');
    }
    add_filter('the_content', 'showPrice_display');
    
    ?>

    What you can do now is rewrite the population of the $prices array inside the showPrice_display() function, so that the array is populated from a CSV file or a database...

    Be sure to remove the underscore from the $currencySymbol assignment; I put it in just to show that it is an HTML entity rather than the actual dollar sign...

  6. richarduk
    Member
    Posted 6 years ago #

    Ooh ....

    Goody! :-)

    I hoped that making the effort would inspire someone to help me out :-)

    Thank you!

    I'll take a good long look at that.

    Thanks again!

  7. richarduk
    Member
    Posted 6 years ago #

    It works like a dream.

    I don't need to update stock or anything else currently, but I could rename this plugin as Show Stock and use it to show whether or not a product was available.

    Sweet!

    Sitewide changes and a minimalist plugin that looks like it could survive plenty of WP upgrades without needing to be updated.

    Beautiful!

    This is definitely one of those hidden gems that should be more widely known.

    Incidentally, here's how you take data from your store's CSV file and import it as an array into the plugin

    To upgrade the arrays, download the store CSV file

    Select Product ID and then Prices (say) whilst holding down CTRL and dragging down; copy; paste into new workbook
    You now have two columns A B
    Click on the right hand column, insert new column B
    You now have columns A B C

    Put this in B1

    =A1&" => "&C1&","

    Click and drag down the small cross on bottom right of B1 to autofill
    Remove last comma from last item in array
    Click on top of column B to select column; copy; paste into plugin

    The & simply joins the contents together , text has to be inside quotes " "
    http://www.excelforum.com/showthread.php?t=646909
    martindwilson

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags