Skip to content
Advertisement

Error when removing a produсt from favorites (I created the wishlist system myself)

guys! I faced some issue again. For a project on WooCommerce I created the custom system favorite products. The default one did not suit us.I could implement the addition created this code:

Button for adding to the favorite list

JavaScript

The jQuery code

JavaScript

The code in function:

JavaScript

But I have not thought about the removal system. No, I thought that I could do everything the other way around. But it doesn’t work. This is how I decided to organize the removal of an item from favorites.

Delete button

JavaScript

The jQuery code for remove

JavaScript

And the function code to remove

JavaScript

But deleting doesn’t work. When I click on the delete button, Ajax return 400 error. Here is screenshot:

enter image description here

I tried but couldn’t figure out what was going on. And I suspect that the removal code has errors, but I just don’t see them. Please help me solve this issue.

Advertisement

Answer

Change your code for remove from favorites:

JavaScript

To this:

JavaScript

You use same actions to add and remove items.

And one more: when you use somethink from user input ($_POST, $_GET, etc) – you must to sanitize this data before use.

More: https://developer.wordpress.org/themes/theme-security/data-sanitization-escaping/#example-simple-input-field

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement