Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

woocommerce order totals wont update when adding fee #26543

Closed
erikdemarco opened this issue May 19, 2020 · 10 comments · Fixed by #37845 · May be fixed by saucal/woocommerce-contribution#1
Closed

woocommerce order totals wont update when adding fee #26543

erikdemarco opened this issue May 19, 2020 · 10 comments · Fixed by #37845 · May be fixed by saucal/woocommerce-contribution#1
Assignees
Labels
Contributor Day - H1 2023 focus: order Issues related to orders. plugin: woocommerce Issues related to the WooCommerce Core plugin. priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. type: bug The issue is a confirmed bug.

Comments

@erikdemarco
Copy link
Contributor

erikdemarco commented May 19, 2020

I have this hook:

add_action( 'woocommerce_new_order', 'woocommerce_new_order_add_bacs_unique_code',  10, 1  );
function woocommerce_new_order_add_bacs_unique_code( $order_id ) {

    $order = wc_get_order( $order_id );
    $order_id = $order->get_id();
    $payment_method = $order->get_payment_method();

    //add 'unique code' fee only if its 'bank transfer'
    if($payment_method == 'bacs'){

        //generate unique_code (get last 3 digit from order id)
        $unique_code_name = 'Bacs Unique Code';
        $unique_code_amount = substr( (string)$order_id, -3 );

        $new_fee            = new stdClass();
        $new_fee->name      = esc_attr( $unique_code_name );
        $new_fee->amount    = (float) esc_attr( $unique_code_amount );
        $new_fee->tax_class = '';
        $new_fee->taxable   = false;

        $order->add_fee( $new_fee );

        $order->calculate_totals();
        $order->save();

    }
    
}

When order created via frontend and backend, its working properly.

But when order is created via REST API. The order-total is not correct. and get_shipping_total() become 0

@jonathansadowski
Copy link
Contributor

Hi @erikdemarco,

Thank you for submitting the issue. However, you didn’t fill out the details of the bug report template that we ask for. Without these details, we can’t fully evaluate this issue. Please provide us with the information requested, especially the expected behavior, so we could take a look further.

Describe the bug
A clear and concise description of what the bug is. Please be as descriptive as possible; issues lacking detail, or for any other reason than to report a bug, may be closed without action.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Screenshots
If applicable, add screenshots to help explain your problem.

Expected behavior
A clear and concise description of what you expected to happen.

Isolating the problem (mark completed items with an [x]):

  • I have deactivated other plugins and confirmed this bug occurs when only WooCommerce plugin is active.
  • This bug happens with a default WordPress theme active, or Storefront.
  • I can reproduce this bug consistently using the steps above.

WordPress Environment

``` Copy and paste the system status report from **WooCommerce > System Status** in WordPress admin. ```

@jonathansadowski jonathansadowski added the needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. label Jun 2, 2020
@erikdemarco
Copy link
Contributor Author

erikdemarco commented Jun 3, 2020

Describe the bug
I want to add fee to every new order which use 'bacs' as payment method. So I call add_fee() in 'woocommerce_new_order' hook.
The result is ok when order is created via front end. but when order is created via rest api, The order-total is not correct. and shipping total become 0.

To Reproduce

  1. use this script
add_action( 'woocommerce_new_order', 'woocommerce_new_order_add_bacs_unique_code',  10, 1  );

function woocommerce_new_order_add_bacs_unique_code( $order_id ) {

    $order = wc_get_order( $order_id );
    $order_id = $order->get_id();
    $payment_method = $order->get_payment_method();

    //add 'unique code' fee only if its 'bank transfer'
    if($payment_method == 'bacs'){

        //generate unique_code (get last 3 digit from order id)
        $unique_code_name = 'Bacs Unique Code';
        $unique_code_amount = substr( (string)$order_id, -3 );

        $new_fee            = new stdClass();
        $new_fee->name      = esc_attr( $unique_code_name );
        $new_fee->amount    = (float) esc_attr( $unique_code_amount );
        $new_fee->tax_class = '';
        $new_fee->taxable   = false;

        $order->add_fee( $new_fee );

        $order->calculate_totals();
        $order->save();

    }
    
}
  1. create order via rest api with payment method 'bacs' with this additional data:
//payment method
$data['payment_method'] = "bacs";

