同时更改 WooCommerce 产品类型和更新状态

我有产品、WordPress(最新版本)、WpAllImport Pro(最新版本)和 WooCommerce(最新版本)的 XML 提要,我想定期更新我的产品。


为了更新我的产品,我需要用从 XML 中消失的旧产品做一些事情。WPAI 有一个名为“删除文件中不再存在的产品”的选项和一个自定义函数:“function my_is_post_to_delete($is_post_to_delete, $post_id, $import)”


现在,问题是我的商店里到处都是外部产品,而对于这种类型的产品,我没有库存。


我需要将产品类型从外部更改为简单,然后更新他的库存,然后他的状态为“缺货”。


我有以下代码:


<?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 );


?>

最后一点:我想“而不是删除我的产品”以使它们缺货。


波斯汪
浏览 235回答 2
2回答

白猪掌柜的

您是否尝试过更新变体缓存?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 );

慕森卡

<?phpfunction my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {&nbsp; &nbsp; // Set the ID of your import&nbsp; &nbsp; if ( $import->id == 2 ) {&nbsp; &nbsp; &nbsp; &nbsp; $product = wc_get_product( $post_id );&nbsp; &nbsp; &nbsp; &nbsp; $sku = $product->get_sku();&nbsp; &nbsp; &nbsp; &nbsp; // Change product type to simple&nbsp; &nbsp; &nbsp; &nbsp; wp_set_object_terms($post_id, 'simple','product_type');&nbsp; &nbsp; &nbsp; &nbsp; // Make the product stock manageable&nbsp; &nbsp; &nbsp; &nbsp; update_post_meta($post_id, '_manage_stock', 'yes');&nbsp; &nbsp; &nbsp; &nbsp; // Set the product quantity to 0&nbsp; &nbsp; &nbsp; &nbsp; $product->set_stock_quantity();&nbsp; &nbsp; &nbsp; &nbsp; // Update the product status to Out of stock&nbsp; &nbsp; &nbsp; &nbsp; update_post_meta( $post_id, '_stock_status', wc_clean( 'outofstock' ) );&nbsp; &nbsp; &nbsp; &nbsp; wp_set_post_terms( $post_id, 'outofstock', 'product_visibility', true );&nbsp; &nbsp; &nbsp; &nbsp; // Delete cache&nbsp; &nbsp; &nbsp; &nbsp; wc_delete_product_transients( $post_id );&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );?>
打开App,查看更多内容
随时随地看视频慕课网APP