Statistics: Posted by Cristián Lávaque — April 22nd, 2011, 11:55 pm
Statistics: Posted by toddz88 — April 22nd, 2011, 11:50 pm
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']));
}
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().
Statistics: Posted by Cristián Lávaque — April 22nd, 2011, 10:36 pm
/*
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.
}
Statistics: Posted by toddz88 — April 22nd, 2011, 7:09 pm