//shipping
$data['shipping_lines'][] = [
    'method_id' => 'other',
    'method_title' => 'Custom Shipping',
    'total' => (string) '5000'
];
  1. now go to the admin panel and see the order total and shipping cost. is it correct?

Screenshots
If applicable, add screenshots to help explain your problem.

Expected behavior
order total and shipping total should be correct.

Isolating the problem (mark completed items with an [x]):

  • I have deactivated other plugins and confirmed this bug occurs when only WooCommerce plugin is active.
  • This bug happens with a default WordPress theme active, or Storefront.
  • I can reproduce this bug consistently using the steps above.

WordPress Environment

``` ` ### WordPress Environment ###

WordPress address (URL): https://xxxxx.com
Site address (URL): https://xxxxxxx.com
WC Version: 4.1.1
REST API Version: ✔ 1.0.7
WC Blocks Version: ✔ 2.5.16
Action Scheduler Version: ✔ 3.1.6
WC Admin Version: ✔ 1.1.3
Log Directory Writable: ✔
WP Version: 5.4.1
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.29 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/1.1.1g
PHP Version: 7.3.18-1+ubuntu18.04.1+deb.sury.org+1
PHP Post Max Size: 32 MB
PHP Time Limit: 30
PHP Max Input Vars: 1000
cURL Version: 7.58.0
OpenSSL/1.1.1g

SUHOSIN Installed: –
MySQL Version: 5.5.5-10.4.13-MariaDB-1:10.4.13+maria~bionic
Max Upload Size: 32 MB
Default Timezone is UTC: ✔
fsockopen/cURL: ✔
SoapClient: ✔
DOMDocument: ✔
GZip: ✔
Multibyte String: ✔
Remote Post: ✔
Remote Get: ✔

Database

