mateusneves
1/16/2018 - 3:23 PM

Woocommerce Bank Ticket Discount PagSeguro

Whit this snippet you can add a percentage discount for payment with Bank Ticket for PagSeguro for Woocommerce plugin.

// Source: https://br.wordpress.org/support/topic/woocommerce-alterar-o-valor-do-pedido-apos-o-pagamento/

add_filter( 'woocommerce_pagseguro_payment_xml', function( $xml, $order ) {

	$newxml = $xml;

	if($newxml->method == 'boleto'){
		$total = $order->total;
		$discount_value = $total * 0.1;
		$boleto_cost = 1;
		$new_total = $discount_value;
		$new_total = '-'.round($new_total, 2);
		$new_total = number_format($new_total, 2);
		$newxml->addChild( 'extraAmount', $new_total );
		$fee = new \WC_Order_Item_Fee();
		$fee->set_props( array(
			'name'      => 'Desconto no Boleto',
			'tax_class' => 0,
			'total'     => -($discount_value - $boleto_cost),
			'total_tax' => 0,
			'taxes'     => array(
				'total' => array( 0 ),
			),
			'order_id'  => $order->get_id(),
		) );
		$fee->save();
		$order->add_item( $fee );
		$order->update_taxes();
		$order->calculate_totals();
		$order->save();
	}

	return $newxml;
}, 10, 2 );