Skip to content

AJAX: Favorite Products

The favorites feature has one AJAX action. This page documents its exact contract as implemented.

  • Action name: rwcv_mark_product_fav
  • Registered on wp_ajax_rwcv_mark_product_fav only. It is not registered for wp_ajax_nopriv_, because the button never renders for visitors and the handler writes user meta.

The frontend script is localized as rwcv_frontend_action with:

  • ajaxurl: the admin-ajax.php URL.
  • nonce: a nonce created with the action reign-wc-vendor-nonce.
  • read_more: the translatable “Read More” label used by the description toggle.

The script sends a POST request with these fields:

Field Value
action rwcv_mark_product_fav
product_id the product ID
product-action mark-fav to add, anything else to remove
nonce rwcv_frontend_action.nonce

Note the field name is product-action (with a hyphen), and the nonce is sent as nonce.

The handler reign_wcvendors_mark_product_favorite():

  1. Verifies the nonce against reign-wc-vendor-nonce. On failure it responds with wp_send_json_error() and HTTP 403.
  2. Requires a logged-in user. If not logged in, it responds with wp_send_json_error() and HTTP 401.
  3. On mark-fav, adds the product ID to the user’s rwcv_my_fav_products meta and adds the user ID to the product’s rwcv_fav_product_by_user meta. Otherwise it removes them.
  4. Responds with wp_send_json_success().

On success the payload is:

{ "success": true, "data": { "btn-action": "remove-fav" } }

btn-action is the action to use for the next click: remove-fav after adding, mark-fav after removing. On a rejected request the button state is left unchanged by the script.