WC Database Version: 4.1.1
WC Database Prefix: wp_
Total Database Size: 10.30MB
Database Data Size: 6.58MB
Database Index Size: 3.72MB
wp_woocommerce_sessions: Data: 0.17MB + 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.17MB + Engine InnoDB
wp_woocommerce_order_items: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_order_itemmeta: Data: 0.05MB + Index: 0.05MB + Engine InnoDB
wp_woocommerce_tax_rates: Data: 0.02MB + Index: 0.08MB + Engine InnoDB
wp_woocommerce_tax_rate_locations: Data: 0.02MB + Index: 0.05MB + 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.03MB + Engine InnoDB
wp_woocommerce_payment_tokenmeta: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
wp_woocommerce_log: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_actionscheduler_actions: Data: 0.06MB + Index: 0.11MB + 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: 0.05MB + Index: 0.03MB + Engine InnoDB
wp_admin_columns: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_atum_order_itemmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_atum_order_items: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_atum_product_data: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
wp_commentmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_comments: Data: 0.05MB + Index: 0.09MB + Engine InnoDB
wp_erp_acct_bills: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_bill_account_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_bill_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_cash_at_banks: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_chart_of_accounts: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_currency_info: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_expenses: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_expense_checks: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_expense_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_financial_years: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_invoices: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_invoice_account_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_invoice_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_invoice_details_tax: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_invoice_receipts: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_invoice_receipts_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_journals: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_journal_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_ledgers: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_ledger_categories: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_ledger_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_ledger_settings: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_opening_balances: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_payment_methods: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_pay_bill: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_pay_bill_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_pay_purchase: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_pay_purchase_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_people_account_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_people_trn: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_people_trn_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_products: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_product_categories: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_product_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_product_types: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_purchase: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_purchase_account_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_purchase_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_taxes: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_tax_agencies: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_tax_agency_details: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_tax_categories: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_tax_cat_agency: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_tax_pay: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_transfer_voucher: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_trn_status_types: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_acct_voucher_no: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_attendance_date_shift: Data: 0.02MB + Index: 0.08MB + Engine InnoDB
wp_erp_attendance_log: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
wp_erp_attendance_shifts: Data: 0.02MB + Index: 0.08MB + Engine InnoDB
wp_erp_attendance_shift_user: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
wp_erp_attendence_shift_generated_to: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_audit_log: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
wp_erp_company_locations: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_erp_crm_activities_task: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_erp_crm_contact_group: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_contact_subscriber: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
wp_erp_crm_customer_activities: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
wp_erp_crm_customer_companies: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_erp_crm_deals: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_activities: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_activity_types: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_agents: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_attachments: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_competitors: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_emails: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_lost_reasons: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_notes: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_participants: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_pipelines: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_pipeline_stages: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_deals_stage_history: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_save_email_replies: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_crm_save_search: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_holidays_indv: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_hr_announcement: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
wp_erp_hr_dependents: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_erp_hr_depts: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_hr_designations: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_hr_education: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_erp_hr_employees: Data: 0.02MB + Index: 0.08MB + Engine InnoDB
wp_erp_hr_employee_history: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_erp_hr_employee_notes: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_hr_employee_performance: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_erp_hr_holiday: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_hr_leaves: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_erp_hr_leave_entitlements: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_erp_hr_leave_policies: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_hr_leave_requests: Data: 0.02MB + Index: 0.08MB + Engine InnoDB
wp_erp_hr_work_exp: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_erp_peoplemeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_erp_peoples: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
wp_erp_people_types: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_erp_people_type_relations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_erp_user_leaves: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_erp_wc_orders: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_erp_wc_order_product: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_gf_addon_feed: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_gf_draft_submissions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_gf_entry: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_gf_entry_meta: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
wp_gf_entry_notes: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_gf_form: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_gf_form_meta: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_gf_form_revisions: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_gf_form_view: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_hrm_attendance: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_designation: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_financial_year: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_formula: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_holiday: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_job_category: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_job_title: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_language: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_leave: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_leave_type: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_location: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_hrm_notice: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_office_time: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_pay_grade: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_personal_education: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_personal_language: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_hrm_personal_skill: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_relation: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_salary: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_salary_group: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_time_shift: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_hrm_work_experience: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_links: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_options: Data: 2.38MB + Index: 0.08MB + Engine InnoDB
wp_podsrel: Data: 0.02MB + Index: 0.06MB + Engine InnoDB
wp_postmeta: Data: 0.16MB + Index: 0.17MB + Engine InnoDB
wp_posts: Data: 0.06MB + Index: 0.06MB + Engine InnoDB
wp_shipment_batch_process: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_stream: Data: 0.02MB + Index: 0.13MB + Engine InnoDB
wp_stream_meta: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
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_ualp_user_activity: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_usermeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_users: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
wp_wc_admin_notes: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_wc_admin_note_actions: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_wc_category_lookup: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_wc_customer_lookup: Data: 0.02MB + Index: 0.05MB + Engine InnoDB
wp_wc_download_log: Data: 0.02MB + Index: 0.05MB + 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.11MB + Engine InnoDB
wp_wc_order_stats: Data: 0.02MB + Index: 0.08MB + Engine InnoDB
wp_wc_order_tax_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_wc_product_meta_lookup: Data: 0.02MB + Index: 0.09MB + Engine InnoDB
wp_wc_tax_rate_classes: Data: 0.02MB + Index: 0.02MB + Engine InnoDB
wp_wc_webhooks: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_woocommerce_square_customers: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_woo_shippment_provider: Data: 0.06MB + Index: 0.00MB + Engine InnoDB
wp_woo_square_integration_deleted_data: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_woo_square_integration_logs: Data: 0.02MB + Index: 0.00MB + Engine InnoDB
wp_wsal_metadata: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_wsal_occurrences: Data: 0.02MB + Index: 0.03MB + Engine InnoDB
wp_wsal_options: Data: 0.02MB + Index: 0.02MB + Engine InnoDB

Post Type Counts

_pods_field: 1
_pods_pod: 1
attachment: 19
page: 9
post: 4
product: 17
revision: 5
shop_order: 41
shop_order_refund: 1
test_cpt: 4

Security

Secure connection (HTTPS): ✔
Hide errors from visitors: ❌Error messages should not be shown to visitors.

Active Plugins (12)

Adminimize: by Frank Bültge – 1.11.6
AfterShip - WooCommerce Tracking: by AfterShip – 1.9.18 – Installed version not tested with active version of WooCommerce 4.1.1
Easy Theme and Plugin Upgrades: by Chris Jean – 2.0.1
WP ERP - Deals: by weDevs – 1.1.1
WP ERP - WooCommerce: by weDevs – 1.3.1 – Installed version not tested with active version of WooCommerce 4.1.1
WP ERP: by weDevs – 1.5.16
Pods - Custom Content Types and Fields: by Pods Framework Team – 2.7.20
WooCommerce Bulk Stock Management: by WooCommerce – 2.2.25
WooCommerce PDF Invoices & Packing Slips: by Ewout Fernhout – 2.4.8 – Installed version not tested with active version of WooCommerce 4.1.1
WooCommerce Print Invoices/Packing Lists: by SkyVerge – 3.8.4
WooCommerce Shipment Tracking: by WooCommerce – 1.6.22
WooCommerce: by Automattic – 4.1.1

