首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WooCommerce产品级简单票务系统

WooCommerce产品级简单票务系统
EN

Stack Overflow用户
提问于 2020-05-19 21:50:41
回答 1查看 146关注 0票数 1

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

代码语言:javascript
复制
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并输出元标记,当我意识到当用户购买的数量超过一个数量时,它为‘票号’键创建的元值是'10 11‘,mysql将其视为一个字符串。我试着使用上面所示的SUBSTRING_INDEX将其分解,但它不会查看第一个订单之后的任何订单。

我需要它将数字看作单个数字,因此如果用户购买了项目中的10个,它可以识别最后一个数字为最高(例如21、22、23、24),下一个用户购买此产品时,sql查询将识别字符串中的24作为最高数字,并使新项目元值为25。

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

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-20 00:23:29

有一种不同的方式,更容易和更有效地使这一工作。

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

  • 首先,对于每个订单项目,我在产品级别增加售票数量(项目数量)作为索引(保存/更新为自定义产品元数据)。然后,对每个订单项目,从相关的产品索引(更新前)生成基于项目数量的票证编号,并将其保存为自定义订单项元数据。
  • 并完成对每个订单项目的相关产品“索引”的更新,将销售的数量添加到当前的“索引”值中。

代码(注释)

代码语言:javascript
复制
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文件中。测试和工作。

隐藏的自定义订单项元数据是可选的,允许您使用:$item->get_meta('_tickets_number');获取票证数组,而不是一串票证。

如果您希望是一个全局票务系统(而不是在产品级别),您将使用以下方法:

代码语言:javascript
复制
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文件中。应该管用的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61901348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档