1/** 2 * Backward compatible. 3 * 4 * @package WooCommerce 5 */ 6 7var affirmAsLowAsOptions = affirmOptions; 8 9jQuery( document ).ready( 10 function ( $ ) { 11 12 /** 13 * Get current amount from `data-amount`. 14 * 15 * @return {number} Amount. 16 */ 17 function getAmount() 18 { 19 return $( '#learn-more' ).data( 'amount' ) 20 } 21 22 /** 23 * Update the amount. 24 * 25 * This will update the `data-amount` attribute in Monthly Payment Messaging 26 * element. 27 * 28 * @param {number} amount - New amount in cents. 29 */ 30 function updateAmount( amount ) 31 { 32 if ( ! isAffirmExists() ) { 33 return; 34 } 35 36 $( '#learn-more' ).attr( 'data-amount', amount ); 37 38 if ('function' === typeof affirm.ui.refresh ) { 39 affirm.ui.refresh(); 40 } 41 } 42 43 /** 44 * Get the minimum amount allowed by Affirm. 45 * 46 * @see {@link https://docs.affirm.com/Partners/Email_Service_Providers/Monthly_Payment_Messaging_API#Collect_the_loan_details|Collect the loan details} 47 * 48 * @return {number} Minimum allowed amount in cents. 49 */ 50 function getMinimumAllowedAmount() 51 { 52 return parseInt( affirmOptions.minimum, 10 ); 53 } 54 55 /** 56 * Get the maximum amount allowed by Affirm. 57 * 58 * @see {@link https://docs.affirm.com/Partners/Email_Service_Providers/Monthly_Payment_Messaging_API#Collect_the_loan_details|Collect the loan details} 59 * 60 * @return {number} Maximum allowed amount in cents. 61 */ 62 function getMaximumAllowedAmount() 63 { 64 return parseInt( affirmOptions.maximum, 10 ); 65 } 66 67 /** 68 * Init support for composite product. 69 */ 70 function initCompositeProductSupport() 71 { 72 var composite_data = $( '.composite_data' ); 73 if (composite_data.length ) { 74 composite_data.on( 'wc-composite-initializing', compositeUpdateAffirmMonthlyPaymentMessaging ); 75 } 76 } 77 78 /** 79 * Update amount when component selection is changed in composite product. 80 * 81 * @param {object} event - Event. 82 * @param {object} composite - Composite object. 83 */ 84 function compositeUpdateAffirmMonthlyPaymentMessaging( event, composite ) 85 { 86 $( document.body ).off( 'found_variation', '.variations_form', onVariationUpdated ); 87 88 var updateAmount = onComponentUpdated.bind( { composite: composite } ); 89 90 composite.actions.add_action( 'component_selection_changed', updateAmount, 99 ); 91 composite.actions.add_action( 'component_quantity_changed', updateAmount, 99 ); 92 } 93 94 /** 95 * Update amount when composite component is updated. 96 * 97 * @callback onComponentUpdated 98 */ 99 function onComponentUpdated() 100 { 101 var totals = this.composite.api.get_composite_totals(); 102 103 if ('object' !== typeof totals ) { 104 return; 105 } 106 if ( ! totals.price ) { 107 return; 108 } 109 110 updateAmount( totals.price * 100 ); 111 } 112 113 /** 114 * Update amount based on variation price. 115 * 116 * @param {object} event - Event. 117 * @param {object} variation - Variation properties. 118 * 119 * @callback onVariationUpdated 120 */ 121 function onVariationUpdated( event, variation ) 122 { 123 updateAmount( variation.display_price * 100 ) 124 } 125 126 /** 127 * Update amount when shipping cost is updated. 128 * 129 * @callback onShippingMethodUpdated 130 */ 131 function onShippingMethodUpdated() 132 { 133 let currentAmount = $( '#learn-more' ).attr( 'data-amount' ); 134 let amount = getAmount(); 135 136 if (currentAmount != amount ) { 137 updateAmount( amount ); 138 } 139 } 140 141 /** 142 * Update amount when cart total is updated. 143 * 144 * @callback onCartTotalsUpdated 145 */ 146 function onCartTotalsUpdated() 147 { 148 // The DOM is rendered again so need to call init again. 149 init(); 150 151 updateAmount( getAmount() ); 152 } 153 154 /** 155 * Check if Affirm and its dependencies exist. 156 * 157 * @return {bool} Returns true if Affirm and its dependencies exist. 158 */ 159 function isAffirmExists() 160 { 161 return ( 162 'undefined' !== typeof affirm 163 && 164 'undefined' !== typeof affirmOptions 165 && 166 $( '#learn-more' ).length 167 ); 168 } 169 170 /** 171 * Init. 172 */ 173 function init() 174 { 175 if (isAffirmExists() ) { 176 // For a roduct, monitor for the customer changing the variation. 177 $( document.body ).on( 'found_variation', '.variations_form', onVariationUpdated ); 178 179 // For a cart, monitor for changes from shipping cost as well. 180 $( document.body ).on( 'updated_shipping_method', onShippingMethodUpdated ); 181
182 // Support updated price in composite product. 183 initCompositeProductSupport(); 184 } 185 } 186 187 init(); 188 189 $( document.body ).on( 'updated_cart_totals', onCartTotalsUpdated ); 190 191 } 192);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.