When using hivepress/v1/models/listing/update_status, setting and saving attributes does not persist.
Steps to reproduce
Example from listing component: after renewal of listing, when the expired time is (re-)set and saved:
if ( $expiration_period && ! $listing->get_expired_time() ) {
// Set expiration time.
$listing->set_expired_time( time() + $expiration_period * DAY_IN_SECONDS )->save_expired_time();
}
This line is reached (when status goes from draft to pending when moderation is activated), and also when listing is published after verification.
Actual result
The attribute is not saved, the expiration date is blank in the admin edit listing form.
Expected result
Id expect the expiration date to be updated.
Extra details
I actually happen to notice that whenever I tried to add some custom code on status change which does save listing attributes, it works only when going from ‘auto-draft’ to another status.
We tested the hook and it works correctly. This hook is an alias of the WordPress post status change hook, so everything should be fine.
If the attribute isn’t being saved, it’s most likely that some code is running later and overwriting the attribute value. Please make sure that your conditions for the old and new status are correct, and check that there’s no other code, either with a higher priority or within the framework, that could logically overwrite your attribute.
Just to be clear, the hook is triggered and the status check (old and new) works correctly. Only saving the listing attributes does not work.
Have you tested the hook for status going from ‘draft‘ to ‘pending’ or ‘draft‘ to ‘publish’ status or ‘pending‘ to ‘draft’?
This is when the attributes are not saved. I gave the example of the ‘expired time’ attribute, but it also does not work with any other attributes, so it is not an overwriting issue.
Sorry for pushing but I really don’t see something from my codebase that could interfere with that.
Clicking Hide and then Unhide link for the listing (“draft” to “publish”) sets the attribute value. Please note that there are no draft/pending and pending/draft status changes in HivePress flows, unless these are manual changes in WordPress dashboard. Pending listings are usually moved to Trash (listing rejected) or Published (listing approved). If you mean listing renewals with Paid Listings, there are multiple status changes, please try logging status with error_log to check if the added condition runs the code.
However, the ‘price’ attribute in the listing edit admin remains 34, and obviously when I trigger the status update again the same logs appear: 34 then 23.
I trigger those by manually changing status on the listing edit admin board and saving (from pending to draft).
I guess that is the issue then. The form submission itself could actually overwrite the values? Meaning that the status change trigger happens before the form values are being saved, is that the case?
Yes, if you change the status on back-end and click the Update button, then all the fields available within this page are also submitted so they probably override the values. For example, if you have the attribute field on the same page, the status change hook probably fires earlier, and after it all the field values are saved for the listing. This may work if the attribute field is hidden in the listing form, or if you change the listing status via Quick Edit without opening the listing form.