首页 > 解决方案 > Woocommerce - 当产品的销售计划开始和结束时,从特定类别中自动添加和删除产品

问题描述

我编写了一个代码,用于在产品的销售计划开始时将产品添加到特定类别,并在产品的销售计划结束时从这些类别中删除。这是我的代码,但不起作用。有人可以帮助我吗?

function zt_wc_scheduled_sales() {
    $data_store = WC_Data_Store::load( 'product' );

    // Sales which are due to start.
    $product_ids = $data_store->get_starting_sales();
    if ( $product_ids ) {
        do_action( 'wc_before_products_starting_sales', $product_ids );
        foreach ( $product_ids as $product_id ) {
            $product = wc_get_product( $product_id );

            if ( $product ) {
                $sale_price = $product->get_sale_price();
                wp_set_object_terms( $product_id, 'pishnahad', 'product_cat', true );

                $product->save();
            }
        }
        do_action( 'wc_after_products_starting_sales', $product_ids );

        WC_Cache_Helper::get_transient_version( 'product', true );
        delete_transient( 'wc_products_onsale' );
    }

    // Sales which are due to end.
    $product_ids = $data_store->get_ending_sales();
    if ( $product_ids ) {
        do_action( 'wc_before_products_ending_sales', $product_ids );
        foreach ( $product_ids as $product_id ) {
            $product = wc_get_product( $product_id );

            if ( $product ) {
                wp_remove_object_terms( $product_id, 'pishnahad', 'product_cat' );
                $product->save();
            }
        }
        do_action( 'wc_after_products_ending_sales', $product_ids );

        WC_Cache_Helper::get_transient_version( 'product', true );
        delete_transient( 'wc_products_onsale' );
    }
}
add_action( 'woocommerce_scheduled_sales', 'zt_wc_scheduled_sales' );

标签: wordpresswoocommerce

解决方案


我已经修复了您代码中的错误。

我已经用自定义查询替换get_starting_sales()了Firts 。get_ending_sales()所以现在你可以获得产品ID

然后我重新排列并删除了未使用的代码。

function zt_wc_scheduled_sales() {
    $data_store = WC_Data_Store::load( 'product' );
    global $wpdb;
   
    // Sales which are due to start.
    $product_ids = $wpdb->get_col(
        $wpdb->prepare(
            "SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta
            LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id
            LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id
            WHERE postmeta.meta_key = '_sale_price_dates_from'
                AND postmeta_2.meta_key = '_price'
                AND postmeta_3.meta_key = '_sale_price'
                AND postmeta.meta_value > 0
                AND postmeta.meta_value < %s",
            time()
        )
    );
    
    if ( $product_ids ) {
        foreach ( $product_ids as $product_id ) {
            $product = wc_get_product( $product_id );
            if ( $product ) {
                wp_set_object_terms( $product_id, 'pishnahad', 'product_cat', true );
                $product->save();
            }
        }
    }

    // Sales which are due to end.
    $product_ids = $wpdb->get_col(
        $wpdb->prepare(
            "SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta
            LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id
            LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id
            WHERE postmeta.meta_key = '_sale_price_dates_to'
                AND postmeta_2.meta_key = '_price'
                AND postmeta_3.meta_key = '_regular_price'
                AND postmeta.meta_value > 0
                AND postmeta.meta_value < %s",
            time()
        )
    );

    if ( $product_ids ) {
        foreach ( $product_ids as $product_id ) {
            $product = wc_get_product( $product_id );
            if ( $product ) {
                wp_remove_object_terms( $product_id, 'pishnahad', 'product_cat' );
                $product->save();
            }
        }
    }
}
add_action( 'woocommerce_scheduled_sales', 'zt_wc_scheduled_sales' );

测试和工作。


推荐阅读