Forum

Thread tagged as: Question, Shop

Free products issue

Hi,

I'm attempting to allow customers to "purchase" free products, and as such I need to use the manual payment gateway.

My issue is that I'm unsure how to check this in PHP.

This is my checkout function, which works:

    <?php

      // if (!perch_shop_addresses_set() || !perch_member_logged_in())  {
      if (!perch_shop_addresses_set()) {

        perch_shop_edit_address_form(false, [
          'template' => 'checkout/customer_create_passwordless.html',
        ]);

      } else {

        // your 'success' return URL
        $return_url = 'https://xx/paypal-complete.php';
        $cancel_url = 'https://xx/paypal-failed.php';

        perch_shop_checkout('paypal-express', [
          'return_url' => $return_url,
          'cancel_url' => $cancel_url,
        ]);


      }
    ?>

I thought I'd be able to check the cart total value and if it is 0 use the manual payment gateway.

I tried adding this check, but it's not working for me:

      $cart_total = perch_shop_cart_total();

      if ($cart_total=='£0.00') {
        // your 'success' return URL
        $return_url = 'https://xx/manual-complete.php';
        $cancel_url = 'https://xx/manual-failed.php';

        perch_shop_checkout('manual', [
          'return_url' => $return_url,
          'cancel_url' => $cancel_url,
        ]);
      }

Any ideas how I could get this up and running?

Chris McMahon

Chris McMahon 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you echo out the value of $cart_total what does it output?

echo $cart_total; doesn't actually print anything. I assume this is because perch_shop_cart_total() is already printing to the page. echo is working for other variables & strings, though.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hi Chris,

perch_shop_cart_total() prints the value by default. Use this to return the value:

$cart_total = perch_shop_cart_total([], true);

Wonderful, thank you Hussein.

Edit: Thanks again, that worked a treat.