Skip to content

Commit

Permalink
Fix: links de pagamento para renovação agora estão visíveis. O botão …
Browse files Browse the repository at this point in the history
…para acessar o link de pagamento direciona para a página 'view-order' para pedidos de renovação. Também foram implementadas melhorias nas restrições e na exibição de mensagens, facilitando o acesso ao link de pagamento.
  • Loading branch information
lucastgama committed Oct 11, 2024
1 parent 4d95d1c commit 21da434
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
51 changes: 36 additions & 15 deletions src/includes/admin/ButtonPaymentLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,56 @@ public function button_link_payment($order)
}

$order_data = $this->get_order_data($order);
$is_renewal = get_post_meta($order->get_id(), '_subscription_renewal', true);

if ($order && empty($is_renewal)) {

if ($order) {
$item = $order_data['has_item'];
$single = $order_data['has_single_product'];
$sub = $order_data['has_subscription'];
$status = $order_data['order_status'];
$link = $order_data['link_payment'];
$shop = $order_data['urlShopSubscription'];
$type = get_post_type($order->get_id());
$created = $order->get_created_via();
$parent = $order_data['parent'];
$disable = $this->should_disable($created, $sub, $item, $order);
$disable = $this->should_disable($created, $sub, $item, $order, $order_data);
$hasClient = $order->get_customer_id();
$order_info = compact('type', 'created', 'parent', 'disable', 'hasClient');
$variables = compact('item', 'sub', 'status', 'link', 'shop');
$variables = compact('item', 'sub', 'status', 'link', 'shop', 'single');
$this->include_template_with_variables($template_path, $variables, $order_info);
}
}

private function should_disable($created, $has_sub, $has_item, $order)
private function should_disable($created, $has_sub, $has_item, $order, $order_data)
{
$posttype = get_post_type();
$hasClient = $order->get_customer_id();

if (!$hasClient || !$has_item) {
return false;
}

if ($posttype == 'shop_order') {
return $this->evaluate_shop_order($has_sub, $created);
}

if ($posttype == 'shop_subscription') {
return $this->evaluate_shop_subscription($has_sub, $created);
return $this->evaluate_shop_subscription($has_sub, $created, $order_data);
}

return false;
}

private function evaluate_shop_order($has_sub, $created)
{
if ($has_sub && $created == "admin") {
return false;
}
return true;
}
private function evaluate_shop_subscription($has_sub, $created)

private function evaluate_shop_subscription($has_sub, $created, $order_data)
{
if ($has_sub && $created == "admin") {
if ($has_sub && $created == "admin" && !$order_data['has_single_product']) {
return true;
}
return false;
Expand All @@ -80,6 +80,7 @@ private function get_order_data($order)
$order_data = [
'has_item' => false,
'has_subscription' => false,
'has_single_product' => false,
'order_status' => $order->get_status(),
'link_payment' => null,
'urlAdmin' => get_admin_url(),
Expand All @@ -88,6 +89,8 @@ private function get_order_data($order)
];
if (count($order->get_items()) > 0) {
$order_data['has_subscription'] = $this->has_subscription($order);
$order_data['has_single_product'] = $this->has_single_product($order);

if ($order->get_checkout_payment_url()) {
$order_data['link_payment'] = $this->build_payment_link($order, $order->get_payment_method());
}
Expand Down Expand Up @@ -128,6 +131,18 @@ public function has_subscription($order)
return false;
}

private function has_single_product($order)
{
$subscriptions_product = new WC_Subscriptions_Product();
$order_items = $order->get_items();
foreach ($order_items as $order_item) {
if(!$subscriptions_product->is_subscription($order_item->get_product_id())){
return true;
}
}
return false;
}

/*
* Build the payment link (Dummy function for illustration).
* @param WC_Order $order The order object.
Expand All @@ -140,6 +155,12 @@ public function build_payment_link($order, $gateway)
$gateway = $gateway ? "&vindi-gateway={$gateway}" : '';
$orderId = $order->get_id();
$orderKey = $order->get_order_key();
$is_renewal = get_post_meta($order->get_id(), '_subscription_renewal', true);

if($is_renewal){
$url = get_site_url();
return "{$url}/my-account/view-order/{$orderId}";
}

return "{$url}order-pay/{$orderId}/?pay_for_order=true&key={$orderKey}&vindi-payment-link=true{$gateway}";
}
Expand Down
3 changes: 1 addition & 2 deletions src/includes/admin/WcVindiColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ public function custom_shop_order_column_data($column)
return;
}
$order = wc_get_order($post->ID);
$is_renewal = get_post_meta($post->ID, '_subscription_renewal', true);

if ($column === 'vindi_payment_link' && empty($is_renewal)) {
if ($column === 'vindi_payment_link') {
if (count($order->get_items()) > 0) {
$status = $order->get_status();
$gateway = $order->get_payment_method();
Expand Down
7 changes: 5 additions & 2 deletions src/templates/admin-payment-button.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<?php else : ?>
<div style="display: flex;gap: 6px; width: 100%;margin-top: 6px;">
<div style="display: flex;gap: 6px; width: 100%;">
<?php $is_disabled = ($sub && $item && $parent) ? 'enable' : 'disabled'; ?>
<?php $is_disabled = ($disable) ? 'enable' : 'disabled'; ?>
<a class="buttonPaymentLink <?php echo $is_disabled; ?>"
target="<?php echo $item ? esc_attr('_blank') : ''; ?>"
href="<?php echo $is_disabled == 'enable' ? esc_url($link) : '#'; ?>">
Expand All @@ -83,7 +83,10 @@
?>
</span>
<?php endif; ?>
<?php if ($item && !$sub) : ?>
<?php if ($single && $sub) : ?>
<?php error_log(var_export($single,true)); ?>
<?php error_log(var_export($sub,true)); ?>

<span class="notificationPaymentLink">
<?php
echo esc_html__(
Expand Down

0 comments on commit 21da434

Please sign in to comment.