Dynamic pricing – Charging multiple times the expected final price
-
I needed to calculate the price of a typeface based on the 3 different licence values selected. I couldn’t find a way of doing this with variations unless I created a variation for each possible combination which would be insane!
So I opted to calculate the price using the
woocommerce_before_calculate_totals
hook.I created the two functions below which all seems to work fine. The correct price is set and charged. Except randomly some customers find themselves being charged multiples of the expected final price. Sometimes up to 6 times the expected price.
I know there are a lot of variables here, but would any one have any idea why this might be? Mike Jolley hinted that the hook can run multiple times and to set a custom prop to resolve but I haven’t had any success trying. I also can’t recreate the problem locally so it’s proving incredibly tricky to resolve!
If anyone could shed any light or possible avenues to explore that would be amazing!
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' ); function add_custom_price( $cart_object ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; if (WC()->cart->get_cart_contents_count() > 0): // Licence values chosen from omsetype.co buying module e.g. 1:10000:2500 must have at least one value $multiplier_desktop = ! empty(WC()->session->get('licence')) ? WC()->session->get('licence') : 0; $multiplier_web = ! empty(WC()->session->get('licence_web')) ? WC()->session->get('licence_web') : 0; $multiplier_app = ! empty(WC()->session->get('licence_app')) ? WC()->session->get('licence_app') : 0; foreach ( $cart_object->cart_contents as $key => $value ): if ($value['data']->is_type('variation')): $price = mttd_return_price($multiplier_desktop, $multiplier_web, $multiplier_app, $value['data']); $value['data']->set_price($price); endif; endforeach; endif; } /** * Return price **/ function mttd_return_price($multiplier_desktop, $multiplier_web, $multiplier_app, $product) { $price = 0; $price_base = $product->get_price(); $price_division_web = get_field('price_division_web', 'options'); // 100 $price_division_app = get_field('price_division_app', 'options'); // 50 if (!empty($multiplier_desktop) && $multiplier_desktop > 0): $price += $price_base * sqrt(floatval($multiplier_desktop)); endif; if (!empty($multiplier_web) && $multiplier_web > 0): $price += ($price_base * sqrt(floatval($multiplier_web))) / $price_division_web; endif; if (!empty($multiplier_app) && $multiplier_app > 0): $price += ($price_base * sqrt(floatval($multiplier_app))) / $price_division_app; endif; return $price; }
### WordPress Environment ### Home URL: https://omsetype.co Site URL: https://omsetype.co WC Version: 3.1.0 Log Directory Writable: ? WP Version: 4.8.5 WP Multisite: – WP Memory Limit: 256 MB WP Debug Mode: – WP Cron: ? Language: en_GB ### Server Environment ### Server Info: Apache PHP Version: 5.6.32 PHP Post Max Size: 200 MB PHP Time Limit: 300 PHP Max Input Vars: 1000 cURL Version: 7.54.0 OpenSSL/1.0.2k SUHOSIN Installed: – MySQL Version: 5.6.36 Max Upload Size: 200 MB Default Timezone is UTC: ? fsockopen/cURL: ? SoapClient: ? DOMDocument: ? GZip: ? Multibyte String: ? Remote Post: ? wp_remote_post() failed. Contact your hosting provider. cURL error 28: Operation timed out after 10000 milliseconds with 0 out of 0 bytes received Remote Get: ? wp_remote_get() failed. Contact your hosting provider. 200 ### Database ### WC Database Version: 3.1.0 WC Database Prefix: ftr57D_omse_ woocommerce_sessions: ? woocommerce_api_keys: ? woocommerce_attribute_taxonomies: ? woocommerce_downloadable_product_permissions: ? woocommerce_order_items: ? woocommerce_order_itemmeta: ? woocommerce_tax_rates: ? woocommerce_tax_rate_locations: ? woocommerce_shipping_zones: ? woocommerce_shipping_zone_locations: ? woocommerce_shipping_zone_methods: ? woocommerce_payment_tokens: ? woocommerce_payment_tokenmeta: ? MaxMind GeoIP Database: ? ### Security ### Secure connection (HTTPS): ? Hide errors from visitors: ? ### Active Plugins (17) ### ACF to REST API: by Aires Gon?alves – 3.1.0 Advanced Custom Fields PRO: by Elliot Condon – 5.6.7 Horizontal scrolling announcement: by Gopi Ramasamy – 8.7 Redirection: by John Godley – 2.10.1 Scripts n Styles: by unFocus Projects – 3.4.4 Scroll Magic: by BestBug – 3.0 TinyMCE Advanced: by Andrew Ozz – 4.6.3 Typed: by zonnix – 1.1.3 WooCommerce Bulk Edit Product Variations: by matterico_themes – 2.6 Woocommerce Advanced Bulk Edit: by George Iron – 4.2.2 WooCommerce EU VAT Compliance (Free): by David Anderson – 1.11.21 WooCommerce Stripe Gateway: by WooCommerce – 3.2.3 WooCommerce: by Automattic – 3.1.0 – 3.2.6 is available Wordfence Security: by Wordfence – 6.3.12 Yoast SEO: by Team Yoast – 5.9.3 WP SES: by Sylvain Deaure – 0.7.21 WP Sync DB: by Sean Lang – 1.5 ### Settings ### API Enabled: ? Force SSL: ? Currency: GBP (£) Currency Position: left Thousand Separator: , Decimal Separator: . Number of Decimals: 2 Taxonomies: Product Types: external (external) grouped (grouped) simple (simple) variable (variable) Taxonomies: Product Visibility: exclude-from-catalog (exclude-from-catalog) exclude-from-search (exclude-from-search) featured (featured) outofstock (outofstock) rated-1 (rated-1) rated-2 (rated-2) rated-3 (rated-3) rated-4 (rated-4) rated-5 (rated-5) ### WC Pages ### Shop base: #226 - /shop/ Basket: #227 - /cart/ Checkout: #228 - /checkout/ My account: #229 - /my-account/ ### Theme ### Name: OMSE Type Version: v1 – 0 is available Author URL: Child Theme: ? – If you are modifying WooCommerce on a parent theme that you did not build personally we recommend using a child theme. See: How to create a child theme WooCommerce Support: ? ### Templates ### Overrides: omse-type/woocommerce/archive-product.php omse-type/woocommerce/auth/footer.php omse-type/woocommerce/auth/form-grant-access.php omse-type/woocommerce/auth/form-login.php omse-type/woocommerce/auth/header.php omse-type/woocommerce/cart/cart-empty.php version 2.0.0 is out of date. The core version is 3.1.0 omse-type/woocommerce/cart/cart-item-data.php omse-type/woocommerce/cart/cart-shipping.php version 2.5.0 is out of date. The core version is 3.1.0 omse-type/woocommerce/cart/cart-totals.php omse-type/woocommerce/cart/cart.php version 3.0.3 is out of date. The core version is 3.1.0 omse-type/woocommerce/cart/cross-sells.php omse-type/woocommerce/cart/mini-cart.php version 2.5.0 is out of date. The core version is 3.1.0 omse-type/woocommerce/cart/proceed-to-checkout-button.php omse-type/woocommerce/cart/shipping-calculator.php omse-type/woocommerce/checkout/cart-errors.php omse-type/woocommerce/checkout/form-billing.php version 3.0.0 is out of date. The core version is 3.0.9 omse-type/woocommerce/checkout/form-checkout.php omse-type/woocommerce/checkout/form-coupon.php omse-type/woocommerce/checkout/form-login.php omse-type/woocommerce/checkout/form-pay.php omse-type/woocommerce/checkout/form-shipping.php version 3.0.0 is out of date. The core version is 3.0.9 omse-type/woocommerce/checkout/payment-method.php omse-type/woocommerce/checkout/payment.php omse-type/woocommerce/checkout/review-order.php omse-type/woocommerce/checkout/terms.php version 2.5.0 is out of date. The core version is 3.1.0 omse-type/woocommerce/checkout/thankyou.php omse-type/woocommerce/content-product.php omse-type/woocommerce/content-product_cat.php omse-type/woocommerce/content-single-product.php omse-type/woocommerce/content-widget-product.php omse-type/woocommerce/emails/admin-cancelled-order.php omse-type/woocommerce/emails/admin-failed-order.php omse-type/woocommerce/emails/admin-new-order.php omse-type/woocommerce/emails/customer-completed-order.php omse-type/woocommerce/emails/customer-invoice.php omse-type/woocommerce/emails/customer-new-account.php omse-type/woocommerce/emails/customer-note.php omse-type/woocommerce/emails/customer-on-hold-order.php omse-type/woocommerce/emails/customer-processing-order.php omse-type/woocommerce/emails/customer-refunded-order.php omse-type/woocommerce/emails/customer-reset-password.php omse-type/woocommerce/emails/email-addresses.php omse-type/woocommerce/emails/email-customer-details.php omse-type/woocommerce/emails/email-footer.php omse-type/woocommerce/emails/email-header.php omse-type/woocommerce/emails/email-order-details.php omse-type/woocommerce/emails/email-order-items.php omse-type/woocommerce/emails/email-styles.php omse-type/woocommerce/emails/plain/admin-cancelled-order.php omse-type/woocommerce/emails/plain/admin-failed-order.php omse-type/woocommerce/emails/plain/admin-new-order.php omse-type/woocommerce/emails/plain/customer-completed-order.php omse-type/woocommerce/emails/plain/customer-invoice.php omse-type/woocommerce/emails/plain/customer-new-account.php omse-type/woocommerce/emails/plain/customer-note.php omse-type/woocommerce/emails/plain/customer-on-hold-order.php omse-type/woocommerce/emails/plain/customer-processing-order.php omse-type/woocommerce/emails/plain/customer-refunded-order.php omse-type/woocommerce/emails/plain/customer-reset-password.php omse-type/woocommerce/emails/plain/email-addresses.php omse-type/woocommerce/emails/plain/email-customer-details.php omse-type/woocommerce/emails/plain/email-order-details.php omse-type/woocommerce/emails/plain/email-order-items.php omse-type/woocommerce/global/breadcrumb.php omse-type/woocommerce/global/form-login.php omse-type/woocommerce/global/quantity-input.php omse-type/woocommerce/global/sidebar.php omse-type/woocommerce/global/wrapper-end.php omse-type/woocommerce/global/wrapper-start.php omse-type/woocommerce/loop/add-to-cart.php omse-type/woocommerce/loop/loop-end.php omse-type/woocommerce/loop/loop-start.php omse-type/woocommerce/loop/no-products-found.php omse-type/woocommerce/loop/orderby.php omse-type/woocommerce/loop/pagination.php omse-type/woocommerce/loop/price.php omse-type/woocommerce/loop/rating.php omse-type/woocommerce/loop/result-count.php omse-type/woocommerce/loop/sale-flash.php omse-type/woocommerce/myaccount/dashboard.php omse-type/woocommerce/myaccount/downloads.php omse-type/woocommerce/myaccount/form-add-payment-method.php omse-type/woocommerce/myaccount/form-edit-account.php omse-type/woocommerce/myaccount/form-edit-address.php version 3.0.0 is out of date. The core version is 3.0.9 omse-type/woocommerce/myaccount/form-login.php omse-type/woocommerce/myaccount/form-lost-password.php omse-type/woocommerce/myaccount/form-reset-password.php omse-type/woocommerce/myaccount/lost-password-confirmation.php omse-type/woocommerce/myaccount/my-account.php omse-type/woocommerce/myaccount/my-address.php omse-type/woocommerce/myaccount/my-downloads.php omse-type/woocommerce/myaccount/my-orders.php omse-type/woocommerce/myaccount/navigation.php omse-type/woocommerce/myaccount/orders.php omse-type/woocommerce/myaccount/payment-methods.php omse-type/woocommerce/myaccount/view-order.php omse-type/woocommerce/notices/error.php omse-type/woocommerce/notices/notice.php omse-type/woocommerce/notices/success.php omse-type/woocommerce/order/form-tracking.php omse-type/woocommerce/order/order-again.php omse-type/woocommerce/order/order-details-customer.php omse-type/woocommerce/order/order-details-item.php omse-type/woocommerce/order/order-details.php omse-type/woocommerce/order/tracking.php omse-type/woocommerce/product-searchform.php omse-type/woocommerce/single-product/add-to-cart/external.php omse-type/woocommerce/single-product/add-to-cart/grouped.php omse-type/woocommerce/single-product/add-to-cart/simple.php omse-type/woocommerce/single-product/add-to-cart/variable.php omse-type/woocommerce/single-product/add-to-cart/variation-add-to-cart-button.php omse-type/woocommerce/single-product/add-to-cart/variation.php omse-type/woocommerce/single-product/meta.php omse-type/woocommerce/single-product/photoswipe.php omse-type/woocommerce/single-product/price.php omse-type/woocommerce/single-product/product-attributes.php version 3.0.0 is out of date. The core version is 3.1.0 omse-type/woocommerce/single-product/product-image.php version 3.0.2 is out of date. The core version is 3.1.0 omse-type/woocommerce/single-product/product-thumbnails.php version 3.0.2 is out of date. The core version is 3.1.0 omse-type/woocommerce/single-product/rating.php version 2.3.2 is out of date. The core version is 3.1.0 omse-type/woocommerce/single-product/related.php omse-type/woocommerce/single-product/review-meta.php omse-type/woocommerce/single-product/review-rating.php version 3.0.0 is out of date. The core version is 3.1.0 omse-type/woocommerce/single-product/review.php omse-type/woocommerce/single-product/sale-flash.php omse-type/woocommerce/single-product/share.php omse-type/woocommerce/single-product/short-description.php omse-type/woocommerce/single-product/stock.php omse-type/woocommerce/single-product/tabs/additional-information.php omse-type/woocommerce/single-product/tabs/description.php omse-type/woocommerce/single-product/tabs/tabs.php omse-type/woocommerce/single-product/title.php omse-type/woocommerce/single-product/up-sells.php omse-type/woocommerce/single-product-reviews.php omse-type/woocommerce/single-product.php omse-type/woocommerce/taxonomy-product_cat.php omse-type/woocommerce/taxonomy-product_tag.php Outdated Templates: ?Learn how to update
- The topic ‘Dynamic pricing – Charging multiple times the expected final price’ is closed to new replies.