Skip to content

Instantly share code, notes, and snippets.

@cgi-caesar
Last active March 4, 2021 13:42
Show Gist options
  • Save cgi-caesar/575ced99e40b9d6c24668f39c550d321 to your computer and use it in GitHub Desktop.
Save cgi-caesar/575ced99e40b9d6c24668f39c550d321 to your computer and use it in GitHub Desktop.
Pay As You Wish (Above Product Price)

Pay As You Wish (Above Product Price)

  • Add Product brick
  • Add donation Brick for each product that you want user can choose price for
  • Add JavScript brick with code (you need to correct map array to map products to donation bricks)
  • Add Code to site.php
jQuery(function() {
var map =[['1-1', '900'], ['3-3', '901']];
for(var i=0;i<map.length;i++) {
let chck = jQuery('<input type="checkbox" />');
let amt = jQuery('#product_id_' + map[i][1] + '-donation-amount');
amt.attr('placeholder', jQuery('#product-' + map[i][0]).data('first_price'));
if (amt.val()) {
chck.attr('checked', 'checked');
}
let amtdiv = jQuery('#row-product_id_' + map[i][1] + '-donation-group').detach();
jQuery('#am-product-terms-' + map[i][0]).append(amtdiv);
amtdiv.hide();
amt.before(chck);
chck.change(function(){
chck.nextAll().toggle(this.checked);
if (!this.checked) {
amt.val('');
}
}).change();
}
jQuery(".am-product-terms .am-row .am-element-title .comment").detach();
jQuery('input[type=radio]').change(function() {
for(var i=0;i<map.length;i++) {
jQuery('#row-product_id_' + map[i][1] + '-donation-group').toggle(jQuery('#product-' + map[i][0]).is(':checked'));
if (!jQuery('#product-' + map[i][0]).is(':checked')) {
let chck = jQuery('#row-product_id_' + map[i][1] + '-donation-group [type=checkbox]');
let amt = jQuery('#row-product_id_' + map[i][1] + '-donation-group [type=text]');
chck.prop('checked', false);
chck.change();
amt.val('');
}
}
}).change();
});
<?php
//handle empty donation amount, reset to product price
if (!empty($_POST['product_id_page-0'])) {
[$product] = $_POST['product_id_page-0'];
[$pid, $bpid] = explode('-', $product);
if (empty($_POST['donation'][$pid])) {
$bp = Am_Di::getInstance()->billingPlanTable->load($bpid);
$_POST['donation'][$pid] = $bp->first_price;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment