Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce产品级简单票务系统_Php_Mysql_Wordpress_Woocommerce_Orders - Fatal编程技术网

Php WooCommerce产品级简单票务系统

Php WooCommerce产品级简单票务系统,php,mysql,wordpress,woocommerce,orders,Php,Mysql,Wordpress,Woocommerce,Orders,我正在使用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

我正在使用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”,mysql将其视为单个字符串时,我的问题就出现了。我尝试使用如上所示的SUBSTRING_索引将其拆分,但它不会查看超过第一个索引的任何订单

我需要它将数字视为单个数字,这样,如果用户购买了10个项目,它可以识别最后一个数字为最高数字(例如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();
    }
}
add_action('woocommerce_order_status_processing'、'action_woocommerce_order_status_completed');
功能操作\商业\订单\状态\完成($order\ id){
$order=新WC\U订单($order\U id);
$items=$order->get_items();
foreach($key=>$value的项目){
$get_last_order=从产品(10)获取_last_order_id_;
if($get\u last\u order==null){
$gen_id_1=“0”;
}否则{
$get\u order\u id=内爆($get\u last\u order);
$lastorder=wc\U get\U order($get\U order\U id);
$lastitem=$lastorder->get_items();
foreach($key2=>$value2的最后一项){
$custom_thing=$value2->get_meta(“票号”);
}
$gen_id=爆炸(“”,$custom_thing);
$gen_id_1=结束($gen_id);
}
$qua=$value->get_quantity();
对于($x=1;$x添加元数据(“票号,$gen\u id\u 2”);
$value->save();
}
}

有一种不同的方法,更容易、更有效地实现这一点

订单更改为处理状态时:

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

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

  • 最后,对于每个订单项目,我更新相关的产品“索引”,将销售数量添加到当前的“索引”值

代码(注释):


代码放在活动子主题(活动主题)的functions.php文件中。它应该可以工作。

这太完美了,非常感谢。我不知道你真的可以这样做,这是我第一次使用woocommerce进行开发,所以我仍在摸索。
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
}
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
}