根据产品信息触发订单状态变化功能

时间:2021-04-23 18:19:43

标签: php wordpress woocommerce hook-woocommerce

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

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 没有得到定义。

1 个答案:

答案 0 :(得分:2)

您无法在 global $product;woocommerce_order_status_changed 内访问 woocommerce_order_status_processing

<块引用>

使用 woocommerce_order_status_changed 动作钩子参数 $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 );
<块引用>

使用 woocommerce_order_status_changed 动作钩子参数 $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);