The function get_discount_total always returns 0 (zero)
-
Hi,
I have the issue that since about two weeks the function get_discount_total always returns 0 (zero) like in the following code:
add_action( 'woocommerce_order_status_processing', 'al_change_coupon_amount' ); function al_change_coupon_amount($order_id) { $order = wc_get_order( $order_id ); $discount_total = $order->get_discount_total(); }
What could be the reason?
-
This topic was modified 2 years, 9 months ago by
Adrian.
-
This topic was modified 2 years, 9 months ago by
-
Hi @adrianlacheta ,
Thanks for reaching out!
Please could you provide clarity on what it is you would like to achieve with this snippet, in order for a better understanding?
Thanks!
Hi Roxanne,
thanks for the reply. I’m trying to decrease the coupon amount by the discount total of an order. The script worked a few days ago. But something changed. And now no matter what I do the function get_discount_total() returns zero. I also tried it in a WP_Query with content type “shop_order”.
add_action( 'woocommerce_order_status_processing', 'al_change_coupon_amount' ); function al_change_coupon_amount($order_id) { $order = wc_get_order( $order_id ); $discount_total = $order->get_discount_total(); foreach( $order->get_used_coupons() as $coupon_code ){ $coupon_post_obj = get_page_by_title($coupon_code, OBJECT, 'shop_coupon'); $coupon_id = $coupon_post_obj->ID; $coupon = new WC_Coupon($coupon_id); $discount_type = $coupon->get_discount_type(); //fixed_cart //$is_voucher = get_post_meta( $coupon_id, 'is_voucher', true ); if ( $discount_type == 'fixed_cart' ){ $coupon_amount = $coupon->get_amount(); $new_coupon_amount = $coupon_amount - $discount_total; if ($new_coupon_amount > 0) { update_post_meta( $coupon_id, 'coupon_amount', $new_coupon_amount ); } else { $new_coupon_amount = 0; update_post_meta( $coupon_id, 'coupon_amount', $new_coupon_amount ); update_post_meta( $coupon_id, 'usage_limit', 1 ); } } } }
-
This reply was modified 2 years, 9 months ago by
Adrian.
Hello,
Please elaborate a bit more about how you are implementing this code, that way we can get a better context.
Also please share a copy of your site’s System Status, it is under
WooCommerce > Status
. Select “Get system report” and then “Copy for support”.?Thank you.
Hi Igor,
thanks for the support.
The code that I posted above is part of my functions.php. Do you need more Informations?This is my woocommerce system status log:
### WordPress Environment ### WordPress address (URL): https://oxundklee-ssl-v3:8890 Site address (URL): https://oxundklee-ssl-v3:8890 WC Version: 6.6.0 REST API Version: ? 6.6.0 WC Blocks Version: ? 7.6.0 Action Scheduler Version: ? 3.4.0 Log Directory Writable: ? WP Version: 6.0 WP Multisite: – WP Memory Limit: 256 MB WP Debug Mode: – WP Cron: ? Language: en_US External object cache: – ### Server Environment ### Server Info: Apache/2.4.46 (Unix) OpenSSL/1.0.2u PHP/7.4.12 mod_wsgi/3.5 Python/2.7.13 mod_fastcgi/mod_fastcgi-SNAP-0910052141 mod_perl/2.0.11 Perl/v5.30.1 PHP Version: 7.4.12 PHP Post Max Size: 1 GB PHP Time Limit: 300 PHP Max Input Vars: 3000 cURL Version: 7.68.0 OpenSSL/1.0.2u SUHOSIN Installed: – MySQL Version: 5.7.32 Max Upload Size: 1 GB Default Timezone is UTC: ? fsockopen/cURL: ? SoapClient: ? DOMDocument: ? GZip: ? Multibyte String: ? Remote Post: ? Remote Get: ? ### Database ### WC Database Version: 6.6.0 WC Database Prefix: wp_ Total Database Size: 74.95MB Database Data Size: 53.35MB Database Index Size: 21.60MB wp_woocommerce_sessions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_woocommerce_api_keys: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_attribute_taxonomies: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_woocommerce_downloadable_product_permissions: Data: 0.02MB + Index: 0.06MB + Engine InnoDB wp_woocommerce_order_items: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_woocommerce_order_itemmeta: Data: 0.13MB + Index: 0.14MB + Engine InnoDB wp_woocommerce_tax_rates: Data: 0.02MB + Index: 0.06MB + Engine InnoDB wp_woocommerce_tax_rate_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_shipping_zones: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_woocommerce_shipping_zone_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_shipping_zone_methods: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_woocommerce_payment_tokens: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_woocommerce_payment_tokenmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_log: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_actionscheduler_actions: Data: 1.02MB + Index: 0.13MB + Engine InnoDB wp_actionscheduler_claims: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_actionscheduler_groups: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_actionscheduler_logs: Data: 1.02MB + Index: 0.03MB + Engine InnoDB wp_aioseo_cache: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_aioseo_notifications: Data: 0.02MB + Index: 0.06MB + Engine InnoDB wp_aioseo_posts: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_borlabs_cookie_consent_log: Data: 17.84MB + Index: 4.06MB + Engine MyISAM wp_borlabs_cookie_content_blocker: Data: 0.02MB + Index: 0.00MB + Engine MyISAM wp_borlabs_cookie_cookies: Data: 0.01MB + Index: 0.00MB + Engine MyISAM wp_borlabs_cookie_groups: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_borlabs_cookie_script_blocker: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_borlabs_cookie_statistics: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_commentmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_comments: Data: 0.02MB + Index: 0.09MB + Engine InnoDB wp_icl_content_status: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_icl_core_status: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_icl_flags: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_icl_languages: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_icl_languages_translations: Data: 0.20MB + Index: 0.17MB + Engine InnoDB wp_icl_locale_map: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_icl_message_status: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_icl_mo_files_domains: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_icl_node: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_icl_reminders: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_icl_strings: Data: 0.13MB + Index: 0.22MB + Engine InnoDB wp_icl_string_batches: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_icl_string_packages: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_icl_string_pages: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_icl_string_positions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_icl_string_status: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_icl_string_translations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_icl_string_urls: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_icl_translate: Data: 1.52MB + Index: 0.06MB + Engine InnoDB wp_icl_translate_job: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_icl_translations: Data: 1.52MB + Index: 1.25MB + Engine InnoDB wp_icl_translation_batches: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_icl_translation_downloads: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_icl_translation_status: Data: 0.38MB + Index: 0.02MB + Engine InnoDB wp_links: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_mailchimp_carts: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_mailchimp_jobs: Data: 0.11MB + Index: 0.00MB + Engine InnoDB wp_options: Data: 5.31MB + Index: 0.34MB + Engine InnoDB wp_pantheon_sessions: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_postmeta: Data: 10.02MB + Index: 11.08MB + Engine InnoDB wp_posts: Data: 6.30MB + Index: 0.25MB + Engine InnoDB wp_redirection_404: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_redirection_groups: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_redirection_items: Data: 0.00MB + Index: 0.02MB + Engine MyISAM wp_redirection_logs: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_termmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_terms: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_term_relationships: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_term_taxonomy: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_usermeta: Data: 0.06MB + Index: 0.03MB + Engine InnoDB wp_users: Data: 0.02MB + Index: 0.05MB + Engine InnoDB wp_wcpdf_credit_note_number: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_wcpdf_invoice_number: Data: 0.05MB + Index: 0.03MB + Engine MyISAM wp_wcpdf_packing_slip_number: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_wcpdf_proforma_number: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_wc_admin_notes: Data: 0.06MB + Index: 0.00MB + Engine InnoDB wp_wc_admin_note_actions: Data: 0.05MB + Index: 0.02MB + Engine InnoDB wp_wc_category_lookup: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_wc_customer_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_wc_download_log: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_wc_order_coupon_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_wc_order_product_lookup: Data: 0.02MB + Index: 0.06MB + Engine InnoDB wp_wc_order_stats: Data: 0.02MB + Index: 0.05MB + Engine InnoDB wp_wc_order_tax_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_wc_product_attributes_lookup: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_wc_product_download_directories: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_wc_product_meta_lookup: Data: 0.06MB + Index: 0.09MB + Engine InnoDB wp_wc_rate_limits: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_wc_reserved_stock: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_wc_tax_rate_classes: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_wc_webhooks: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_wfblockediplog: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_wfblocks7: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_wfconfig: Data: 0.39MB + Index: 0.01MB + Engine MyISAM wp_wfcrawlers: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_wffilechanges: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_wffilemods: Data: 1.75MB + Index: 0.35MB + Engine MyISAM wp_wfhits: Data: 0.56MB + Index: 0.11MB + Engine MyISAM wp_wfhoover: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_wfissues: Data: 0.00MB + Index: 0.01MB + Engine MyISAM wp_wfknownfilelist: Data: 0.95MB + Index: 0.11MB + Engine MyISAM wp_wflivetraffichuman: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_wflocs: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_wflogins: Data: 0.08MB + Index: 0.02MB + Engine MyISAM wp_wfls_2fa_secrets: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_wfls_settings: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_wfnotifications: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_wfpendingissues: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_wfreversecache: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_wfsnipcache: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_wfstatus: Data: 0.07MB + Index: 0.04MB + Engine MyISAM wp_wftrafficrates: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_woocommerce_gzd_dhl_im_products: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_woocommerce_gzd_dhl_im_product_services: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_woocommerce_gzd_dhl_labelmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_gzd_dhl_labels: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_gzd_packaging: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_woocommerce_gzd_packagingmeta: Data: 0.00MB + Index: 0.00MB + Engine MyISAM wp_woocommerce_gzd_shipmentmeta: Data: 1.52MB + Index: 0.56MB + Engine InnoDB wp_woocommerce_gzd_shipments: Data: 0.23MB + Index: 0.19MB + Engine InnoDB wp_woocommerce_gzd_shipment_itemmeta: Data: 0.30MB + Index: 0.30MB + Engine InnoDB wp_woocommerce_gzd_shipment_items: Data: 0.23MB + Index: 0.33MB + Engine InnoDB wp_woocommerce_gzd_shipment_labelmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_gzd_shipment_labels: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_gzd_shipping_provider: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_woocommerce_gzd_shipping_providermeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_wpforms_tasks_meta: Data: 0.02MB + Index: 0.00MB + Engine InnoDB ### Post Type Counts ### acf-field: 131 acf-field-group: 9 attachment: 109 custom_css: 1 nav_menu_item: 8 page: 43 post: 6 product: 24 product_variation: 80 revision: 448 shop_coupon: 173 shop_order: 28 tageskontingent: 80 wp_global_styles: 1 wpcf7_contact_form: 3 ### Security ### Secure connection (HTTPS): ? Hide errors from visitors: ? ### Active Plugins (25) ### WPML Multilingual CMS: by OnTheGoSystems – 4.5.8 Native PHP Sessions for WordPress: by Pantheon – 1.2.4 Advanced Custom Fields Multilingual: by OnTheGoSystems – 1.10.3 Advanced Custom Fields PRO: by Delicious Brains – 5.12.2 All-in-One WP Migration: by ServMask – 7.61 Borlabs Cookie - Cookie Opt-in: by Borlabs GmbH – 2.2.51 Contact Form 7 Extension For Mailchimp: by Renzo Johnson – 0.5.62 Contact Form 7 Multilingual: by OnTheGoSystems – 1.1.0 Contact Form 7: by Takayuki Miyoshi – 5.5.6.1 Datalayer for Woocommerce: by Array é Vida – 1.0.9 Yoast Duplicate Post: by Enrico Battocchi & Team Yoast – 4.4 GTM4WP: by Thomas Geiger – 1.15.2 MC4WP: Mailchimp for WordPress: by ibericode – 4.8.7 Redirection: by John Godley – 5.2.3 Under Construction: by WebFactory Ltd – 3.93 UpdraftPlus - Backup/Restore: by UpdraftPlus.Com DavidAnderson – 2.16.2.24 WooCommerce Stripe Gateway: by WooCommerce – 6.4.1 Germanized for WooCommerce: by vendidero – 3.9.7 WooCommerce Multilingual & Multicurrency: by OnTheGoSystems – 5.0.1 WooCommerce PDF Invoices & Packing Slips: by WP Overnight – 2.15.0 WooCommerce PDF Invoices & Packing Slips Professional: by Ewout Fernhout – 2.6.6 WooCommerce Side Cart Premium: by XootiX – 2.2 WooCommerce: by Automattic – 6.6.0 WPML Media: by OnTheGoSystems – 2.7.0 WPML String Translation: by OnTheGoSystems – 3.2.1 ### Inactive Plugins (8) ### Advanced WordPress Reset: by Younes JFR. – 1.5 All in One SEO: by All in One SEO Team – 4.2.1.1 GAinWP Google Analytics Integration for WordPress: by IntelligenceWP – 5.4.6 Google Analytics for WordPress by MonsterInsights: by MonsterInsights – 8.6.0 Mailchimp for WooCommerce: by Mailchimp – 2.6.2 Password Protected: by WPExperts – 2.5.2 Pixel Manager for WooCommerce: by SweetCode – 1.17.9 WooCommerce Admin: by WooCommerce – 3.3.2 ### Settings ### API Enabled: – Force SSL: – Currency: EUR (€) Currency Position: right_space 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) Connected to WooCommerce.com: – Enforce Approved Product Download Directories: – ### WC Pages ### Shop base: #7 - /shop/ Cart: #8 - /warenkorb/ Checkout: #9 - /checkout/ My account: #10 - /mein-konto/ Terms and conditions: #70 - /agb/ ### Theme ### Name: AL Mediadesign Theme Version: 1.2 Author URL: https://www.adrianlacheta.de 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: ? Not declared ### Templates ### Overrides: al_theme/woocommerce/checkout/form-checkout.php al_theme/single-product.php version - is out of date. The core version is 1.6.4 Outdated Templates: ? Learn how to update ### Admin ### Enabled Features: activity-panels analytics coupons customer-effort-score-tracks experimental-products-task experimental-import-products-task experimental-fashion-sample-products homescreen marketing mobile-app-banner navigation onboarding onboarding-tasks remote-inbox-notifications remote-free-extensions payment-gateway-suggestions shipping-label-banner subscriptions store-alerts transient-notices wc-pay-promotion wc-pay-welcome-page wc-pay-subscriptions-page Disabled Features: minified-js settings Daily Cron: ? Next scheduled: 2022-06-16 22:33:31 +02:00 Options: ? Notes: 93 Onboarding: completed ### Action Scheduler ### Canceled: 1 Oldest: 2022-05-25 11:03:11 +0200 Newest: 2022-05-25 11:03:11 +0200 Complete: 176 Oldest: 2022-05-16 03:19:30 +0200 Newest: 2022-06-15 23:26:55 +0200 Pending: 7 Oldest: 2022-06-16 23:25:16 +0200 Newest: 2022-06-16 23:25:16 +0200 ### Status report information ### Generated at: 2022-06-16 02:05:02 +02:00
Hi @adrianlacheta ,
Thanks for providing clarity on what you would like to achieve here.
Please could you walk me through the exact steps you are taking, to the point where you get the “0” so that I am able to see if I can replicate this on my end?
Thanks!
The
get_discount_total
method in the order class returns the difference betweensubtotal
andtotal
but how this is handled differs slightly according to whether your tax is set to inclusive or exclusive.This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.
I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.
You can also visit the WooCommerce Facebook group or the
#developers
channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.Seems we’ve not had additional inputs on this thread. Thus, we encourage you to make use of the above resources.
I’ll go ahead and mark the thread as resolved but please feel free to create a new thread if you have further questions.
-
This reply was modified 2 years, 9 months ago by
- The topic ‘The function get_discount_total always returns 0 (zero)’ is closed to new replies.