I have to change the product price (only in single product page) depending on orders made in a date range, and if user has a membership. This is fine, but the problems is that something fails when I use WC_Order_Query inside the woocommerce_get_price_html filter
The problem is using the WC_Order_Query inside filter, because when I comment it then works. What can I use for that query? Or I have to change of filter, hook?
function change_price( $price ) {
global $product;
if($product->get_type() != "lottery") return $price;
$user_id = get_current_user_id();
$membresias = wc_memberships_get_user_memberships($user_id); // Get the memberships of user, returning an array
if(!$membresias) return;
foreach ($membresias as $value) {
if ($value->status == "wcm-free_trial" || $value->status == "wcm-active") {
$currentDayOfWeek = date("N");
$start = date('Y-m-d', strtotime('-'.(intval($currentDayOfWeek )-1).' days'));
$end = date('Y-m-d', strtotime('+'.(7 - intval($currentDayOfWeek )).' days'));
$query = new WC_Order_Query(
array(
'limit' => 10,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
'customer_id' => $user_id,
'date_paid' => $start."...".$end, //'2018-02-01...2018-02-28',
'meta_key' => '_lottery',
)
);
$orders = $query->get_orders(); // If I comment this page loads well.
/* Also fails
$orders = wc_get_orders(
array(
'limit' => 10,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
'customer_id' => $user_id,
'date_paid' => $start."...".$end, //'2018-02-01...2018-02-28',
'meta_key' => '_lottery',
)
);
*/
$sorteosJugadosCount = 0;
foreach ($orders as $order) {
$order = wc_get_order($order);
foreach ($order->get_items() as $item_id => $item_data) {
$product = $item_data->get_product();
$item_quantity = $item_data->get_quantity(); // Get the item quantity
$sorteosJugadosCount += $item_quantity;
}
}
if ( $sorteosJugadosCount < 7 )
return "0€ (Free lottery)";
}
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'sv_change_product_price_display', 10, 2);