【问题标题】:Set all products status "out of stock" based on orders count in Woocommerce根据 Woocommerce 中的订单数设置所有产品状态“缺货”
【发布时间】:2018-11-03 20:08:31
【问题描述】:

我对 woocommerce 订单有此特定要求。所以我想要实现的是,一旦 woocommerce 中的订单总数达到特定数字(比如说 200),那么我不想再接受订单,我想显示“缺货” ” 消息或任何其他自定义消息(基本上我不想让客户在该条件变为真时购买任何东西)在我库存中的每个产品上。我希望这个逻辑也适用于可变产品。

我能够获得订单数量,但我现在卡住了。这是我迄今为止尝试过的。

function display_woocommerce_order_count( $atts, $content = null ) {
  $args = shortcode_atts( array(
    'status' => 'processing',
   ), $atts );

  $statuses = array_map( 'trim', explode( ',', $args['status'] ) );
  $order_count = 0;

  foreach ( $statuses as $status ) {
    if ( 0 !== strpos( $status, 'wc-' ) ) {
     $status = 'wc-' . $status;
    }
    $order_count += wp_count_posts( 'shop_order' )->$status;
  }

  echo number_format( $order_count );

  if($order_count == 200){
   //Then make all products go out of stock automatically
   //Not able to figure out this part
  }

}

请指导我如何从这里继续前进。

【问题讨论】:

    标签: php wordpress woocommerce product orders


    【解决方案1】:

    你不能直接在你的简码中做到这一点……而且你获取订单状态计数的功能有点重。相反,您可以使用以下自定义函数,该函数使用非常简单的 SQL 查询来获取订单数。

    另一组钩子函数会将所有产品(甚至可变产品的产品变体)设置为“缺货”状态,此时订单数将达到 200。

    最后我更改了显示订单数量的短代码功能。

    您可以在计算订单的功能中,也可以在您的简码中根据需要设置订单状态。

    改为使用以下内容:

    // Utility function to get orders count based on orders statuses 
    // Default statuses are processing + completed (and you can change them as you like)
    function get_orders_count_from_statuses( $statuses = 'processing, completed' ){
        global $wpdb;
    
        // Filtering order statuses
        $statuses = "wc-" . str_replace(array("wc-",", ",","), array("",",","','wc-"), $statuses );
    
        // return the query
        return (int) $wpdb->get_var("SELECT count(ID)  FROM {$wpdb->prefix}posts
        WHERE post_status IN ('$statuses') AND `post_type` LIKE 'shop_order'");
    }
    
    
    // Set conditionally product status based on order count limit
    add_filter( 'woocommerce_product_get_stock_status', 'conditional_product_status', 10, 2 );
    add_filter( 'woocommerce_product_variation_get_stock_status', 'conditional_product_status', 10, 2 );
    function conditional_product_status( $stock_status, $product ){
        if( get_orders_count_from_statuses() >= 200 ){
            $stock_status = 'outofstock';
        }
        return $stock_status;
    }
    
    
    // Your shortcode function that will display the count (if needed)
    add_shortcode( 'order_count', 'display_woocommerce_order_count' );
    function display_woocommerce_order_count( $atts ) {
        $atts = shortcode_atts( array(
            'statuses' => 'processing,completed',
        ), $atts, 'order_count' );
    
        $order_count = get_orders_count_from_statuses( $atts['statuses'] );
    
        return number_format( $order_count ); // Always use return (not echo for a shortcode)
    }
    

    代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 效果很好,非常感谢您的帮助并提出了一种更简单的订单计数方法。你太棒了,干杯!
    猜你喜欢
    • 1970-01-01
    • 2018-08-01
    • 2017-07-23
    • 2020-01-16
    • 2013-04-12
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多