Inactive Plugins (5)

Advanced Shipment Tracking for WooCommerce: by zorem – 2.9.9.4 – Installed version not tested with active version of WooCommerce 4.1.1
PublishPress Capabilities: by PublishPress – 1.9.6
User Role Editor: by Vladimir Garagulya – 4.53.1
WP ERP - Attendance: by weDevs – 2.0.0
WP Security Audit Log: by WP White Security – 4.0.2

Settings

API Enabled: –
Force SSL: –
Currency: IDR (Rp)
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)

Connected to WooCommerce.com: –

WC Pages

Shop base: #6 - /shop/
Cart: #7 - /cart/
Checkout: #8 - /checkout/
My account: #9 - /my-account/
Terms and conditions: ❌ Page not set

Theme

Name: Storefront Child Theme
Version: 1.0.0
Author URL: http://stuartduff.com
Child Theme: ✔
Parent Theme Name: Storefront
Parent Theme Version: 2.5.5 – 2.5.7 is available
Parent Theme Author URL: https://woocommerce.com/
WooCommerce Support: ✔

Templates

Overrides: –

Action Scheduler

Complete: 107
Oldest: 2020-05-03 10:47:08 +0000
Newest: 2020-06-02 04:45:28 +0000

Pending: 1
Oldest: 2020-06-03 04:45:28 +0000
Newest: 2020-06-03 04:45:28 +0000

