Skip to content

Commit

Permalink
Tax rate based on amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
fbognini committed Dec 8, 2024
1 parent 5b0d815 commit ff32b59
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions voucherly.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ private function customerPaymentMethodsToWoocommercePaymentTokens($customerId, $
$tokens[] = $token;
}

$index++;
++$index;
}

return $tokens;
Expand Down Expand Up @@ -774,51 +774,60 @@ private function getPaymentLines(WC_Order $order)
{
$lines = [];

$cart_items = WC()->cart->get_cart();

$foodCategoryId = $this->get_option('foodCategory');

foreach ($cart_items as $key => $item) {
foreach (WC()->cart->get_cart() as $key => $item) {
$product = wc_get_product($item['product_id']);

$quantity = $item['quantity'];
$lineNetAmount = $item['line_total'];
$unitTotal = round(($lineNetAmount + $item['line_tax']) / $quantity, 2);
$lineAmount = $unitTotal * $quantity;

$line = new CreatePaymentRequestLine();
$line->productName = $product->get_name();
$line->productImage = wp_get_attachment_image_src(get_post_thumbnail_id($product->get_id()), 'full')[0];
$line->productImage = wp_get_attachment_image_src(get_post_thumbnail_id($item['product_id']), 'full')[0];
$line->unitAmount = round($product->get_regular_price() * 100);
if ($product->get_sale_price()) {
$line->unitDiscountAmount = round($line->unitAmount - $product->get_sale_price() * 100);
}
$line->quantity = $item['quantity'];
$line->isFood = true;

$tax_class = $product->get_tax_class();
$tax_rates = WC_Tax::get_rates( $tax_class );
if ( ! empty( $tax_rates ) ) {
$line->taxRate = reset( $tax_rates )['rate'];
}
$line->unitDiscountAmount = $line->unitAmount - ($unitTotal * 100);
$line->quantity = $quantity;

if (isset($foodCategoryId) && !empty($foodCategoryId)) {
$categorys = $product->get_category_ids();
$line->isFood = in_array($foodCategoryId, $categorys, true);
$line->isFood = in_array($foodCategoryId, $product->get_category_ids(), true);
} else {
$line->isFood = true;
}

$line->taxRate = $this->calculateTaxRate($lineAmount - $lineNetAmount, $lineNetAmount);

$lines[] = $line;
}

foreach ($order->get_shipping_methods() as $shipping_method) {
$totalWithTax = $shipping_method->get_total() + $shipping_method->get_total_tax();
$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');

if ($totalWithTax <= 0) {
foreach (WC()->shipping()->get_packages() as $package_key => $package) {
if (!isset($package['rates']) && empty($package['rates'])) {
continue;
}
// Get the chosen shipping method for this package
$chosen_method_id = isset($chosen_shipping_methods[$package_key])
? $chosen_shipping_methods[$package_key]
: '';

if ($chosen_method_id && isset($package['rates'][$chosen_method_id])) {
$shipping_method = $package['rates'][$chosen_method_id];

$shipping = new CreatePaymentRequestLine();
$shipping->productName = $shipping_method->get_method_title();
$shipping->unitAmount = round($totalWithTax * 100);
$shipping->quantity = $shipping_method->get_quantity();
$shipping->isFood = 'yes' === $this->get_option('shippingAsFood');
$shippingTaxAmount = $shipping_method->get_shipping_tax();
$shippingNetAmount = $shipping_method->get_cost();

$lines[] = $shipping;
$shipping = new CreatePaymentRequestLine();
$shipping->productName = $shipping_method->get_label();
$shipping->unitAmount = round(($shippingTaxAmount + $shippingNetAmount) * 100);
$shipping->quantity = 1;
$shipping->isFood = 'yes' === $this->get_option('shippingAsFood');
$shipping->taxRate = $this->calculateTaxRate($shippingTaxAmount, $shippingNetAmount);

$lines[] = $shipping;
}
}

return $lines;
Expand All @@ -845,6 +854,15 @@ private function getPaymentDiscounts()
return $discounts;
}

private function calculateTaxRate($taxAmount, $netAmount)
{
if (0 === $netAmount || 0 === $taxAmount) {
return 0.0;
}

return round($taxAmount / $netAmount * 100, 2);
}

private function getVoucherlyCustomerUserMetaKey(): string
{
return 'yes' === 'voucherly_customer_'.$this->get_option('sandbox') ? 'sand' : 'live';
Expand Down

0 comments on commit ff32b59

Please sign in to comment.