Member Avatar for anmol.raghuvanshi1

i have continued my developing my code on e-commerce website to grant the discount to client on basis of coupon code entered

<?php
/**
  * Consider and apply voucher codes
  * Types of voucher code
  * - = FIXED AMOUNT OFF
  * % = PERCENTAGE OFF
  * s = SET NEW SHIPPING COST
  * @param voucherCode String
  * @return void
  */
  if(isset($_POST["submit"])){
  $code=$_POST["code"];
  $coupon=new coupon();
  $coupon->Vouchers($code);
  }

  class coupon {

   function Vouchers(  Registry $registry, $voucherCode )
  {
    //The voucher code value is checked to ensure it is not empty 
    // (as per point 1)
    if( $voucherCode != '' )
    {
      // we need the date, to see if the voucher has expired
      $cts = date('Y-m-d H:i:s');
      $voucherCode=$this->registry;
      $voucher_sql = "SELECT *, if('{$cts}' > expiry, 1, 0)
                        AS expired FROM code
                      WHERE code='{$voucherCode}' LIMIT 1";
                      $voucher_sql=$this->registry;
      $this->registry->getObject('code')->executeQuery( $voucher_sql );
      if( $this->registry->getObject('code')->numRows() == 0 )
      {
        $this->voucher_notice = 'Sorry, the voucher code you entered
            is invalid';
      }
      else
      {
        $voucher = $this->registry->getObject('code')->getRows();
        if( $voucher['active'] == 1 )
        {
          if( $voucher['expired'] == 1 )
          {
            $this->voucher_notice = 'Sorry, this voucher has 
                expired';
            return false;
          }
          else
          {
            // check to see there are some vouchers, and customer
            // has enough in their basket (points 3 and 4)
            if( $voucher['num_vouchers'] != 0 )
            {
              if( $this->cost >= $voucher['min_cost'] )
              {
                $this->discountCode = $voucherCode;
                $this->discountCodeId = $voucher['id'];
                // If the discount operation is a percentage, then
                // the discount value is applied to the basket cost
                // to calculate that percentage. This amount is then
                // deducted from the order cost, giving a "discount
                // value"% discount from the order.
                if( $voucher['operation'] == '%' )
                {
                  $this->cost = $this->
              cost - (($this->cost)/100)*$voucher['amount'];
                  $this->voucher_notice = 'A ' 
                      . $voucher['amount'] 
                      . '% discount has been applied to your order';
                  return true;
                  // If the discount operation is a subtraction, then 
                  // the discount amount from the discount code is 
                  // deducted from the order cost, and the order cost 
                  // is updated to reflect this.
                }
                elseif( $voucher['operation'] == '-' )
                {
                  $this->cost = 
                      $this->cost - $voucher['amount'];
                  $this->voucher_notice = 'A discount of &pound;'
                      . $voucher['amount'] 
                      . ' has been applied to your order';
                  return true;
                  // Finally, if the discount operation is set to s 
                  // then, we set the shipping cost to the discount 
                  // value. This could allow us to set free shipping,
                  // or just reduce shipping costs.
                }
                elseif( $voucher['operation'] == 's' )
                {
                  $this->shipping_cost = $voucher['amount'];
                  $this->voucher_notice = 'Your orders shipping cost
                      has been reduced to &pound;' 
                      . $voucher['amount'];
                  return true;
                }
              }
              else
              {
                $this->voucher_notice = 'Sorry, your order total is 
                    not enough for your order to qualify for this 
                    discount code';
                return false;
              }
            }
            else
            {
              $this->voucher_notice = 'Sorry, this was a limited
                  edition voucher code, there are no more instances
                  of that code left';
              return false;
            }
          }
        }
        else
        {
          $this->voucher_notice = 'Sorry, the vocuher code you
              entered is no longer active';
          return false;
        }
      }
    }
  }
  }
  ?>

it gives me the

Catchable fatal error: Argument 1 passed to coupon::Vouchers() must be an instance of Registry, array given, called in C:\Web Project\xampp\htdocs\shop\a.php on line 14 and defined in C:\Web Project\xampp\htdocs\shop\a.php on line 19

// this is my html form
<form method="post" action="a.php">
    Enter The Coupon Code:<br />
    <input id="cod" name="code[cod]" type="text" size="10" />
    <br />
    <input type="submit" name="submit" value="submit" />
    </form>

On line 14 you are calling the Voucher method which expects two parameters: the object of the Registry class (first parameter) and the voucher code (second parameter). You supplied only one parameter, which should be the second one. Hence it is of wrong type. Supply both parameters (and both of correct type) and you should be OK.

Member Avatar for anmol.raghuvanshi1

i am really confused what this registry is this is code given to be to edit.....

Member Avatar for anmol.raghuvanshi1

can be there another way of implementing this i am not workin on any framework

It is not clear what the coupon class is expected to do. It seems that there is some code missing. The only method defined in it is the Voucher method. The method fires calls to some other methods and properties of the class but they are not defined anywhere. The class is also not the extension of some other class.

You should design a function or a class (if you are OK with OOP) yourself. Figure out and define what the functionality should be the code it, staring with simple functionality and adding the complexity if needed.

If you're not using a framework, then you should know exactly what Registry is. Search through your files for class Registry, this should help you find out what exactly Registry is. If you're using PHPStorm, you can option/alt+click on Registry to get straight into the code for the class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.