同时更改 WooCommerce 产品类型和更新状态
Change WooCommerce product type and update status in the same time
我有一个 XML 产品提要、WordPress(最新版本)、WpAllImport Pro(最新版本)和 WooCommerce(最新版本),我想定期更新我的产品。
为了更新我的产品,我需要用从 XML 中消失的旧产品做点什么。
WPAI 有这个名为 "Delete products that are no longer present in your file" 的选项和一个自定义函数:"function my_is_post_to_delete( $is_post_to_delete, $post_id, $import )"
现在,问题是我的商店里全是外部产品,而对于这种类型的产品,我没有库存。
我需要的是将产品类型从外部更改为简单,然后将他的库存和状态更新为 "out of stock"。
我有以下代码:
<?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 );
?>
但我得到一个错误:
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
最后一点:
我想 "Instead of delete my products" 让它们脱销。
您尝试过更新变体缓存吗?
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 );
<?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 );
?>
我有一个 XML 产品提要、WordPress(最新版本)、WpAllImport Pro(最新版本)和 WooCommerce(最新版本),我想定期更新我的产品。
为了更新我的产品,我需要用从 XML 中消失的旧产品做点什么。 WPAI 有这个名为 "Delete products that are no longer present in your file" 的选项和一个自定义函数:"function my_is_post_to_delete( $is_post_to_delete, $post_id, $import )"
现在,问题是我的商店里全是外部产品,而对于这种类型的产品,我没有库存。
我需要的是将产品类型从外部更改为简单,然后将他的库存和状态更新为 "out of stock"。
我有以下代码:
<?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 );
?>
但我得到一个错误:
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
最后一点: 我想 "Instead of delete my products" 让它们脱销。
您尝试过更新变体缓存吗?
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 );
<?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 );
?>