Forum

Thread tagged as: Shop

productDynamicFields, Dashboard

I'm using Perch Shop in Runway to sell training courses. I've added some extra fields into the product template including date, venue, duration of each course. When I add products, these fields are stored in the productDynamicFields column of the perch2_shop_products table.

I've also built a simple Dashboard app to present data on orders. I'm using get_rows to run a query on the database and present the results on the Dashboard - and that's all working well.

But I'd like the Dashboard app to be able to access the extra product fields as well - eg to show the orders for courses by date or by venue. My get_rows query is giving me an array with a row for each product. I can include productDynamicFields in my database query, and it gives pairs of names/values like this:

{"duration":"3","date":"2017-01-10","venue":"North Hotel, Anytown","description": ... ... etc etc ... ... }

How would I show just one of my extra fields eg "date" without showing everything else in productDynamicFields? Can anyone point me in the right direction?

Summary information

    Perch Runway: 2.8.32, PHP: 7.0.13, MySQL: 10.0.27-MariaDB, with PDO
    Server OS: Linux, litespeed
    Installed apps: content (2.8.32), assets (2.8.32), categories (2.8.32), perch_blog (5.0), perch_events (1.9.3), perch_forms (1.8.3), perch_gallery (2.8.6), interact_orders (1), perch_shop_orders (1.0.10), perch_shop_products (1.0.10), my_sample (2.1), perch_shop (1.0.10), perch_members (1.5)
    App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_blog', 'perch_gallery', 'perch_forms', 'perch_events', 'perch_members', 'perch_shop', );
    PERCH_LOGINPATH: /cms
    PERCH_PATH: /home/northernfirstaid/public_html/cms
    PERCH_CORE: /home/northernfirstaid/public_html/cms/core
    PERCH_RESFILEPATH: /home/northernfirstaid/public_html/cms/resources
    Image manipulation: GD
    PHP limits: Max upload 100M, Max POST 100M, Memory: 1024M, Total max file upload: 100M
    F1: 2edba60ed1f613d6dd804feb202456a2
    Resource folder writeable: Yes
    HTTP_HOST: northern.test-singlepage.co.uk
    DOCUMENT_ROOT: /home/northernfirstaid/public_html
    REQUEST_URI: /cms/core/settings/diagnostics/
    SCRIPT_NAME: /cms/core/settings/diagnostics/index.php
Mark Melling

Mark Melling 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You probably don't want to query the database directly. Use the API instead.

$Products = new PerchShop_Products($API);
$my_product = $Products->find($productID); 
echo $my_product->custom_field_name();

Fantastic - thanks.

Sorry but I think I need to query the database, because I want to pull in order, product and customer details from different tables. My Dashboard app is basically as follows:

<?php
    // set up database connection
    $API  = new PerchAPI(1.0, 'interact_orders');
    $DB = PerchDB::fetch();

    // function to convert array to a nicely-formatted table
    function build_table($array){
        // start table
        $html = '<table>';
        // header row
        $html .= '<tr>';
        foreach($array[0] as $key=>$value){
            $html .= '<th>' . $key . '</th>';
        }
        $html .= '</tr>';

        // data rows
        foreach( $array as $key=>$value){
            $html .= '<tr>';
            foreach($value as $key2=>$value2){
                $html .= '<td>' . $value2 . '</td>';
            }
            $html .= '</tr>';
        }

        // finish table and return it
        $html .= '</table>';
        return $html;
    }
    // end function

    // orders by sku
    echo "<h3>Orders by training course</h3>";
    $Statuses = new PerchShop_OrderStatuses($API);
    $sql = 'SELECT i.orderID as "ORDER ID", p.sku as "SKU", 
        SUM(i.itemQty) AS "QUANTITY", p.title as TITLE, p.productDynamicFields as "DYNAMIC FIELDS", c.customerFirstName as "CUSTOMER FIRST NAME", c.customerLastName as "CUSTOMER LAST NAME", c.customerEmail as "CUSTOMER EMAIL" 
        FROM '.PERCH_DB_PREFIX.'shop_customers AS c INNER JOIN '.PERCH_DB_PREFIX.'shop_orders AS o USING (customerID) 
        INNER JOIN '.PERCH_DB_PREFIX.'shop_order_items as i USING (orderID) 
        INNER JOIN '.PERCH_DB_PREFIX.'shop_products as p USING (productID) 
        WHERE o.orderStatus IN ('.$DB->implode_for_sql_in($Statuses->get_status_and_above('paid')).') 
        AND o.orderDeleted IS NULL 
        GROUP BY p.sku, i.orderID 
        WITH ROLLUP';

    $result1 = $DB->get_rows($sql);

    if ($result1) {
        echo build_table($result1);
    }

?>  

This presents all the productDynamicFields in one column of the finished table. Is there any way I can present columns in my finished table for my specific extra fields like date, venue and duration? Perhaps by amending the database query or by manipulating the $result1 array? I've been playing around with json decode to no effect - along these lines:

$fields = PerchUtil::json_safe_decode($result1['DYNAMIC FIELDS'], true);

I'm getting this in debug: Undefined index: DYNAMIC FIELDS