WooCommerce产品级简单票务系统

问题描述 投票:1回答:1

我正在使用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并输出meta标签,当我意识到当用户购买的数量多于一个数量时,它为'Ticket Number'键创建的meta值为'10 11'视图作为单个字符串。我尝试使用SUBSTRING_INDEX将其分解,如上所示,但它不会查看第一个订单之后的订单。

我需要将其视为单独的数字,因此,如果用户购买了十个商品,则它可以将最后一个数字识别为最高数字(例如21 22 23 24),而下一个购买此产品的用户可以通过sql查询来识别字符串中的24作为最大数字,并使新项meta为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();
    }
}
php mysql wordpress woocommerce orders
1个回答
1
投票

[以另一种方式,使这项工作更加容易和有效。

当订单更改为处理状态时:

  • [首先,对于每个订单商品,我增加了售票数量((商品数量))作为产品级别的索引(已保存/更新为自定义产品元数据)。

  • [[然后,对于每个订单商品,我会根据相关商品索引(在更新之前)根据商品数量生成票证编号,并将其保存为自定义订单商品元数据。
  • 最后,对于每个订单商品,我将更新相关产品“索引”,并将售出的数量添加到当前“索引”值中。

代码

(注释):

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文件中。经过测试和工作。

_tickets_number隐藏的自定义订单项目元数据是可选的,它使您可以获取票证数组,而不是使用以下方法获得一串票证:$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文件中。应该可以。
© www.soinside.com 2019 - 2024. All rights reserved.