产品级别的 WooCommerce 简单票务系统

WooCommerce simple ticketing system at product level

我正在使用 WordPress 和 WooCommerce,并且有一个 SQL 查询来调用特定产品类型的元标记。

function get_last_order_id_from_product( $product_id ) {
global $wpdb;

return $wpdb->get_col( $wpdb->prepare( "
    SELECT oi.order_id
    FROM {$wpdb->prefix}woocommerce_order_items as oi
    LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as oim
        ON oi.order_item_id = oim.order_item_id
    LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as oim2
        ON oi.order_item_id = oim2.order_item_id
    LEFT JOIN {$wpdb->posts} AS p
        ON oi.order_id = p.ID
    WHERE p.post_type = 'shop_order'
    AND oi.order_item_type = 'line_item'
    AND oim.meta_key = '_product_id'
    AND oim.meta_value = '%d'
    AND oim2.meta_key = 'Ticket Number'
    ORDER BY SUBSTRING_INDEX(oim2.meta_value, ' ', 10) DESC

    LIMIT 1
", $product_id ) );
}

上面的代码采用产品 ID 并输出元标记,当我意识到当用户购买多个数量时,它为 'Ticket Number' 键创建的元值是 '10 11' mysql 视为单个字符串。我尝试使用 SUBSTRING_INDEX 将其分开,如上所示,但它不会查看第一个之后的任何订单。

我需要它来将数字视为单独的数字,因此如果用户购买了十件商品,它可以将最后一个数字识别为最高的(比如 21 22 23 24),而下一个购买此产品的用户 sql 查询会将字符串中的 24 识别为最高数字,并使新项目元为 25。

这也是我的功能,如果它有任何用处的话,它目前正在运行,但只能从元值 1 开始工作,但是购买了许多产品:

add_action('woocommerce_order_status_processing', 'action_woocommerce_order_status_completed');

function action_woocommerce_order_status_completed($order_id) {
    $order = new WC_Order($order_id);
    $items = $order->get_items();
    foreach ($items as $key => $value) {
        $get_last_order = get_last_order_id_from_product( 10 );
        if ($get_last_order == null){
            $gen_id_1 = "0";
        } else {
            $get_order_id = implode($get_last_order);
            $lastorder = wc_get_order($get_order_id);
            $lastitem = $lastorder->get_items();

            foreach ($lastitem as $key2 => $value2) {
                $custom_thing = $value2->get_meta('Ticket Number');
            }
            $gen_ids = explode(' ', $custom_thing);
            $gen_id_1 = end($gen_ids);
        }

        $qua = $value->get_quantity();
        for ($x = 1; $x <= $qua; $x++) {
            $gen_id_1++;
            $gen_id_2 .= " $gen_id_1";
        };
        $value->add_meta_data( "Ticket Number", $gen_id_2);
        $value->save();
    }
}

有一种不同的方法,可以更轻松、更高效地完成这项工作。

订单状态变为处理中时:

  • 首先,对于每个订单商品,我增加销售的票数(商品数量)作为产品级别的指标(saved/updated 作为自定义产品元数据)。

  • 然后对于每个订单项目,我根据项目数量从相关产品索引(更新之前)生成票号,我将其保存为自定义订单项目元数据。

  • 最后,对于每个订单项目,我更新相关产品 "index" 将售出数量添加到当前 "index" 值。

代码(已评论):

add_action( 'woocommerce_order_status_processing', 'action_woocommerce_order_status_processing', 10, 2 );
function action_woocommerce_order_status_processing( $order_id, $order ) {
    // Loop through order items
    foreach ($order->get_items() as $item_id => $item ) {
        // Check that tickets numbers haven't been generated yet for this item
        if( $item->get_meta( "_tickets_number") )
            continue;

        $product     = $item->get_product(); // Get the WC_Produt Object
        $quantity    = (int) $item->get_quantity(); // Get item quantity

        // Get last ticket sold index from product meta data
        $now_index = (int) $product->get_meta('_tickets_sold');

        $tickets_ids = array(); // Initializing

        // Generate an array of the customer tickets Ids from the product registered index (last ticket ID)
        for ($i = 1; $i <= $quantity; $i++) {
            $tickets_ids[] = $now_index + $i;
        };

        // Save the tickets numbers as order item custom meta data
        $item->update_meta_data( "Tickets numbers", implode('  ', $tickets_ids) ); // Displayed string of tickets numbers on customer orders and emails
        $item->update_meta_data( "_tickets_number", $tickets_ids ); // (Optional) Array of the ticket numbers (Not displayed to the customer)
        $item->save(); // Save item meta data

        // Update the Ticket index for the product (custom meta data)
        $product->update_meta_data('_tickets_sold', $now_index + $quantity );
        $product->save(); // Save product data
    }
    $order->save(); // Save all order data
}

代码进入您的活动子主题(活动主题)的 functions.php 文件。已测试并有效。

The _tickets_number hidden custom order item meta data is optional and allow you to get the array of tickets, instead of a string of tickets using: $item->get_meta('_tickets_number');


如果您想要全球票务系统(不是在产品级别),您将使用以下内容:

add_action( 'woocommerce_order_status_processing', 'action_woocommerce_order_status_processing', 10, 2 );
function action_woocommerce_order_status_processing( $order_id, $order ) {
    // Loop through order items
    foreach ($order->get_items() as $item_id => $item ) {
        $now_index = (int) get_option( "wc_tickets_number_index"); // Get last ticket number sold (globally)
        $quantity  = (int) $item->get_quantity(); // Get item quantity

        $tickets_ids = array(); // Initializing

        // Generate an array of the customer tickets Ids from the tickets index
        for ($i = 1; $i <= $quantity; $i++) {
            $tickets_ids[] = $now_index + $i;
        };

        // Save the tickets numbers as order item custom meta data
        $item->update_meta_data( "Tickets numbers", implode('  ', $tickets_ids) ); // Displayed string of tickets numbers on customer orders and emails
        $item->update_meta_data( "_tickets_number", $tickets_ids ); // (Optional) Array of the ticket numbers (Not displayed to the customer)
        $item->save(); // Save item meta data

        // Update last ticket number sold (globally)
        update_option( 'wc_tickets_number_index', $now_index + $quantity );
    }
    $order->save(); // Save all order data
}

代码进入您的活动子主题(活动主题)的 functions.php 文件。它应该有效。