Skip to content

Instantly share code, notes, and snippets.

@ricardobrg
Last active November 23, 2020 13:29
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardobrg/61b3414c809e6ec3683b1219d9f7dfee to your computer and use it in GitHub Desktop.
Save ricardobrg/61b3414c809e6ec3683b1219d9f7dfee to your computer and use it in GitHub Desktop.
Add instalments in product page
<?php
add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
global $woocommerce, $product;
// let's setup our divs
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;display:none">%s %s</div>','Total do Produto','<span class="price">'.$product->get_price().'</span>');
echo sprintf('<div id="cart_total_price" style="margin-bottom:20px;display:none">%s %s</div>','Total do Carrinho','<span class="price">'.$product->get_price().'</span>');
?>
<script>
jQuery(function($){
var price = <?php echo $product->get_price(); ?>,
current_cart_total = <?php echo $woocommerce->cart->cart_contents_total; ?>,
currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
$('[name=quantity]').change(function(){
if (!(this.value < 1)) {
var product_total = parseFloat(price * this.value),
cart_total = parseFloat(product_total + current_cart_total);
$('#product_total_price .price').html( currency + product_total.toFixed(2));
$('#cart_total_price .price').html( currency + cart_total.toFixed(2));
var coef = [1, 0.52255, 0.35347, 0.26898, 0.21830, 0.18453,0.16044, 0.14240, 0.12838,0.11717, 0.10802, 0.10040];
$('#parcel_1 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[0]).toFixed(2)).replace('.',',')));
$('#parcel_2 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[1]).toFixed(2)).replace('.',',')));
$('#parcel_3 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[2]).toFixed(2)).replace('.',',')));
$('#parcel_4 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[3]).toFixed(2)).replace('.',',')));
$('#parcel_5 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[4]).toFixed(2)).replace('.',',')));
$('#parcel_6 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[5]).toFixed(2)).replace('.',',')));
$('#parcel_7 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[6]).toFixed(2)).replace('.',',')));
$('#parcel_8 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[7]).toFixed(2)).replace('.',',')));
$('#parcel_9 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[8]).toFixed(2)).replace('.',',')));
$('#parcel_10 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[9]).toFixed(2)).replace('.',',')));
$('#parcel_11 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[10]).toFixed(2)).replace('.',',')));
$('#parcel_12 .woocommerce-Price-amount.amount').html( currency + (((product_total * coef[11]).toFixed(2)).replace('.',',')));
}
$('#product_total_price,#cart_total_price').toggle(!(this.value < 1));
});
});
</script>
<?php
}
@cleucimar
Copy link

ricardo qual pagina no woocommerce que insiro esse codigo?

@eliasfaical
Copy link

@cleucimar inclui no function.php

@murilopratavieira
Copy link

@cleucimar inclui no function.php

Tentei ir lá no editar tema e colocar no function, mas não funcionou com o método de pagamento que eu tenho (ASAAS), sabe se pode ser alguma coisa do tipo?

O método de pagamento estava configurado para parcelamento em até 5 vezes.

@ricardobrg
Copy link
Author

@murilopratavieira esse código inclui as parcelas no sumário do produto, independente do método de pagamento. Precisa ajustar os coeficientes de correção das parcelas ali na linha 23, de acordo com as suas taxas. Se não está aparecendo provavelmente é por causa do tema ou algum outro plugin que usa o hook woocommerce_single_product_summary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment