• I have a project with automatic import which adjusts products and if there are no more in the file, changes them to out of stock instead of deleting them. But I also have other products in it from other suppliers. So I created a function. But it doesn’t work. Can anyone help me figure out what’s wrong?

    My Code..

    function prevent_out_of_stock_for_protected_categories($continue_processing, $post_id, $import_id) {
    $protected_category_slugs = array(‘marke-1’, ‘marke-2’);

    // Hole die Slugs der Kategorien des Produkts
    $categories = wp_get_post_terms($post_id, 'product_cat', array("fields" => "slugs"));
    
    // Falls das Produkt in einer geschützten Kategorie ist, verhindere das „Nicht auf Lager“-Setzen
    if (!empty(array_intersect($categories, $protected_category_slugs))) {
        return false; // Stoppt das Setzen auf "Nicht auf Lager"
    }
    
    return $continue_processing;

    }

    // Verhindert, dass Produkte aus geschützten Kategorien gelöscht oder deaktiviert werden
    add_filter(‘wp_all_import_is_post_to_delete’, ‘prevent_out_of_stock_for_protected_categories’, 10, 3);

    function prevent_stock_update_for_protected_categories($post_id, $data, $import_id) {
    $protected_category_slugs = array(‘marke-1’, ‘marke-2’);

    // Hole die Slugs der Kategorien des Produkts
    $categories = wp_get_post_terms($post_id, 'product_cat', array("fields" => "slugs"));
    
    // Falls das Produkt in einer geschützten Kategorie ist, verhindere das Lagerbestands-Update
    if (!empty(array_intersect($categories, $protected_category_slugs))) {
        delete_post_meta($post_id, '_stock_status');  // Entfernt das Stock-Status-Update
        delete_post_meta($post_id, '_stock'); // Entfernt das Lagerbestands-Update
    }

    }

    add_action(‘wp_all_import_before_update_post’, ‘prevent_stock_update_for_protected_categories’, 10, 3);

The topic ‘Help with custom function on “deletion”’ is closed to new replies.