Filter Conditional Option

Pending

Import WP Pro Updated 5 days ago 10 Replies

Evelien asked 2 weeks ago on October 9, 2024 at 11:55 am

Hi James,

I was wondering if you could advise about next scenario.

We have an XML feed with properties that have the following condition:

If <Status> = ‘WITHDRAWN’ it skips the item. This works.

However, if an item first had <Status> ≠ ‘WITHDRAWN’ it has been imported. But if the next day it has <Status> = ‘WITHDRAWN’ how do I remove this from the system?

Is there a smart way to get this done?

With thanks,
Demian

J

James replied Support Agent

2 weeks ago on October 9, 2024 at 1:39 pm

Hi Demian,

Can you confirm if you have tried enabling the delete permission in the importer? this should delete any previous records that the importer has imported that no longer exist in the import file.

James

ET

Evelien replied

2 weeks ago on October 10, 2024 at 12:17 pm

HI James,

The records still exist in the file, past records should also not be removed. We just have the scenario that if a record has a different "status" after time it should not be part of the import anymore. But this status changes. 

So all status with "available" for example should be imported, but not the one with "withdrawn". Initially this works, but a status can change later on.

ET

Evelien replied

2 weeks ago on October 11, 2024 at 10:09 am

Hi James,

I re-read my case and it might be still unlcear. To give more calrity:

The records can be imported with this <status> field:

FOR_RENT
FOR_SALE

Over time they can have these <status>:

RENTED
SOLD
WITHDRAWN

The records that went from FOR_RENT/FOR_SALE towards RENTED/SOLD need to stay in the website. But the ones that become WITHDRAWN should not stay and should be trashed.

Is there a filter for this?

J

James replied Support Agent

2 weeks ago on October 11, 2024 at 8:23 pm

I think i understand, if you have the status being set to a importer template field then you could fetch that fields value, and if its being updated you can delete the record, and if its an insert you can skip the record, using the following code (changing the getValue line to match the status field, and remove any previous filtering you have to skipping the rented/sold/withdrawn status):

add_action('iwp/importer/mapper/before', function ($data) {

    /**
     * @var \ImportWP\Common\Importer\ParsedData $data
     */
    if ($data->getValue('post_title') == 'Post 1 Title') {

        // if data is an update, then we move the record to trash,
        // otherwise we just skip
        if ($data->isUpdate()) {
            wp_delete_post($data->getId());
        }

        throw new \ImportWP\Common\Importer\Exception\RecordUpdatedSkippedException("Record deleted.");
    }
});
ET

Evelien replied

1 week ago on October 14, 2024 at 1:01 pm

Hi James, Thank you. This is the field record: record: {/RealEstateProperty/PropertyInfo/Status}And it's to update ACF 'status_listing'How do I add this for $data?With thanks,Demian  
J

James replied Support Agent

1 week ago on October 15, 2024 at 8:47 pm

Are you importing that into a field? {/RealEstateProperty/PropertyInfo/Status}, if not i can look into a way to get this direct from the xml file.

ET

Evelien replied

6 days ago on October 16, 2024 at 11:44 am

Hi James,

Yes that field is being imported. Come from the ticket: https://helpdesk.importwp.com/ticket/c8f8c319-eb82-4f76-bae2-46c2c0d45941/

function iwphd888_status($status = '', $for_sale = '', $rent = '')
{
    if ($rent == 'true') {
        return 'Rent';
    } elseif ($for_sale == 'true') {
        return 'Sale';
    } elseif (!empty($status)) {
        return $status;
    }

    return '';
}
[iwphd888_status("{/RealEstateProperty/PropertyInfo/Status}","{/RealEstateProperty/Offer/IsForSale}","{/RealEstateProperty/Offer/IsForRent}")]
J

James replied Support Agent

6 days ago on October 16, 2024 at 8:21 pm

Can you please export and attach your importer settings to this ticket by going to Tools > Import WP > Settings / Tools > Import / Export and select and export your importer as a JSON file.

So that i can see which field is using that, maybe also take a screenshot of the field in the importer so i can  confirm.

James

ET

Evelien replied privately

6 days ago on October 16, 2024 at 8:48 pm

J

James replied Support Agent

5 days ago on October 17, 2024 at 4:05 pm

The following code will check to see if the value for custom fields row 1 has either 'RENTED', 'SOLD', or 'WITHDRAWN' it will be deleted.

add_action('iwp/importer/mapper/before', function ($data) {

    /**
     * @var \ImportWP\Common\Importer\ParsedData $data
     */
    if (in_array(trim($data->getValue('custom_fields.0.value', 'custom_fields')), ['RENTED', 'SOLD', 'WITHDRAWN'])) {

        // if data is an update, then we move the record to trash,
        // otherwise we just skip
        if ($data->isUpdate()) {
            wp_delete_post($data->getId());
        }

        throw new \ImportWP\Common\Importer\Exception\RecordUpdatedSkippedException("Record deleted.");
    }
});

James