skip to Main Content

I have an XML feed of producs, WordPress (last version), WpAllImport Pro (last version) and WooCommerce (last version) and I want to update my products periodically.

In order to update my products, I need to make something with the old ones that disappear from the XML.
WPAI have this option named “Delete products that are no longer present in your file” and a custom function for this: “function my_is_post_to_delete( $is_post_to_delete, $post_id, $import )”

Now, the problem is that my store is full of External products and for this type of product, I don’t have stock.

What I need is to change the product type from External to Simple and then to update his stock and then his status to “out of stock”.

I have the following code:

<?php

function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
    if ( $import->id == 2 ) {

        $product = wc_get_product( $post_id );
        $sku = $product->get_sku();
        wp_set_object_terms($post_id, 'simple','product_type');
        update_post_meta($post_id, '_manage_stock', 'yes');

        //$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );

        // Get an instance of the WC_Product object
        //$product = new WC_Product( $product_id );

        // Get product stock quantity and stock status
        //$stock_quantity = $product->get_stock_quantity();
        //$stock_status   = $product->get_stock_status();

        // Set product stock quantity (zero) and stock status (out of stock)
        //$product->set_stock_quantity();
        //$product->save();

        $woocmmerce_instance = new WC_Product( $post_id );
        $new_quantity=wc_update_product_stock( $woocmmerce_instance, $quantity);

        $product->set_stock_status('outofstock');

        // Save the data and refresh caches
        $product->save();

        //file_put_contents(get_home_path() .'/result.stef', $product->get_sku().'n' );
        return false;

    }
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );

?>

but I get an error:

CRITICAL Uncaught WC_Data_Exception: External products cannot be stock managed. in /home/etc/public_html/qcd/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-data.php:810

Stack trace:
#0 /home/etc/public_html/qcd/wp-content/plugins/woocommerce/includes/class-wc-product-external.php(120): WC_Data->error('product_externa...', 'External produc...')
#1 /home/etc/public_html/qcd/wp-content/uploads/wpallimport/functions.php(33): WC_Product_External->set_stock_status('outofstock')
#2 /home/etc/public_html/qcd/wp-includes/class-wp-hook.php(286): my_is_post_to_delete(true, '3329', Object(PMXI_Import_Record))
#3 /home/etc/public_html/qcd/wp-includes/plugin.php(208): WP_Hook->apply_filters(true, Array)
#4 /home/etc/public_html/qcd/wp-content/plugins/wp-all-import-pro/models/import/record.php(4169): apply_filters('wp_all_import_i...', true, '3329', Object(PMXI_Import_Record))
#5 /home/etc/public_html/qcd/wp-content/plugins/wp-all-import-pro/controllers/admin/import.php in /home/etc/public_html/qcd/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-data.php on line 810

As a final note:
I want to “Instead of delete my products” to make them out of stock.

2

Answers


  1. Chosen as BEST ANSWER
    <?php
    
    function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
    
        // Set the ID of your import
        if ( $import->id == 2 ) {
    
            $product = wc_get_product( $post_id );
            $sku = $product->get_sku();
    
            // Change product type to simple
            wp_set_object_terms($post_id, 'simple','product_type');
    
            // Make the product stock manageable
            update_post_meta($post_id, '_manage_stock', 'yes');
    
            // Set the product quantity to 0
            $product->set_stock_quantity();
    
            // Update the product status to Out of stock
            update_post_meta( $post_id, '_stock_status', wc_clean( 'outofstock' ) );
            wp_set_post_terms( $post_id, 'outofstock', 'product_visibility', true );
    
            // Delete cache
            wc_delete_product_transients( $post_id );
    
            return false;
    
        }
    }
    add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );
    
    ?>
    

  2. Have you tried updating the variation cache?

    update_post_meta( $product_id, '_stock_status', wc_clean( 'outofstock' ) );

    wp_set_post_terms( $product_id, 'outofstock', 'product_visibility', true );

    wc_delete_product_transients( $product_id );

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search