Нужно получить название использованного в заказе купона. Для наглядности вывожу под продуктами на той же странице заказа. При поиске решения окончательно запутался, потому что некоторые методы уже устаревшие. Буду очень благодарен за помощь.
Вот пример того, что есть на данный момент:
add_action( 'woocommerce_after_order_itemmeta', 'so_32457241_after_order_itemmeta', 10, 3 );
function so_32457241_after_order_itemmeta( $post_id ){
// Get an instance of WC_Order object
$order = wc_get_order( $order_id );
// Coupons used in the order LOOP (as they can be multiple)
foreach( $order->get_coupon_codes() as $coupon_code ){
// Retrieving the coupon ID
$coupon_post_obj = get_page_by_title($coupon_code, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
// Get an instance of WC_Coupon object in an array(necessary to use WC_Coupon methods)
$coupon = new WC_Coupon($coupon_id);
return $coupon;
}
echo $coupon;}
Но в итоге выдаётся ошибка Call to a member function get_coupon_codes() on bool. Я очень мало понимаю в WordPress, заранее спасибо!
Update: Основная моя задача - добавить к передаче данных из заказа, помимо адреса, телефона и тд., ещё и использованный пользователем купон. Вот так выглядит функция в плагине:
public static function get_data_order_address($order_id)
{
$order = wc_get_order($order_id);
$address = '';
if ($order->get_billing_postcode()) {
$address .= $order->get_billing_postcode();
}
if ($order->get_billing_state()) {
$address .= ', ' . $order->get_billing_state();
}
return $address;
}
Я хочу добавить следом похожую функцию, с передачей купона, по типу
public static function get_data_order_coupons($order_id)
{
$order = wc_get_order($order_id);
$coupons = [];
if ($order->get_used_coupons()) {
$coupons .= $order->get_used_coupons();
}
return $coupons;
}