`

</details>

@erikdemarco
Copy link
Contributor Author

erikdemarco commented Jun 7, 2020

@jonathansadowski im pretty sure 90% its a bug. I have tried every possible combination this past 5days with no success.

here's what i found:
This is not 'woocommerce_new_order' bug. Because I tried to hook on different hook such as woocommerce_update_order / woocommerce_after_order_object_save / woocommerce_before_order_object_save. All with the same result.

here's my newest code, this code works flawlessly when order created via frontend (checkout) and via backend (add order from admin panel). But when order created via restapi. the order totals is never gets updated (wrong value).

add_action( 'woocommerce_after_order_object_save', 'woocommerce_after_order_object_save_update_bacs_unique_code', 10, 1  );
function woocommerce_after_order_object_save_update_bacs_unique_code( $order ) {

    $order_id = $order->get_id();
    $order = wc_get_order($order_id);     //get the freshest order data (to prevent fee inputed twice)
    $payment_method = $order->get_payment_method();
    $unique_code_name = 'Unique Code';


    //check if 'Unique Code' fee exist
    $unique_code_fee_is_exist = 0;   
    foreach ( (array) $order->get_fees() as $fee_id => $fee) {
        $fee_name = $fee->get_name();
        if( $fee_name == $unique_code_name ){
            $unique_code_fee_is_exist = 1;
            $unique_code_fee_item_key = $fee_id;
        }
    }

    //add 'unique code' fee for bacs
    if($payment_method == 'bacs' && $unique_code_fee_is_exist == 0){

        //generate unique_code (get last 3 digit from order id)
        $unique_code_amount = substr( (string)$order_id, -3 );

        $fee   = new WC_Order_Item_Fee();
        $fee->set_props(
            array(
                'name'       => $unique_code_name,
                'tax_status' => 'none',
                'total'      => $unique_code_amount,
                'tax_class'  => '',
            )
        );
        $order->add_item( $fee );
        $order->calculate_totals();
        //$order->save();

    }

    //remove 'unique code' fee for non-bacs
    if($payment_method !== 'bacs' && $unique_code_fee_is_exist == 1 && isset($unique_code_fee_item_key) ){
        wc_delete_order_item($unique_code_fee_item_key);
        $order = wc_get_order( $order_id ); //this is needed after 'wc_delete_order_item', if not it will hangs
        $order->calculate_totals();
        //$order->save();

    }


}

@erikdemarco erikdemarco changed the title "woocommerce_new_order" BUG woocommerce order totals wont update when adding fee Jun 7, 2020
@peterfabian peterfabian removed the needs: author feedback The issue/PR needs a response from any of the parties involved in the issue. label Jul 1, 2020
@peterfabian
Copy link
Contributor

Sorry for the delay in response, @erikdemarco . We'll try to reproduce on our end and assign correct priority.

@juliaamosova juliaamosova added the needs: developer feedback Issues that need feedback from one of the WooCommerce Core developers. label Sep 11, 2020
@rrennick
Copy link
Contributor

The test code worked for me.

REST response from POSTing the order

  "line_items": [
    {
      "id": 7,
      "name": "Test Variable - Small, Square",
      "product_id": 19,
      "variation_id": 20,
      "quantity": 1,
      "tax_class": "",
      "subtotal": "10.00",
      "subtotal_tax": "0.00",
      "total": "9.00",
      "total_tax": "0.00",
      "taxes": [],
      "meta_data": [
        {
          "id": 55,
          "key": "pa_size",
          "value": "large"
        },
        {
          "id": 56,
          "key": "shape",
          "value": "Square"
        }
      ],
      "sku": "",
      "price": 9
    }
  ],
  "tax_lines": [],
  "shipping_lines": [
    {
      "id": 8,
      "method_title": "Shipping",
      "method_id": "other",
      "instance_id": "0",
      "total": "50.00",
      "total_tax": "0.00",
      "taxes": [],
      "meta_data": []
    }
  ],
  "fee_lines": [
    {
      "id": 6,
      "name": "Bacs Unique Code",
      "tax_class": "0",
      "tax_status": "taxable",
      "amount": "",
      "total": "46.00",
      "total_tax": "0.00",
      "taxes": [],
      "meta_data": []
    }
  ],
  "coupon_lines": [
    {
      "id": 9,
      "code": "friday10",
      "discount": "1",
      "discount_tax": "0",
      "meta_data": []
    }
  ],

POST body

"payment_method":"bacs",
"meta_data":[],
"line_items":[{"product_id":19,"variation_id":20,"quantity":1}],
"tax_lines":[],
"shipping_lines":[{"method_title":"Shipping","method_id":"other","instance_id":"0","total":"50.00","total_tax":"0.00"}],
"coupon_lines":[{"code":"friday10","discount":"2","discount_tax":"0"}]

Order in dashboard
Screen Shot 2020-10-22 at 2 09 59 PM

@erikdemarco I'll close the issue. If you can provide steps to reproduce we will be happy to reopen.

@rrennick rrennick reopened this Oct 22, 2020
@rrennick
Copy link
Contributor

@erikdemarco Sorry, I missed that the order totals don't show the amounts. If I click the recalculate button is does include the shipping.

@rrennick rrennick added type: bug The issue is a confirmed bug. and removed needs: developer feedback Issues that need feedback from one of the WooCommerce Core developers. labels Oct 22, 2020
@erikdemarco
Copy link
Contributor Author

The test code worked for me.

REST response from POSTing the order

  "line_items": [
    {
      "id": 7,
      "name": "Test Variable - Small, Square",
      "product_id": 19,
      "variation_id": 20,
      "quantity": 1,
      "tax_class": "",
      "subtotal": "10.00",
      "subtotal_tax": "0.00",
      "total": "9.00",
      "total_tax": "0.00",
      "taxes": [],
      "meta_data": [
        {
          "id": 55,
          "key": "pa_size",
          "value": "large"
        },
        {
          "id": 56,
          "key": "shape",
          "value": "Square"
        }
      ],
      "sku": "",
      "price": 9
    }
  ],
  "tax_lines": [],
  "shipping_lines": [
    {
      "id": 8,
      "method_title": "Shipping",
      "method_id": "other",
      "instance_id": "0",
      "total": "50.00",
      "total_tax": "0.00",
      "taxes": [],
      "meta_data": []
    }
  ],
  "fee_lines": [
    {
      "id": 6,
      "name": "Bacs Unique Code",
      "tax_class": "0",
      "tax_status": "taxable",
      "amount": "",
      "total": "46.00",
      "total_tax": "0.00",
      "taxes": [],
      "meta_data": []
    }
  ],
  "coupon_lines": [
    {
      "id": 9,
      "code": "friday10",
      "discount": "1",
      "discount_tax": "0",
      "meta_data": []
    }
  ],

POST body

"payment_method":"bacs",
"meta_data":[],
"line_items":[{"product_id":19,"variation_id":20,"quantity":1}],
"tax_lines":[],
"shipping_lines":[{"method_title":"Shipping","method_id":"other","instance_id":"0","total":"50.00","total_tax":"0.00"}],
"coupon_lines":[{"code":"friday10","discount":"2","discount_tax":"0"}]

Order in dashboard
Screen Shot 2020-10-22 at 2 09 59 PM

@erikdemarco I'll close the issue. If you can provide steps to reproduce we will be happy to reopen.

Hi @rrennick this is exactly the problem. Please see your posted screenshot once again.

Product total: $9
"Bacs unique code" fee: $46
Shipping fee: $50

So the order total should be $105. In your screenshot the order total shows wrong value ($59)

@rrennick
Copy link
Contributor

So the order total should be $105. In your screenshot the order total shows wrong value ($59)

Right, if you click recalculate the total is updated and correct. The issue is it should be correct without needing to have that button click.

@ObliviousHarmony ObliviousHarmony added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Feb 21, 2022
@peterfabian peterfabian added priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. focus: order Issues related to orders. labels May 5, 2022
@alexiglesias31
Copy link
Contributor

Hey ! I'd like to pick this one up for contributor's day 2023.

@msaggiorato
Copy link
Contributor

Hey team, been collaborating with @alexiglesias31 here on the issue for the past couple hours, and even tho the PR #37845 does fix the issue for the code posted here, there seems to be an issue (or several) with how the woocommerce_new_order gets called.

I believe the underlying problem should be addressed. Let me elaborate.

Background

Whenever WC_Order->save() gets called for the first time (with some exceptions), woocommerce_new_order gets called.

Research

Regular Checkout

This is the baseline for how the order should be created. Most of the calculations are done in the WC_Cart class, and then transferred over. No calculations are done on the WC_Order object. There's no calls to WC_Order->calculate_totals.

As explained on the issue, the code works under these circumstances.

Admin Created Order

There's a difference here between HPOS and CPT data stores.

CPT: Creates the order as autodraft. Code calls WC_Order->calculate_totals whenever it adds a new item, fee, or shipping method. woocommerce_new_order is prevented to be called if the post is in auto-draft. Tho woocommerce_update_order gets called on the order before woocommerce_new_order is ever called for thar particular order_id.

This is our first inconsistence here.. Semantically, I believe woocommerce_new_order should never be called on an order that has already got calls to woocommerce_update_order.

HPOS: From the moment you open the order editor, woocommerce_new_order gets called, even without any items on it. It's an empty order at that point.

Second problem here.. woocommerce_new_order gets called on an order that is not finished created, and can never be fully created. During checkout, this hook has the order fully created, and is practically the same as hooking into woocommerce_checkout_order_created. The use of woocommerce_new_order (as intended in the reported issue above) works for REST API, and Admin generated orders (at least without HPOS).

REST API Created Order

Now here the object is created with a pending status (with CPT Data Store), and WC_Order->save() gets called several times here:

  • DuringWC_Order->calculate_taxes (update_taxes specifically)
  • DuringWC_Order->calculate_totals (after it has called calculate_taxes above)
  • During WC_REST_Orders_Controller->save_object
  • And maybe more places we didn't find.

Third problem here. Again, woocommerce_new_order gets called early in the process (not as early as in the admin with HPOS, but still early). Order is not yet fully created, and some data sent to the API is not yet taken into account.

Conclusion

All of these inconsistencies about how the woocommerce_new_order hook works, make it unpredictable to work with. People may have handled this in several ways over the years, but it doesn't mean that there's no problem.

IMO this hook should be called right after the order is created in a "finished" state based on wherever it got the input.

  • Checkout -> All data from cart is processed, and order is created. If there's errors, and order is refreshed (recreated), hook should be called again on the same order.
  • Admin -> Order is completely created. Should be in draft before that point, unless you specifically want to create it as pending, in which case, the hook would be called on an order with the "pending" status.
  • REST API -> Order is fully created, and all input from the request is handled.

This requires refactors in a lot of places that we couldn't get to in the allocated timeframe for the contributor day. But hopefully this shed some light into the issue as far as the underlying reason why this happen.

To think about all the implications of making the changes mentioned here in this post is something that would take a lot of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Contributor Day - H1 2023 focus: order Issues related to orders. plugin: woocommerce Issues related to the WooCommerce Core plugin. priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. type: bug The issue is a confirmed bug.
Projects
8 participants