Last active
August 4, 2022 18:27
-
-
Save robindevitt/5a8a95ab3f59aa0261bc8d7197158a23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function action__product_stock_change( $continue, $post_id, $data, $import_id ) { | |
$firstImportId = 10; | |
$supplierPrefix = "A-"; | |
$secondImportId = 20; | |
// Set the input ID | |
if ( $import_id === $firstImportId ) { | |
// Change 'sku' to the column name that contains your SKU. | |
$product_sku = $data['sku']; | |
// if product sku doesn't start with the prefix return early | |
if( ! substr( $product_sku, 0, strlen( $supplierPrefix ) ) === $supplierPrefix ){ | |
return false; | |
} | |
// Change 'qty' to the column name that contains your QTY. | |
$product_qty = ( $data['stock'] ? $data['stock'] : '0'); | |
$product_id_by_sku = wc_get_product_id_by_sku( $product_sku ); | |
// Set up WooCommerce product object | |
$product = wc_get_product( $product_id_by_sku ); | |
// Process if product found | |
if ( $product ) { | |
$product_import = get_transient( 'product_import' ); | |
$product_import = ( $product_import ? $product_import : "" ); | |
// check if product and the product id by sku match, | |
// if they don't, set the product qty to 0 | |
if ( $product->get_id() !== $product_id_by_sku ) { | |
$product_qty = 0; | |
} | |
$array[] = $product->get_id(); | |
// Make changes to stock quantity and save | |
$product->set_manage_stock( true ); | |
$product->set_stock_quantity( $product_qty ); | |
$product->save(); | |
$product_import .= $product->get_id() . ","; | |
set_transient( 'product_import', $product_import, 3000 ); | |
return true; | |
} | |
// Don't update this post | |
return false; | |
} else if ( $import_id == $secondImportId ) { | |
// Change 'sku' to the column name that contains your SKU. | |
$product_sku = $data['sku']; | |
// if product sku starts with the prefix return early | |
if( substr( $product_sku, 0, strlen( $supplierPrefix ) ) === $supplierPrefix ){ | |
return false; | |
} | |
// Change 'qty' to the column name that contains your QTY. | |
$product_qty = ( $data['stock'] ? $data['stock'] : '0'); | |
$product_id_by_sku = wc_get_product_id_by_sku( $product_sku ); | |
// Set up WooCommerce product object | |
$product = wc_get_product( $product_id_by_sku ); | |
// Process if product found | |
if ( $product ) { | |
$product_import = get_transient( 'product_import' ); | |
$product_import = ( $product_import ? $product_import : "" ); | |
// check if product and the product id by sku match, | |
// if they don't, set the product qty to 0 | |
if ( $product->get_id() !== $product_id_by_sku ) { | |
$product_qty = 0; | |
} | |
$array[] = $product->get_id(); | |
// Make changes to stock quantity and save | |
$product->set_manage_stock( true ); | |
$product->set_stock_quantity( $product_qty ); | |
$product->save(); | |
$product_import .= $product->get_id() . ","; | |
set_transient( 'product_import', $product_import, 3000 ); | |
return true; | |
} | |
// Don't update this post | |
return false; | |
} | |
} | |
add_filter( 'wp_all_import_is_post_to_update', 'action__product_stock_change', 10, 4 ); | |
function after_xml_import( $import_id, $import ) { | |
$firstImportId = 10; | |
$secondImportId = 20; | |
// Only run if import ID is 5. | |
if ( $import_id === $firstImportId || $import_id === $secondImportId ) { | |
$product_import = get_transient( 'product_import' ); | |
if ( $product_import ){ | |
$array = explode( ",",$product_import ); | |
$args = array( | |
'limit' => -1, | |
); | |
$products = wc_get_products( $args ); | |
$count = count( $products ); | |
foreach( $products as $product ){ | |
if ( $product->get_type() === 'simple' && ! in_array( $product->get_id(), $array ) ) { | |
wc_update_product_stock( $product->get_id(), 0 ); | |
wc_update_product_stock_status( $product->get_id(), 'outofstock' ); | |
} | |
} | |
delete_transient( 'product_import' ); | |
} | |
} | |
} | |
add_action( 'pmxi_after_xml_import', 'after_xml_import', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment