【问题标题】:Woocommerce, payment successfully, run SQL command and emailWoocommerce,付款成功,运行 SQL 命令和电子邮件
【发布时间】:2021-12-27 02:35:31
【问题描述】:

我想为我的搜索找到一个正确的解决方案。我使用 Woocommerce,我创建了 5 个产品,包括 3 个专用虚拟产品。 这是这些产品的 id “产品 1” = #id23、“产品 2” = #id24 和 “产品 3” = #id26 我希望在订单付款成功后,脚本(将包含在我的子主题的 functions.php 中)分析我的订单,如下所示:

例如,如果买家购买了 x2 数量的“产品 1”,该脚本将运行该脚本两次,就像在数据库中添加一些内容(如自动生成的密码、随机序列号、日期、姓名产品,元信息...)。 请注意:循环两次的信息必须不同,尤其是密码,例如序列号) 并且买方必须购买“产品 2”,脚本必须再次运行....

所以如果你理解我的意思,这个脚本必须运行 n 次,包括带有 id(id#23、id#24、id#26)的产品及其数量。

这是一段合乎逻辑的代码(不起作用),因为我在寻求帮助;)

代码已更改/更新但不起作用:(

                <?php
    // When payment is OK
    function so_32512552_payment_complete( $order_id )
    {
        // Connecting to external DB credentials
        // the file 'connect_sql.inc.php' is in the same directory than functions.php on your child theme?
        include ('connect.inc.php');
        // Create connection, try to avoid this, use wpdb class
        //$conn = new mysqli($DBHost, $DBLogin, $DBPass, $DBName);
        $conn = mysqli_connect($DBHost, $DBLogin, $DBPass, $DBName);
       
        // Check all specific products IDs (#23, #24 and #26) to be treat --- Possibility to add new products IDs later...
        // this must implement in other way, but depend on your process
        //$productsIds = array(18, 19, 20, 21, 22, 23);
        $productsIds = array(18, 19, 20, 21, 22, 23);
        // Check if the connection has no errors
        if (!$conn->connect_error)
        {
            $order = wc_get_order( $order_id );
            // remember $item is an instance of WC_Order_item
            foreach ( $order->get_items() as $item ) 
            {
                // no use $item['product_id']
                if ( in_array($item->get_id(), $productsIds) ) 
                {
                    //$qty = $order->get_quantity_from_item( $item );
                    // the correct way for get the quantity is this, but not necesary here, there will not be reuse
                    // $qty = $item->get_quantity();
                    // this is an infinite loop
                    // while ($qty):
                    for ($i=0; $i < $item->get_quantity(); $i++) 
                    { 
                        // Get datas into variables
                        // this will not work
                        // $customer_name = $order->get_customer_name();
                        // use instead billing or shippings fields
                        $customer_name = $order->get_billing_first_name().' '.$order->get_billing_last_name();
                        // this will not work
                        // $customer_email = $order->get_customer_email();
                        $customer_email = $order->get_billing_email();
                        // this will not work
                        // use
                        $product = wc_get_product($item->get_id());
                        //$product_name = $order->get_product_from_item($item);
                        $product_name = $product->get_name();
                        // no use this, no good
                        // $password = rand(9999,9999999);
                        $password = wp_generate_password( 10, false );
                        // neither this
                        // $serial_number = rand(9999,9999999);
                        // this implementation depend on your project
                        $serial_number = rand(9999,9999999);
                        
                        // Add a new entry in DB thru SQL command
                        // where the sql is executed?
                        //$sql = "INSERT INTO tableTest_v5 (user_fullname, user_email, software_product, user_passwd, user_licensenumber, val1) VALUES ('$customer_name', '$customer_email', '$product_name', '$password', '$serial_number')";
                        
                        mysqli_query("INSERT INTO `tableTest_v5` (`id`, `state_account`, `user_email`, `user_passwd`, `user_fullname`, `user_corporate`, `user_licensenumber`, `software_product`, `software_license`, `activation_date`, `software_expiredate`, `wcid`, `user_computerinfos`, `activation_cpuname`, `activation_id`, `activation_cpt`, `superaccess`) VALUES (NULL, -1, 'email', NULL, 'Nick POHENIS', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 3, 0)");
                        
                        //$conn->query($sql);
                    
                        // Then entry added, prepare and send the mail to customer
                        // this not good, use other implementation if can
                        $message = 'Hi,'.$customer_name;
                        $message .= 'Your product: '.$product_name;
                        $message .= 'Name: '.$customer_name;
                        $message .= 'ID: '.$customer_email;
                        $message .= 'Password: '.$password;
                        $message .= 'Serial number: '.$serial_number;
                        wp_mail('mail@domaine.com', 'Email: Additionnal Info', $message);
                    }
                    //endwhile;
                }
            }
            // Close DB
            //$conn->close();   
            mysqli_close($conn);
        } else {
            // no die, because we are in woocommerce task 'payment_complete'
            // register as other way for inform to you
            // die("Connection failed: " . $conn->connect_error);
        }
    }
    add_action( 'woocommerce_payment_complete', 'so_32512552_payment_complete' );
    ?>

希望您理解,无论如何,非常感谢您的帮助。因为我已经做了几个星期的研究,但没有成功。欢迎您的帮助!!!!

问候, 妮可

【问题讨论】:

标签: php wordpress function woocommerce payment


【解决方案1】:
<?php

// When payment is OK
// remove this, it is not in use
// global $woocommerce;

function so_32512552_payment_complete( $order_id )
{
    // Connecting to external DB credentials
    // the file 'connect_sql.inc.php' is in the same directory than functions.php on your child theme?
    include ('connect_sql.inc.php');
    // Create connection, try to avoid this, use wpdb class
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check all specific products IDs (#23, #24 and #26) to be treat --- Possibility to add new products IDs later...
    // this must implement in other way, but depend on your process
    $productsIds = array(23, 24, 26);
    // Check if the connection has no errors
    if (!$conn->connect_error)
    {
        $order = wc_get_order( $order_id );
        // remember $item is an instance of WC_Order_item
        foreach ( $order->get_items() as $item ) 
        {
            // no use $item['product_id']
            if ( in_array($item->get_id(), $productsIds) ) 
            {
                //$qty = $order->get_quantity_from_item( $item );
                // the correct way for get the quantity is this, but not necesary here, there will not be reuse
                // $qty = $item->get_quantity();
                // this is an infinite loop
                // while ($qty):
                for ($i=0; $i < $item->get_quantity(); $i++) 
                { 
                    // Get datas into variables
                    // this will not work
                    // $customer_name = $order->get_customer_name();
                    // use instead billing or shippings fields
                    $customer_name = $order->get_billing_first_name().' '.$order->get_billing_last_name();
                    // this will not work
                    // $customer_email = $order->get_customer_email();
                    $customer_email = $order->get_billing_email();
                    // this will not work
                    // use
                    $product = wc_get_product($item->get_id());
                    //$product_name = $order->get_product_from_item($item);
                    $product_name = $product->get_name();
                    // no use this, no good
                    // $password = rand(9999,9999999);
                    $password = wp_generate_password( 16, false );
                    // neither this
                    // $serial_number = rand(9999,9999999);
                    // this implementation depend on your project
                    $serial_number = rand(9999,9999999);
                    
                    // Add a new entry in DB thru SQL command
                    // where the sql is executed?
                    $sql = "INSERT INTO MyData_v5 (fullname, email, productname, password, serialnumber, val1) VALUES ('$customer_name', '$customer_email', '$product_name', '$password', '$serial_number')";
                    // maybe $conn->query($sql);
                
                    // Then entry added, prepare and send the mail to customer
                    // this not good, use other implementation if can
                    // $message = 'Hi,'.$cumstomer_name;
                    // has an extra 'm'
                    $message = 'Hi,'.$customer_name;
                    // need '_'
                    $message .= 'Your product: '.$product_name;
                    // has an extra 'm'
                    // $message .= 'Name: '.$cumstomer_name;
                    $message .= 'Name: '.$customer_name;
                    $message .= 'ID: '.$customer_email;
                    $message .= 'Password: '.$password;
                    $message .= 'Serial number: '.$serial_number;
                    wp_mail($customer_email, 'Email: Additionnal Info', $message);
                }
                //endwhile;
            }
        }
        // Close DB
        $conn->close();   
    } else {
        // no die, because we are in woocommerce task 'payment_complete'
        // register as other way for inform to you
        // die("Connection failed: " . $conn->connect_error);
    }
}
add_action( 'woocommerce_payment_complete', 'so_32512552_payment_complete' );

【讨论】:

  • 太棒了,谢谢,我会试试的。是否可以将 ID 客户帐户作为 Val2 放入 DB 中?再次感谢您
  • 我刚刚更新了代码,但它不起作用,没有添加数据,也没有收到额外的:(
  • 在 WordPress 解决方案的范围内,这不是一个很好的答案。 wordpress.stackexchange.com/questions/1604/…
猜你喜欢
  • 2013-08-17
  • 1970-01-01
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
相关资源
最近更新 更多