Reputation: 762
Im trying to trigger function with some information from product (like title, attributes ect.) on order status change, but Im getting this error:
FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to a member function get_title() on null
Im using this code as my function:
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);
I have same global $product;
and $pavadinimas = $product->get_title();
used in my other functions without any problems, any Ideas why this not working specifically with this one?
I also tried a bit different function with
add_action( 'woocommerce_order_status_changed', 'trigerinam_i_duombaze', 99, 3 );
But I'm stuck at exactly same place.. seems like global $product
is not getting defined.
Upvotes: 1
Views: 388
Reputation: 11282
You can't access global $product;
inside woocommerce_order_status_changed
or woocommerce_order_status_processing
.
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);
Upvotes: 2