【问题标题】:Set the product category to New in for imported products automatically in woocommerce在woocommerce中自动将产品类别设置为新进口产品
【发布时间】:2021-10-22 22:12:24
【问题描述】:

我将尝试解释我的目标是什么以及我已经做了什么。

我正在将一堆产品导入 WooCommerce,但现在我想添加一个功能,如果产品不超过 60 天,它会自动将产品类别设置为“NEW IN”。 60 天后,它应该再次移出该类别。

可以说,我正在使用WordPress和WooCommerce学习php,所以如果我不知道某些事情,我很抱歉。

   function add_product_to_new_in()
{
    global $product;

    // Get the date for the product published and current date
    $start = date('n/h/Y', strtotime($product->get_date_created()));
    $today = date('n/j/Y');


    // Get the date for the start of the event and today's date
    $start = new \DateTime($start);
    $end = new \DateTime($today);

    //FInd the difference
    $difference = $start->diff($end);
    $days = $difference->d;

    // If the difference is less than 60, apply "NEW IN cat"
    if ($days = (60 < $days)) {
        wp_set_object_terms($product, 40, 'product_cat');
    }
}

如果您能以任何方式帮助我,我将不胜感激!

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您可以在每次页面加载时触发的 init 挂钩上执行此操作。

    首先,您必须获得所有产品。

    $args = array(
        'post_type'   => 'product',
        'posts_per_page' => -1
    );
    
    $products = new WP_Query( $args );
     
    if ( $products->have_posts() ) {  while ( $products->have_posts() ) {  $products->the_post();
        // your logic will go here.
    } wp_reset_postdata(); }
    

    现在计算天数。

    $product  = wc_get_product( get_the_ID() );
    $datetime = $product->get_date_created();
    $timezone = $datetime->getTimezone();
    $now_time = new WC_DateTime();
    
    $now_time->setTimezone($timezone);
    
    $timestamp_diff = $now_time->getTimestamp() - $datetime->getTimestamp();
    $data           = timestamp_to_array( $timestamp_diff );
    $days           = $data['d'];
    

    然后您可以使用wp_set_object_termswp_remove_object_terms 来设置和删除类别。

    // If the difference is less than 60, apply "NEW IN cat"
    if ( $days < 60 ) {
        wp_set_object_terms( get_the_ID(), 40, 'product_cat', true );
    }else{
        wp_remove_object_terms( get_the_ID(), 40, 'product_cat' );
    }
    

    完整代码。在您的活动主题functions.php文件中添加以下代码。

    function add_category_to_product_for_certain_days(){
    
        $args = array(
            'post_type'   => 'product',
            'posts_per_page' => -1
        );
    
        $products = new WP_Query( $args );
         
        if ( $products->have_posts() ) {  while ( $products->have_posts() ) {  $products->the_post();
    
            $product  = wc_get_product( get_the_ID() );
            $datetime = $product->get_date_created();
            $timezone = $datetime->getTimezone();
            $now_time = new WC_DateTime();
    
            $now_time->setTimezone($timezone);
    
            $timestamp_diff = $now_time->getTimestamp() - $datetime->getTimestamp();
            $data           = timestamp_to_array( $timestamp_diff );
            $days           = $data['d'];
            
            // If the difference is less than 60, apply "NEW IN cat"
            if ( $days < 60 ) {
                wp_set_object_terms( get_the_ID(), 40, 'product_cat', true );
            }else{
                wp_remove_object_terms( get_the_ID(), 40, 'product_cat' );
            }
    
        } wp_reset_postdata(); }
    
    }
    
    add_action( 'init', 'add_category_to_product_for_certain_days', 10, 1 );
    
    function timestamp_to_array( $timestamp ) {
        $d = floor($timestamp/86400);
        $_d = ($d < 10 ? '0' : '').$d;
    
        $h = floor(($timestamp-$d*86400)/3600);
        $_h = ($h < 10 ? '0' : '').$h;
    
        $m = floor(($timestamp-($d*86400+$h*3600))/60);
        $_m = ($m < 10 ? '0' : '').$m;
    
        $s = $timestamp-($d*86400+$h*3600+$m*60);
        $_s = ($s < 10 ? '0' : '').$s;
    
        return array('d' => $_d, 'h' => $_h, 'm' => $_m, 's' => $_s);
    }
    

    经过测试并且有效。

    【讨论】:

    • 感谢您的回答,对我帮助很大!
    • 我对这段代码还有另一个问题。它现在自动设置类别,有没有办法防止已经存在的类别的任何覆盖?我投了赞成票,但我需要至少 15 名声望。
    • 有没有办法防止对现有类别的任何覆盖? => 你能解释更多吗?
    • 例如,如果我在 woocommerce 中导入产品,我会给它们已经固定的类别。但是当我使用这段代码时,它会覆盖已经设置的类别。
    • 好的,知道了。将更新答案。
    猜你喜欢
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 2018-06-26
    • 2019-01-08
    • 2016-03-20
    • 2019-06-20
    • 1970-01-01
    • 2018-01-30
    相关资源
    最近更新 更多