sammyskills / Opencart-Payment-Discount

3 stars 5 forks source link

feat: add discount indication to payment method #3

Open sammyskills opened 1 year ago

sammyskills commented 1 year ago

image

katalin2k commented 11 months ago

I made some modifications in catalog/model/extension/total/payment_discount.php Works but has a little issue: on mobile devices it hows the discount twice in the payment method title. Also I added sprintf to $desc_text so in backend settings you just need to add text like this: "%s discount on payment method"(this should be added as placeholder or tooltip for future users.

I can't seem to be able to fix the duplication issue on mobile devices.


class ModelExtensionTotalPaymentDiscount extends Controller
{
    public function getTotal($total)
    {
        if ($this->config->get('total_payment_discount_status') && $this->cart->getSubTotal()) {
            $discount_payment_method = $this->config->get('total_payment_discount_payment_type');
            $discount = $this->config->get('total_payment_discount_percentage');
        $desc_text = sprintf($this->config->get('total_payment_discount_description'), $discount.'%');
            $sort_order = $this->config->get('total_payment_discount_sort_order');

            // Loop through available payment methods
            foreach ($this->session->data['payment_methods'] as &$payment_method) {
                // Check if the current payment method matches
                if ($discount_payment_method == $payment_method['code']) {
                    // Update payment method title with discount percentage
                    $payment_method['title'] .= ' (-' . $discount . '%)';

                    // If payment method is selected, add the discount to totals
                    if ($this->session->data['payment_method']['code'] == $discount_payment_method) {
                        $total['totals'][] = array(
                            'code' => 'payment_discount',
                            'title' => $desc_text,
                            'value' => '-' . (($discount / 100) * $total['total']),
                            'sort_order' => $sort_order,
                        );

                        $total['total'] -= (($discount / 100) * $total['total']);
                    }
                }
            }
        }
    }
}
sammyskills commented 11 months ago

Nice work @katalin2k.

At a glance, I can guess that the issue is coming from the loop: the discount is appended to the payment method title more than once here: $payment_method['title'] .= ' (-' . $discount . '%)'.

I'll see how I can fix the issue and push an update as soon as I can.

Thank you.

sammyskills commented 11 months ago

Which version of opencart are you currently using?

katalin2k commented 11 months ago

3.0.3.8

On Sun, 15 Oct 2023 at 19:44, Samuel Asor @.***> wrote:

Which version of opencart are you currently using?

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763444094, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLDPAKMAUDAOHQZRSATX7QHH3ANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>

katalin2k commented 11 months ago

Dont forget about the description in backend. Add a tooltip or placeholder on how text should be added with %s.

On Sun, 15 Oct 2023 at 19:49, Catalin Florica @.***> wrote:

3.0.3.8

On Sun, 15 Oct 2023 at 19:44, Samuel Asor @.***> wrote:

Which version of opencart are you currently using?

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763444094, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLDPAKMAUDAOHQZRSATX7QHH3ANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>

sammyskills commented 11 months ago

Please try this code instead:


class ModelExtensionTotalPaymentDiscount extends Controller
{
    public function getTotal($total)
    {
        if ($this->config->get('total_payment_discount_status') && $this->cart->getSubTotal()) {
            $discount_payment_method = $this->config->get('total_payment_discount_payment_type');
            $discount = $this->config->get('total_payment_discount_percentage');
        $desc_text = sprintf($this->config->get('total_payment_discount_description'), $discount.'%');
            $sort_order = $this->config->get('total_payment_discount_sort_order');

            // Update the title of the discounted payment method
            $this->session->data['payment_methods'][$discounted_payment_method]['title'] = $this->session->data['payment_methods'][$discounted_payment_method]['title'] . ' (-' . $discount . '%)';
            // If payment method is selected, add the discount to totals
            if ($this->session->data['payment_method']['code'] == $discount_payment_method) {
                    $total['totals'][] = array(
                        'code' => 'payment_discount',
                        'title' => $desc_text,
                        'value' => '-' . (($discount / 100) * $total['total']),
                        'sort_order' => $sort_order,
                    );

                    $total['total'] -= (($discount / 100) * $total['total']);
            }
        }
    }
}
katalin2k commented 11 months ago

It creates another payment method, please check screenshot:

On Sun, 15 Oct 2023 at 20:25, Samuel Asor @.***> wrote:

Please try this code instead:

class ModelExtensionTotalPaymentDiscount extends Controller { public function getTotal($total) { if ($this->config->get('total_payment_discount_status') && $this->cart->getSubTotal()) { $discount_payment_method = $this->config->get('total_payment_discount_payment_type'); $discount = $this->config->get('total_payment_discount_percentage'); $desc_text = sprintf($this->config->get('total_payment_discount_description'), $discount.'%'); $sort_order = $this->config->get('total_payment_discount_sort_order');

        // Update the title of the discounted payment method
        $this->session->data['payment_methods'][$discounted_payment_method]['title'] = $this->session->data['payment_methods'][$discounted_payment_method]['title'] . ' (-' . $discount . '%)';
        // If payment method is selected, add the discount to totals
        if ($this->session->data['payment_method']['code'] == $discount_payment_method) {
                $total['totals'][] = array(
                    'code' => 'payment_discount',
                    'title' => $desc_text,
                    'value' => '-' . (($discount / 100) * $total['total']),
                    'sort_order' => $sort_order,
                );

                $total['total'] -= (($discount / 100) * $total['total']);
        }
    }
}

}

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763453775, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLGPPOGY2JFKA4TRHCLX7QMCBANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>

sammyskills commented 11 months ago

Can't find the screenshot you are talking about. But update the code as follows:


// Update the title of the discounted payment method
if (isset($this->session->data['payment_methods'])) {
    $this->session->data['payment_methods'][$discounted_payment_method]['title'] = $this->session->data['payment_methods'][$discounted_payment_method]['title'] . ' (-' . $discount . '%)';
}
katalin2k commented 11 months ago

Now it just creates a new payment method but it just shows the discount. Its not duplicated.

On Sun, 15 Oct 2023 at 21:41, Samuel Asor @.***> wrote:

Can't find the screenshot you are talking about. But update the code as follows:

// Update the title of the discounted payment methodif (isset($this->session->data['payment_methods'])) { $this->session->data['payment_methods'][$discounted_payment_method]['title'] = $this->session->data['payment_methods'][$discounted_payment_method]['title'] . ' (-' . $discount . '%)'; }

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763471844, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLGJQKJD77XAV2OTWRLX7QU5ZANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>

sammyskills commented 11 months ago

Screenshot?

katalin2k commented 11 months ago

I’m on mobile right now but here it is

On Sun, 15 Oct 2023 at 21:54, Samuel Asor @.***> wrote:

Screenshot?

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763474726, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLDZZCGALNJZDPBD2B3X7QWPLANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>

sammyskills commented 11 months ago

Your screenshots do not display when you send via email.

Try replying directly on github.

katalin2k commented 11 months ago

IMG_1227 /

katalin2k commented 11 months ago

I uploaded it on github but please contact me directly by email, its easier this way

On Sun, 15 Oct 2023 at 22:04, Samuel Asor @.***> wrote:

Your screenshots do not display when you send via email.

Try replying directly on github.

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763476904, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLAGBQG3WQINPAJJJLLX7QXVHANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>

katalin2k commented 11 months ago

I think I fixed it. Please check the code: [image: File] payment_discount.php https://drive.google.com/file/d/1j9XDPKkKUzhtKXjcK2x4kws6cBPAR7tt/view?usp=drivesdk

On Sun, 15 Oct 2023 at 22:08, Catalin Florica @.***> wrote:

I uploaded it on github but please contact me directly by email, its easier this way

On Sun, 15 Oct 2023 at 22:04, Samuel Asor @.***> wrote:

Your screenshots do not display when you send via email.

Try replying directly on github.

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763476904, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLAGBQG3WQINPAJJJLLX7QXVHANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>

sammyskills commented 11 months ago

I don't think this is a full fix.

This will not work for a new user. You can try by clearing your browser session and cookies, then try to checkout.

katalin2k commented 11 months ago

Why not if it checks if the discount is already added after payment method title?

On Sun, 15 Oct 2023 at 23:19, Samuel Asor @.***> wrote:

I don't think this is a full fix.

This will not work for a new user. You can try by clearing your browser session and cookies, then try to checkout.

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763494403, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLAITV2OU2A7SGNFPODX7RAOPANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>

katalin2k commented 11 months ago

Check now. Seems to be working fine now. [image: File] payment_discount.php https://drive.google.com/file/d/1Gqy4F1bDgAzhdanApKojb9XkVPJ3Wxnl

On Sun, 15 Oct 2023 at 23:23, Catalin Florica @.***> wrote:

Why not if it checks if the discount is already added after payment method title?

On Sun, 15 Oct 2023 at 23:19, Samuel Asor @.***> wrote:

I don't think this is a full fix.

This will not work for a new user. You can try by clearing your browser session and cookies, then try to checkout.

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763494403, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLAITV2OU2A7SGNFPODX7RAOPANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>

sammyskills commented 11 months ago

On initial visit, a user will be presented with the error:

image

katalin2k commented 11 months ago

I modified the condition a little and seems to be working fine now. I will test further

On Sun, 15 Oct 2023 at 23:30, Samuel Asor @.***> wrote:

On initial visit, a user will be presented with the error:

[image: image] https://user-images.githubusercontent.com/8720569/275353039-5d34fe98-2abe-405f-8c05-6dd4a808d8a8.png

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763496603, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLHX35DENGNFODNA3TLX7RBW5ANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>

katalin2k commented 11 months ago

For me works perfectly fine. Probably because I use Journal3 theme and it auto selects first available payment method. You should investigate more if you think it’s not a full fix.

On Sun, 15 Oct 2023 at 23:30, Samuel Asor @.***> wrote:

On initial visit, a user will be presented with the error:

[image: image] https://user-images.githubusercontent.com/8720569/275353039-5d34fe98-2abe-405f-8c05-6dd4a808d8a8.png

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763496603, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLHX35DENGNFODNA3TLX7RBW5ANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>

sammyskills commented 11 months ago

Yes, this fix is not applicable to all themes. It doesn't work on the default theme either.

katalin2k commented 11 months ago

This is the most I can do. I gave you the start, now you should fine a fix for default theme. Note that this works for Journal3 theme with auto select first available payment method.

On Sun, 15 Oct 2023 at 23:42, Samuel Asor @.***> wrote:

Yes, this fix is not applicable to all themes. It doesn't work on the default theme either.

— Reply to this email directly, view it on GitHub https://github.com/sammyskills/Opencart-Payment-Discount/issues/3#issuecomment-1763498980, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6EQSLGAQAJ7FY3UJRPQMQTX7RDDNANCNFSM6AAAAAA4BKOK3Q . You are receiving this because you were mentioned.Message ID: @.***>