使用产品信息触发订单状态变化的功能

triggering function on order status change with information from product

我试图在订单状态更改时使用产品的一些信息(如标题、属性等)触发功能,但我收到此错误:

FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to a member function get_title() on null

我将此代码用作我的函数:

function trigerinam_i_duombaze($order_id) {

    global $product;

    $pavadinimas = $product->get_title();

        $sql = "INSERT INTO garantiniai (kompiuterio_bukle, preke, uzsakymo_bukle)
        VALUES ('bukle', $pavadinimas, 'processing')";
}

add_action( 'woocommerce_order_status_processing', 'trigerinam_i_duombaze', 10, 1);

我在我的其他函数中使用了相同的 global $product;$pavadinimas = $product->get_title();,没有任何问题,任何想法为什么这不能专门用于这个?

我也尝试了一些不同的功能

add_action( 'woocommerce_order_status_changed', 'trigerinam_i_duombaze', 99, 3 );

但我被困在完全相同的地方.. 似乎 global $product 没有被定义。

您无法在 woocommerce_order_status_changedwoocommerce_order_status_processing 中访问 global $product;

using woocommerce_order_status_changed action hook parameter $order_id, $status_from, $status_to, $order

function trigerinam_i_duombaze_changed( $order_id, $status_from, $status_to, $order) {

    // get order from order id
    $order = wc_get_order( $order_id ); 

    foreach ($order->get_items() as $item_id => $item ) {

        $pavadinimas = $item->get_name(); 
         
        $sql = "INSERT INTO garantiniai (kompiuterio_bukle, preke, uzsakymo_bukle)
        VALUES ('bukle', $pavadinimas, 'processing')";
    }
}
add_action( 'woocommerce_order_status_changed', 'trigerinam_i_duombaze_changed', 99, 4 );

using woocommerce_order_status_changed action hook parameter $order_id, $order

function trigerinam_i_duombaze_processing( $order_id, $order) {

    // get order from order id
    $order = wc_get_order( $order_id ); 

    foreach ($order->get_items() as $item_id => $item ) {

        $pavadinimas = $item->get_name(); 
         
        $sql = "INSERT INTO garantiniai (kompiuterio_bukle, preke, uzsakymo_bukle)
        VALUES ('bukle', $pavadinimas, 'processing')";
    }
}
add_action( 'woocommerce_order_status_processing', 'trigerinam_i_duombaze_processing', 10, 2);