Skip to content

Instantly share code, notes, and snippets.

@xadapter
Created December 18, 2019 13:45
Show Gist options
  • Save xadapter/98c50da766ba36ac49ba3f76e28f4501 to your computer and use it in GitHub Desktop.
Save xadapter/98c50da766ba36ac49ba3f76e28f4501 to your computer and use it in GitHub Desktop.
woo-credits compatibility
// to display correct credit amount for line total and subtotal after order is placed. In case of person as booking, the credits should be multiplied with number of participants.
add_filter('woocommerce_order_subtotal_to_display', 'order_subtotal_to_display_with_credits', 11, 3);
add_filter('woocommerce_order_formatted_line_subtotal', 'order_formatted_line_subtotal_with_credits', 11, 3);
function order_subtotal_to_display_with_credits($subtotal, $compound, $order)
{
$order_id = (WC()->version < '2.7.0') ? $order->id : $order->get_id();
$credits_total = 0;
$payment_method = get_post_meta($order_id, '_payment_method', true);
if( $order )
{
$items = $order->get_items();
foreach( $items as $order_item_id => $line_item )
{
$formatted_data = $line_item->get_formatted_meta_data();
$product = $line_item->get_product();
$product_id = $product->get_id();
foreach ($formatted_data as $key => $value)
{
if($value->key == 'Number of persons' && !empty($value->value))
{
$persons_as_booking = get_post_meta( $product_id, "_phive_booking_persons_as_booking", 1 );
if(!empty($persons_as_booking) && $persons_as_booking == 'yes')
{
$no_of_persons = $value->value;
$credits_amount = get_post_meta($product_id, '_credits_amount', true);
if ($credits_amount || (is_numeric($credits_amount) && credits_amount==0))
{
$credits_total += $credits_amount * $no_of_persons;
}
}
}
}
}
}
if ($payment_method == 'wdc_woo_credits' && $credits_total != 0)
{
//for credit lable
if ($credits_total)
{
if ($credits_total > 1) {
$credits_label = trim(get_option('mwdcp_credits_label'));
$label = empty($credits_label) ? __('Credits', 'mwdcp') : $credits_label;
} else {
$credit_label = trim(get_option('mwdcp_credit_label'));
$label = empty($credit_label) ? __('Credit', 'mwdcp') : $credit_label;
}
$clabel = $credits_total . ' ' . $label;
} else if (is_numeric($credits) && $credits == 0) {
$credit_label = trim(get_option('mwdcp_credit_label'));
$label = empty($credit_label) ? __('Credit', 'mwdcp') : $credit_label;
$clabel = $credits_total . ' ' . $label;
}
$subtotal = $clabel;
}
return $subtotal;
}
function order_formatted_line_subtotal_with_credits($subtotal, $item, $order)
{
$order_id = (WC()->version < '2.7.0') ? $order->id : $order->get_id();
$payment_method = get_post_meta($order_id, '_payment_method', true);
if ($payment_method == 'wdc_woo_credits')
{
$product=$item->get_product();
$product_id=$product->get_id();
$product = wc_get_product($product_id);
$credits_amount = get_post_meta($product_id, '_credits_amount', true);
$formatted_data = $item->get_formatted_meta_data();
foreach($formatted_data as $key => $value)
{
if($value->key == 'Number of persons' && !empty($value->value))
{
$persons_as_booking = get_post_meta( $product_id, "_phive_booking_persons_as_booking", 1 );
if(!empty($persons_as_booking) && $persons_as_booking == 'yes')
{
$no_of_persons = $value->value;
$credits_amount = get_post_meta($product_id, '_credits_amount', true);
if ($credits_amount || (is_numeric($credits_amount) && credits_amount==0))
{
$credits_amount = $credits_amount * $no_of_persons;
}
}
}
}
if ($product->is_type('phive_booking'))
{
if ($credits_amount)
{
if ($credits_amount > 1)
{
$credits_label = trim(get_option('mwdcp_credits_label'));
$label = empty($credits_label) ? __('Credits', 'mwdcp') : $credits_label;
}
else
{
$credit_label = trim(get_option('mwdcp_credit_label'));
$label = empty($credit_label) ? __('Credit', 'mwdcp') : $credit_label;
}
$clabel = $credits_amount . ' ' . $label;
}
else if (is_numeric($credits) && $credits == 0)
{
$credit_label = trim(get_option('mwdcp_credit_label'));
$label = empty($credit_label) ? __('Credit', 'mwdcp') : $credit_label;
$clabel = $credits_amount . ' ' . $label;
}
$subtotal = $clabel;
}
}
return $subtotal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment