• Resolved tccand

    (@tccand)


    Hello,

    Lets say we have a product that has a value of 100 (prices include VAT 9%).

    If I create an order with this product by adding it in the cart and then I apply a coupon of 10% on the cart
    1. The coupon discount will be saved as 9.17 (WC_Order_Item_Coupon->get_discount())

    If I create an order and then I apply a coupon after checkout.
    2. The coupon discount will be saved as 9.174312 (WC_Order_Item_Coupon->get_discount())

    There shouldn’t be a difference if the coupon is added before or after checkout.

    I would like to get 9.174312 for the first case as well. Is there a better way of retrieving the coupon discount?

    Thank you!


Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @tccand,

    The difference you see in the coupon discount amounts is due to how WooCommerce calculates discounts. When a coupon is applied at the cart level, the discount is calculated based on the product price, including VAT, which is 9.17. However, when the coupon is applied after checkout, the discount is calculated on the product price, excluding VAT, resulting in 9.174312.

    Can you please navigate to WooCommerce > Settings > enable the “Calculate coupon discounts sequentially” option and see whether it works or not?

    Let us know how that goes. Looking forward to hearing from you!

    Thread Starter tccand

    (@tccand)

    Hello @shameemreza,

    I tried enabling “Calculate coupon discounts sequentially”. It made no difference.

    I am using WordPress 6.5-RC2 and Woocommerce 8.7.0-rc.1

    I also have “Round tax at subtotal level, instead of rounding per line” enabled.

    Is there anything else I can try?

    Thank you!

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @tccand,

    This is unusual and I couldn’t replicate this using stable released version.

    As you’re using a release candidate version of both WordPress and WooCommerce, it’s possible that this could be a bug in those versions. I recommend installing stable versions of WordPress and WooCommerce to see if the issue persists.

    If the issue continues, please provide us with more information or screenshots of your coupon settings for further investigation.

    Looking forward to hearing from you!

    Thread Starter tccand

    (@tccand)

    Hello again @shameemreza,
    I can confirm that the error can be reproduced on WordPress 6.4.3 and Woocommerce 8.7.0

    After the order is created I use this PHP code to check for the discount value:

    $data ? ? ? ? ? ? ?= $order->get_data();
    $coupons ? ? ? ? ? = $data[‘coupon_lines’];
    foreach ( $coupons as $item ) {
    error_log(print_r($item->get_discount(),true));
    }

    Adding a coupon from wc-orders page will give different results than checkout page or cart page.

    Thank you!

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @tccand,

    I am still unable to replicate the issue. I created a 10% coupon and applied it on the cart, checkout, and even in the admin panel from the order edit page, but everything looks fine, as expected.

    Here is the order details while I applied the coupon on cart page.

    And another test I did applying the coupon on the checkout page.

    However, as you’re using a custom code to check for the discount value, the discrepancy might also be due to how the discount is calculated in the code. The WC_Order_Item_Coupon->get_discount() function retrieves the discount amount for the coupon, but the value might vary depending on when and how it’s applied.

    For better assistance, can you share a video showing the issue on your end. Also share a copy of the following:

    • Fatal Error log: Share a copy of any fatal error log found under WooCommerce → Status → Logs.
    • Any custom code or settings you are using.

    You could copy and paste your reply or share it via Mozilla Community Pastebin and share the link here.

    I hope this provides some clarity. Please let us know if you have any other questions!

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @tccand,

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. Hopefully, you were able to find a solution to your problem!

    If you have further questions, please feel free to open a new topic.

    Thanks!

    Thread Starter tccand

    (@tccand)

    Hello @shameemreza,

    Sorry for the delayed response.

    I am not sure I understand what you were trying to say here:
    “The?WC_Order_Item_Coupon->get_discount()?function retrieves the discount amount for the coupon, but the value might vary depending on when and how it’s applied.”

    There are 2 cases for my problem:
    1. adding a coupon from the admin side of wordpress.
    2. adding a coupon from the frontend site.

    get_discount() should return the same value after the order has been created for both of these cases.
    The problem is not theget_discount() function. The coupon data that is stored in the database is wrong.

    One way of checking this is to look in the database at the _woocommerce_order_itemmeta table:
    DB example
    Case 1:
    6190 | 887 | coupon_info | [204,”brmg”,”percent”,10]
    6189 | 887 | discount_amount_tax | 0.652294
    6188 | 887 | discount_amount | 7.247706

    Case 2:
    6187 | 886 | coupon_info | [204,”brmg”,”percent”,10]
    6186 | 886 | discount_amount_tax | 0.65
    6185 | 886 | discount_amount | 7.25

    Another way is to do a get order on the REST API:
    Case 1:
    “couponLines”: [
    {
    “id”: 892,
    “code”: “brmg”,
    “discount”: “7.247706”,
    “discountTax”: “0.652294”,
    “metaData”: [
    {
    “id”: 6220,
    “key”: “coupon_info”,
    “value”: “[204,\”brmg\”,\”percent\”,10]”,
    “displayKey”: “coupon_info”,
    “displayValue”: “[204,\”brmg\”,\”percent\”,10]”
    }
    ],
    “discountType”: “percent”,
    “nominalAmount”: 10,
    “freeShipping”: false
    }
    ],

    Case 2:
    “couponLines”: [
    {
    “id”: 891,
    “code”: “brmg”,
    “discount”: “7.25”,
    “discountTax”: “0.65”,
    “metaData”: [
    {
    “id”: 6217,
    “key”: “coupon_info”,
    “value”: “[204,\”brmg\”,\”percent\”,10]”,
    “displayKey”: “coupon_info”,
    “displayValue”: “[204,\”brmg\”,\”percent\”,10]”
    }
    ],
    “discountType”: “percent”,
    “nominalAmount”: 10,
    “freeShipping”: false
    }
    ],


    In both cases the order has the same items so the response should be the same.
    I look forward to your response.

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @tccand

    The difference in the discount_amount and discount_amount_tax values in the _woocommerce_order_itemmeta table and the REST API response could be due to a number of factors, including rounding errors, tax calculation settings, or even a potential bug in the system.

    However, I’ve tried replicating the issue you’re facing using the default setting and theme, but I haven’t had any luck so far. If you believe that this is a bug that our developer needs to fix, I’d recommend submitting a bug report here.

    For reference, this particular forum is meant for general support with the core functionality of WooCommerce itself. For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Thread Starter tccand

    (@tccand)

    We have the same problem reported here and here.

    Why is this thread tagged as resolved? In addition to that, the problem is by no means related to development or custom coding. It’s the core functionality that is not working properly.

    It’s not super easy to reproduce, since rounding errors can add up or compensate each other.

    • This reply was modified 6 months, 1 week ago by cutu234.
    • This reply was modified 6 months, 1 week ago by cutu234.
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Woocommerce differences between admin and cart coupons’ is closed to new replies.