We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43240
    • 10 Posts
    Hi All

    my first question and its going to be focused around the handy little PayPal snippet by mark Hamstra

    if anyone is not familiar with it then you can check it out here http://rtfm.modx.com/display/ADDON/mhPayPal

    he also wrote a handy blog about it back when it didn't have half the features it does now https://www.markhamstra.com/modx-blog/2011/12/accepting-paypal-on-your-modx-site/

    anyway ... on to my point

    Currently the snippet does not allow for the user to pass in a quantity in the snippet. So if i wanted to make a "buy it now" button with a an item such as ...socks, i could only buy a pair at a time.


    the snippet does allow for extra fields to be passed into the snippet which "i think" it reads from extra input fields i add to my formTpl.

    below is my snippet call so far

    [[!mhPayPal?
    &amount=`[[*productprice]]`
    &amountTax=`2.85`
    &description=`[[*menutitle]] ([[+currency]][[+amount]])`
    &currencies=`GBP,EUR,USD`
    &formTpl=`priceTemplate`
    &shipping=`1`
    &extraRequiredFields=`quantity`
    ]]


    I have made essentially a product page which allows the price to be edited in the manager tpl variable and i have passed my own form into it and stripped out the features i don't need (the donate field which allows the user to type in an amount they want to pay)

    From the snippet it works perfectly and checks out to the Paypal express checkout with price, description and currency all working perfectly.

    But now i want to be able to pass in a quantity so that the user could buy 2 socks or 3 socks

    here is the code i have added to my formTpl which is used to gather data from

    <div class="quantity">
    <input type="text" name="quantity" value="[[+quantity:empty=`1`]]">
    </div>


    you can see i have added a "quantity" placeholder which i have then mentioned in my original snippet call above &extraRequiredFields=`quantity`

    extraRequiredFields
    [string] Comma separated list of field names that are required and cannot be left empty, can be used to collect extra data about the user for use in a custom hook or the likes.



    This is where i hit a roadblock

    I don't know if my [[+quantity:empty=`1`]] is actually the correct syntax.

    is my reference to that placeholder correct &extraRequiredFields=`quantity` or &extraRequiredFields=`[[+quantity]]`

    What i want to accomplish

    I want to be able to pass in that extra parameter that the user can type in and then use that to multiply the price by that amount.

    Example

    quantity = 5 (that the end user types in .. defaults to one if not changed)
    price of item (which is defined by site owner &amount=`[[*productprice]]`)

    submit form

    goes into a hook which will take the price and multiple it by the quantity

    Goes to checkout with final price.

    the snippet does allow for posthooks which allow for price changes prior to connecting to paypal

    &postHooks
    [string] Comma separated list of hooks to be executed after successfully submitting the form, but before connecting with PayPal to set up the payment. Could be used to (for example) calculate taxes before it is registered with PayPal, or to further validate the data.


    I have looked into creating the hook but im not too familar with the modx api and reading from information passed into a snippet from another snippet.

    if anyone can give a steer with the information below it would be a massive help.

    Simply call a snippet in one of the hooks properties, and it will be executed.

    Available in the $scriptProperties and as $variables are all the data available at that point. Configuration properties. $mhpp class.


      • 18373 ☆ A M B ☆
      • 3,141 Posts
      Do keep in mind it's not been publicly released yet on the extras repo, which in my case usually means it 1) wasn't entirely done yet 2) can be refactored to eternity without caring about backwards compatibility or 3) documentation may be completely off.

      The below assumes version 1.1.1-rc1 from https://github.com/Mark-H/mhPayPal/tree/master/_packages!

      Yes you can use [[+quantity]] in your formTpl. Not in the mhPayPal snippet call tho, just in the chunk. Your &extraRequiredFields=`quantity` syntax is the right one. &extraRequiredFields just checks if a field was posted.

      The snippet you'll need as hook takes the "amount" property, and multiplies it by whatever is being passed to it as "quantity" value. Something like this (untested):

      <?php
      /**
       * @var mhPayPal $mhpp 
       * @var modX $modx
       */
      
      $priceEach = $mhpp->getProperty('amount');
      $quantity = (int)$modx->getOption('quantity', $mhpp->values, 1, true);
      
      // PHP doesn't handle floats all that well when calculating, so make integer
      $priceEach = (int)($priceEach * 100);
      $priceTotal = $priceEach * $quantity;
      
      // Bring back price to float
      $priceTotal = $priceTotal / 100;
      
      // Set total price back to the class so it can make the payment etc.
      $mhpp->setProperty('amount', $priceTotal);
      
      // You may want to do the same for the amountTax property and
      // probably also update the description property in the same way.
      
      [ed. note: markh last edited this post 13 years, 5 months ago.]
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 43240
        • 10 Posts
        That is perfect, just the steer i was looking for. I just needed to get my head around how to access the $mhpp->getProperty('amount'); aspect of things from different snippets.

        I will keep this thread updated if i get a completed solution.

        I saw you just updated the mhpaypal to version 1.1.1-rc1 a few minutes ago. what kind of changes did you make to it so i can take a look at the code and learn from it.



        Quote from: markh at Apr 27, 2013, 05:48 AM
        Do keep in mind it's not been publicly released yet on the extras repo, which in my case usually means it 1) wasn't entirely done yet 2) can be refactored to eternity without caring about backwards compatibility or 3) documentation may be completely off.

        The below assumes version 1.1.1-rc1 from https://github.com/Mark-H/mhPayPal/tree/master/_packages!

        Yes you can use [[+quantity]] in your formTpl. Not in the mhPayPal snippet call tho, just in the chunk. Your &extraRequiredFields=`quantity` syntax is the right one. &extraRequiredFields just checks if a field was posted.

        The snippet you'll need as hook takes the "amount" property, and multiplies it by whatever is being passed to it as "quantity" value. Something like this (untested):

        <!--?php
        /**
         * @var mhPayPal $mhpp 
         * @var modX $modx
         */
        
        $priceEach = $mhpp--->getProperty('amount');
        $quantity = (int)$modx->getOption('quantity', $mhpp->values, 1, true);
        
        // PHP doesn't handle floats all that well when calculating, so make integer
        $priceEach = (int)($priceEach * 100);
        $priceTotal = $priceEach * $quantity;
        
        // Bring back price to float
        $priceTotal = $priceTotal / 100;
        
        // Set total price back to the class so it can make the payment etc.
        $mhpp->setProperty('amount', $priceTotal);
        
        // You may want to do the same for the amountTax property and
        // probably also update the description property in the same way.
        
          • 18373 ☆ A M B ☆
          • 3,141 Posts
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 43240
            • 10 Posts
            Just gave your snippet and run and its throwing me this error after submitting my paypal form.

            Fatal error: Cannot access protected property mhPayPal::$values in /home/med/public_html/core/cache/includes/elements/modsnippet/56.include.cache.php on line 13

            I had a go at changing the variable to public as it is currently protected and it goes through to checkout but the details quantity is not multiplied.

            any thoughts on this.

            I'm gonna keep playing around in the mean time to see if i can get to the bottom of it

            ^_^