Community Support Forums — WordPress® ( Users Helping Users ) — 2011-04-22T23:55:23-05:00 http://www.primothemes.com/forums/feed.php?f=4&t=6172 2011-04-22T23:55:23-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6172&p=13208#p13208 <![CDATA[Re: How to set a ShortCode attribute dynamically, using Hook]]>
Yes, I also add some comments to my code when I need to know what's going on. Most of the time, though, the function and vars names almost say all I need, and being in 3 lines I can see what's going on all right there.

I'm curious, what error checking and user feedback did you add?

Statistics: Posted by Cristián Lávaque — April 22nd, 2011, 11:55 pm


]]>
2011-04-22T23:50:05-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6172&p=13205#p13205 <![CDATA[Re: How to set a ShortCode attribute dynamically, using Hook]]>
And you're right.. after I posted, I found the need for is_numeric, and also added bit of error checking and user feedback.

Statistics: Posted by toddz88 — April 22nd, 2011, 11:50 pm


]]>
2011-04-22T22:36:22-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6172&p=13201#p13201 <![CDATA[Re: How to set a ShortCode attribute dynamically, using Hook]]>

I personally enjoy discussing code and how it can be written in other ways, so here's my version of it in case you're interested and find it helpful.

Code:
add_action('ws_plugin__s2member_pro_before_sc_paypal_form_after_shortcode_atts', 'my_dynamic_price', 10);

function my_dynamic_price($vars)
{
    $_GET['qty'] = isset($_GET['qty']) && is_numeric($_GET['qty']) && $_GET['qty'] > 1 ? (int) $_GET['qty'] : 1;
    $vars['__refs']['attr']['ra'] *= $_GET['qty'];
    $vars['__refs']['attr']['desc'] = strtr($vars['__refs']['attr']['desc'], array('%%My-Qty%%' => $_GET['qty'], '%%My-Price%%' => $vars['__refs']['attr']['ra']));
}
 


You will notice that I changed the is_int for is_numeric, because it is recommended in the documentation.

is_int wrote:
To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().


I also make sure that there is a quantity passed in the URL and that it's at least 1, e.g. not 0.

I changed the str_ireplace to strtr because it's faster and am assuming that the replacement code can be case sensitive (faster than insensitive).

Let me know what you think.

Statistics: Posted by Cristián Lávaque — April 22nd, 2011, 10:36 pm


]]>
2011-04-22T19:09:26-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6172&p=13189#p13189 <![CDATA[How to set a ShortCode attribute dynamically, using Hooks]]>
GOAL
To set ShortCode attributes dynamically, specifically the PRICE ("ra" or Regular Amount) and DESCRIPTION, using HOOKS, instead of a php-exec plugin.

BACKGROUND
I read the Forum post from Jason "TIP: Using variables in a Shortcode" (viewtopic.php?f=36&t=1604), but I wanted to use HOOKS (Filters or Actions), instead of a PHP-exec plugin. I also watched the new Video series, related to the post "Custom Registration Fields ( a developer how-to )" (viewtopic.php?f=36&t=2834).

I found the ShortCode prep code in "s2member-pro/includes/classes/gateways/paypal/paypal-form-in.inc.php", and I found this hook:

ws_plugin__s2member_pro_before_sc_paypal_form_after_shortcode_atts

I created my /wp-content/mu-plugins/s2-hacks.php file, and wrote this:

Code:
/*
DYNAMIC SHORTCODES USING HOOKS IN S2MEMBER PRO.

Get the Quantity var from the url, multiply it by the Price (per user), to derive a Total, and update the RA (Regular Amount) attribute of the ShortCode. Also update the Description accordinly, using our own custom Replacement Codes! Using Hooks instead of php-exec plugins.
*/

add_action('ws_plugin__s2member_pro_before_sc_paypal_form_after_shortcode_atts','my_dynamic_price',10);

function 
my_dynamic_price $vars = array() ) {
    
    
# PREP
    
$attr = &$vars["__refs"]["attr"]; // get the vars as REFERENCES so we actually modify the originals, not just copy them.

    # QUANTITY
    
$quantity htmlentities($_GET['qty']); // from link on Membership Options page.
    
if(!is_int($quantity)) { // verify it's an integer, otherwise default to 1.
        
$quantity 1;
    }
    
    
# PRICE
    
$price_each $attr["ra"]; // price for Individual; from orig RA (price) in ShortCode.     
    
$total $quantity $price_each// math.
        //$total = 9876; //TEST with hardcode value.
    
    # DESCRIPTION
    
$orig_desc $attr["desc"]; // orig DESCRIPTION expects two Custom Replacement Codes: %%My-Qty%% and %%My-Price%%.
    
$new_desc str_ireplace('%%My-Qty%%'$quantity$orig_desc); // replace
    
$new_desc str_ireplace('%%My-Price%%'$total$new_desc); // replace

    # OUTPUT
    
$attr["ra"] = "$total"// update RA to new value. $total must be in quotes, to make $attr["ra"] be a string, which I guess it needs to be.
    
$attr["desc"] = $new_desc// update DESCRIPTION so user sees new values.



So if my form url is "domain.com/signup?qty=20" and if my shortcode has these attributes: ra="99" and desc="Group account for %%My-Qty%% users: $%%My-Price%% USD / Monthly ( recurring charge, for ongoing access )"

then my form shows "Group account for 20 users: $1980 USD / Monthly ( recurring charge, for ongoing access )"

And after payment, my PayPal dev sandbox > Test Emails shows:
For:Group account for 20 users: $1980 USD / Monthly ( recurring charge, for ongoing access )
Amount paid each time:$1,980.00 USD

Which looks good, I think.

Statistics: Posted by toddz88 — April 22nd, 2011, 7:09 pm


]]>