dongxing7083 2017-12-13 15:08
浏览 29
已采纳

在WooCommerce中获取新发布的产品(今天)[关闭]

I have a WooCommerce site where products are considered as trade/deal. Therefore, when someone take a trade (buy a product), it become out of stock.

How to display the number of products that has been published today (the new ones)?

ex: 4 New Trades (products) Published Today

  • 写回答

1条回答 默认 最新

  • dongyou1926 2017-12-13 16:03
    关注

    The custom function below will return with a SQL query the newly published products count (maid in the 24 hours):

    function get_new_products_count(){
        global $wpdb;
    
        // 24 hours ago
        $is_24h_ago = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s")." -1day"));
    
        // The SQL query
        $result = $wpdb->get_col( "
            SELECT COUNT(p.ID)
            FROM {$wpdb->prefix}posts as p
            WHERE p.post_type LIKE '%product%'
            AND p.post_status LIKE 'publish'
            AND p.post_date > '$is_24h_ago'
        " );
    
        return reset($result);
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and working


    Usage example (in any php file):

    $count = get_new_products_count();
    $message = sprintf( __( '%s new trades have been published…' ), $count );
    echo '<div class="woocommerce-message">'.$message.'</div>';
    

    will display something like:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)