Skip to content

Coupon can't be removed when added to cart via WC_AJAX #16133

@iMicknl

Description

@iMicknl

Prerequisites

  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate
  • The issue still exists against the latest master branch of WooCommerce
  • This is not a usage question (Those should be directed to the community, unless this is a question about a premium plugin in which you should use the helpdesk for official extensions or contact the author of 3rd party extensions)
  • I have attempted to find the simplest possible steps to reproduce the issue

I am writing a plugin where I check via an external API if a discount card is valid. If they deserve a discount I create a new coupon which I apply to the their cart via WC()->cart->add_discount(). This works like a charm, however users can't remove the coupon anymore. If I call this function not via Ajax (in footer.php for example) it works without any problems.

Steps to reproduce the issue

  1. Call WC()->cart->add_discount( $coupon_code ); via WC_AJAX or WP_AJAX
  2. Remove coupon from cart, which results in 'Coupon removed' message.
  3. Refresh page. Coupon is still applied.

Expected behavior and actual behavior

I was expecting that the coupon would be removed successfully when I remove it from the cart.

Environment

``` ### WordPress Environment ###

Home URL: https://webshop.dev
Site URL: https://webshop.dev
WC Version: 3.1.1
Log Directory Writable: ✔
WP Version: 4.8
WP Multisite: –
WP Memory Limit: 256 MB
WP Debug Mode: –
WP Cron: ✔
Language: en_US

Server Environment

Server Info: nginx/1.10.2
PHP Version: 7.1.6
PHP Post Max Size: 8 MB
PHP Time Limit: 30
PHP Max Input Vars: 1000
cURL Version: 7.51.0
SecureTransport

SUHOSIN Installed: –
Max Upload Size: 2 MB
Default Timezone is UTC: ✔
fsockopen/cURL: ✔
SoapClient: ✔
DOMDocument: ✔
GZip: ✔
Multibyte String: ✔
Remote Post: ✔
Remote Get: ✔

Database

WC Database Version: 3.1.1
WC Database Prefix: wp_
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 (2)

WooCommerce CJP discount: by codeconverters – 1.0.0
WooCommerce: by Automattic – 3.1.1

Settings

API Enabled: ✔
Force SSL: ✔
Currency: EUR (€)
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: #4 - /
Cart: #5 - /cart/
Checkout: #6 - /checkout/
My account: #7 - /my-account/

Theme

Name: Storefront
Version: 2.2.4
Author URL: https://woocommerce.com/
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: –

</details>

## Isolating the problem
- [x] This bug happens with only WooCommerce plugin active
- [x] This bug happens with a default WordPress theme active, or [Storefront](https://woocommerce.com/storefront/)
- [x] I can reproduce this bug consistently

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions