We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26310
    • 130 Posts
    Hello,

    I have a simple snippet that takes three variables. This is for a simple catalog of products. If the variable ’onsale’ is checked, the snippet takes the items price (TV: price), strikes it out and writes the sale price (TV: saleprice).

    This snippet works great on the product pages but I wanted to include it in ditto where I have my category landing pages. The template variables needed for the snippet are being read correctly and on their own appear just fine within the ditto call. The snippet placeholder ([+cost+]) is also showing up. The snippet just doesn’t recognize if the onsale variable is checked or not and doesn’t show the sale price if it is. Here’s the snippet code:
    <?php
    /*
    This snippet is used to check whether the 'chksale' template variable 
    has been checked.  It then displays the appropiate price.
    
    This snippet takes 3 parameters
    
    &orprice=`[*price*]` //TV price - Original price of item
    &saleprice=`[*saleprice*]`  //Sale Price of the item; TV saleprice
    &onsale=`[*chksale*]` //TV chksale determines whether it's on sale.
    
    Call:
    [!salecost? &orprice=`[*price*]` &saleprice=`[*saleprice*]`  &onsale=`[*chksale*]`!]
    
    Produces two placeholders:
    [+frmcost+] //Numeric price for PayPal form
    [+cost+]  //Pre formatted HTML string to strikeout itemprice and display saleprice
    
    Still to come:
    Add exception if sale date has expired.
    */
    if ($onsale == 1) {
    	$cost = '<del>$'.$orprice.'</del><br /><span style="color:red;">$'.$saleprice.'</span>';
    $frmcost = $saleprice;
    }else{
    	$cost = '$'.$orprice;
    $frmcost = $orprice;
    }
    $modx->setPlaceholder('cost', $cost);
    $modx->setPlaceholder('frmcost', $frmcost);
    return;
    ?>


    Here’s the Ditto call on the category page:
    [!Ditto? &parents=`3` &tpl=`dittoTpl` &tagData=`cats` &tagDelimiter=`,` &tagMode=`onlyTags` &tags=`Widgets`!]


    And here’s the snippet call within Ditto template:
    [[salecost? &orprice=`[+price+]` &saleprice=`[+saleprice+]`  &onsale=`[+chksale+]`]]


    I’m not sure why this isn’t working. Right below the salecost call I’m calling Maxigallery which is working great. huh
      I twitch because I care....and drink too much coffee.
      • 4172
      • 5,888 Posts
      did you try:
      if ($onsale == '1')
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 26310
        • 130 Posts
        I just did and still no luck. If I hard code in the values to the snippet call it works...in fact it seems to be the sale price TV that’s causing an error. I renamed the variable to ’sprice’ with no luck.
          I twitch because I care....and drink too much coffee.
          • 30552
          • 75 Posts
          What if you make the category page uncacheable, then call Ditto cached, i.e.
          [[Ditto? &parents=`3` &tpl=`dittoTpl` &tagData=`cats` &tagDelimiter=`,` &tagMode=`onlyTags` &tags=`Widgets`]]


          then call your snippet uncached, i.e.
          [!salecost? &orprice=`[+price+]` &saleprice=`[+saleprice+]`  &onsale=`[+chksale+]`!]


          It just seems like something like the salecost snippet, whose output would always change, should be called uncached. I apologize if I’m incorrect!
            • 26310
            • 130 Posts
            Tried that just now as well. I made the snippet call uncahced and the page but kept Ditto cached. This produced no results from the snippet. I changed the snippet call only back to cached and I was able to get back to square one.
            No apologizes needed I’m no PHP guru by any means.

            Does anyone know, could it be because the Ditto call is repeatedly calling the snippet? Maybe I didn’t write the snippet right?
              I twitch because I care....and drink too much coffee.
              • 30552
              • 75 Posts
              I’m afraid that was the only suggestion I had sad

              I’ve had a terrible time in the past getting snippets to return output via a Ditto call.
                • 26310
                • 130 Posts
                Ok well I just confirmed that it seems to be because the snippet is being called multiple times on the page. After marking the first item listed on sale, the first items sale price was displayed for all items. It seems I need to research how to make a snippet that can be called many times over on the same page. Thanks for everyones help.
                  I twitch because I care....and drink too much coffee.
                  • 8109
                  • 128 Posts
                  I’ve found that calling my snippets [!uncached!] allows them to be called multiple times per page. There are a number of wiki and forum documents related to this topic. Good luck!
                    Revo 2.0.8-pl