We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45118
    • 123 Posts
    I'm using this snippet to display product information on a products page. (A thank you to Bob Ray http://bobsguides.com/custom-db-tables.html, this page really helped me to get this started.) It works nicely.
    <?php
    $path = MODX_CORE_PATH . 'components/aeiproducts/';
    $result = $modx->addPackage('aeiproducts',$path .
        'model/','aei_');
    
    if (! $result) {
        return 'failed to add package';
    }
    
    $products = $modx->getCollection('Product');
    foreach($products as $product) {
        $fields = $product->toArray();
        $output .= $modx->getChunk('ShowProds', $fields);
    }
    
    return $output;
    
    ?>

    Now I need to convert some of the db data before displaying it. The product dimensions are in decimal format in the db and need to be converted in a inch fractional notation. Ex: 3.25" becomes 3-1/4".
    To do this I'm running a snippet within the snippet by adding this:
    $modx->runSnippet('FeetInch');
    $fi = new FeetInch($modx,$scriptProperties);

    To see if it works I added this:
    $output .= $fi->dec2inch(3.25) .'<br/>';

    It displays 3-1/4". It works!
    Next I changed the foreach part like so:
    $products = $modx->getCollection('Product');
    foreach($products as $product) {
        $width = $product->get('width');
        $nwWidth = $fi->dec2inch($width);
        $output .= $nwWidth .'<br/>'; // Testing if the conversion works
        $fields = $product->toArray();
        $output .= $modx->getChunk('ShowProds', $fields);
    }

    The $nwWidth output is displaying the whole list of converted widths. Nice!!

    What I'm NOT seem to be able to do is getting the converted width (nwWidth) to display through the chunk.

    Any ideas? Thanks for your help.

    This question has been answered by opengeek. See the first response.

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I would create a custom Output Modifier for this. Then you could just have something like this in your chunk
      [[+field1:FeetInch]]


        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 45118
        • 123 Posts
        Quote from: sottwell at Sep 16, 2013, 10:10 AM
        I would create a custom Output Modifier for this. Then you could just have something like this in your chunk
        [[+field1:FeetInch]]


        I checked the link but can't figure out how to make a custom Output Modifier. Could you help me getting started?
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          It's just a snippet. You should be able to use your FeetInch snippet, as long as it accepts the input and returns the output.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 45118
            • 123 Posts
            The FeetInch snippet only loads a class, feetinch.class.php:
            <?php
            $feetinch_base = $modx->getOption('feetinch.core_path',$scriptProperties,$modx->getOption('core_path').'components/feetinch/');
            
            include_once $feetinch_base.'feetinch.class.php';
            if (!$modx->loadClass('FeetInch',$feetinch_base,true,true)) {
                return 'error: FeetInch class not found';
            }
            
            return '';

            Maybe I should add the following to this snippet instead of having it in the ShowProds snippet?
            $fi = new FeetInch($modx,$scriptProperties);

            Should I then also add something like this?:
            $output = $fi->dec2inch()
            • discuss.answer
              • 22303 MODX Staff
              • 10,725 Posts
              All you need is to include your calculated value in the properties you are passing to getChunk:
                      $output .= $modx->getChunk('ShowProds', array_merge($fields, array('nwWidth' => $nwWidth)));
                • 45118
                • 123 Posts
                Quote from: opengeek at Sep 16, 2013, 04:02 PM
                All you need is to include your calculated value in the properties you are passing to getChunk:
                        $output .= $modx->getChunk('ShowProds', array_merge($fields, array('nwWidth' => $nwWidth)));
                That works perfectly smiley
                Thank you very much!