[1.3.4] Currency Promocodes

Forum
Last Post
Threads / Messages

KittHaven

Member
Member
Joined
May 21, 2013
Messages
478
Points
28
Age
25
Location
Devon, UK
Mysidian Dollar
8,292
Hello! This is my first ever tutorial on here lol. Anyway, I wanted the ability to make a promocode to give a user some money (and even premium currency) so I managed to do that. :meow: It also has adminCP functions so you can just add promos like you normally would!

First of all, open admincp/view/promoview.php.

Find this, in public function add():

PHP:
		    $typesList = new RadioList("type");
		    $typesList->add(new RadioButton(" Adoptables", "type", "Adopt"));
		    $typesList->add(new RadioButton(" Items", "type", "Item"));
		    $typesList->add(new RadioButton(" Pages", "type", "Page"));

Change those 4 lines to:

PHP:
		    $typesList = new RadioList("type");
		    $typesList->add(new RadioButton(" Adoptables", "type", "Adopt"));
		    $typesList->add(new RadioButton(" Items", "type", "Item"));
			$typesList->add(new RadioButton("Currency", "type", "Currency"));
		    $typesList->add(new RadioButton(" Pages", "type", "Page"));

See where I just added a currency line?

Now, scroll down further until you find the public function edit() section. Find the same 3 lines and add the new currency line. Save the file.

Find and open classes/class_promocode.

Find public function execute(){ and replace it with this:

PHP:
  public function execute(){
      // This method will execute the promocode and give users their desired adoptables or items, need to be used after validation is completed
	  $mysidia = Registry::get("mysidia");
      $document = $mysidia->frame->getDocument();
	  if($this->valid != TRUE) throw new NoPermissionException($mysidia->lang->validate);
	  
	  switch($this->type){
	     case "Adopt":
		    // The user will receive an adoptable from the promocode now.
		    $code = codegen(10, 0);
			$genders = array('f', 'm');
			$rand = rand(0,1);
	        $mysidia->db->insert("owned_adoptables", array("aid" => NULL, "type" => $this->reward, "name" => $this->reward, "owner" => $this->user, "currentlevel" => 0, "totalclicks" => 0, "code" => $code, 
			                                               "imageurl" => NULL, "alternate" => 0, "tradestatus" => 'fortrade', "isfrozen" => 'no', "gender" => $genders[$rand], "lastbred" => 0, "originalowner" => $mysidia->user->username, "birthday" => date("F jS, Y")));
			$document->addLangvar("Congrats, you have acquired the adoptable {$this->reward} by entering promocode.");
			$this->usepromo();
		    break;
		 case "Item":
		    // The user will receive an item from the promocode now.
    	
			$item = new StockItem($this->reward, 1);
			$item->assign($this->user);
			$newquantity = $item->getoldquantity() + 1;
			if($item->cap != 0 and $newquantity > $item->cap){
                throw new NoPermissionException("It appears that you cannot add one more of item {$this->reward} to your inventory, its quantity has already exceeded the upper limit.");
			}
			else{
			   $item->append();
			   $document->addLangvar("Congrats, you have acquired the item {$this->reward} by entering promocode.");
			   $this->usepromo();
			}  
		    break;
         case "Page":
            $this->usepromo();
            break;
		 case "Currency":
			// The user will receive currency from the promocode now.
			$cost = $mysidia->settings->cost; //The name of your site's currency
			$cash = $mysidia->db->select("users", array("money"), "username ='{$mysidia->user->username}'")->fetchColumn(); //How much cash the user has
			$new_cash = $cash + $this->reward; //Simple sum to calculate user's current cash + reward
		    $mysidia->db->update("users", array("money" => $new_cash), "username ='{$mysidia->user->username}'");  //Update the database with the new cash amount
			$document->addLangvar("Congrats, you have acquired {$this->reward} {$cost} by entering promocode.");
			$this->usepromo();
		    break;
		 default:
		    throw new InvalidIDException($mysidia->lang->type);	 
	  }
	  // All done, we're good to go!
  }

You can see the part that I added at the bottom there. That section makes it so that currency can be given!

Premium currency tutorial on next comment!
 
Adding Premium Currency

First of all, follow this tutorial to add premium currency to your site

Then in admincp/view/promoview.php, add this line...

PHP:
$typesList->add(new RadioButton("Premium Currency", "type", "Premium Currency"));

...in both the add() and edit() sections. (Use the previous part of this tutorial for guidance)

Then in classes/class_promocode.php, add this...

PHP:
 case "Premium Currency":
			// The user will receive premium currency from the promocode now.
			$premiumcost = $mysidia->settings->premiumcurrency; //The name of your site's premium currency
			$premiumcash = $mysidia->db->select("users", array("premiumcurrency"), "username ='{$mysidia->user->username}'")->fetchColumn(); //How much premium currency the user has
			$new_premiumcash = $premiumcash + $this->reward; //Simple sum to calculate user's current cash + reward
		    $mysidia->db->update("users", array("premiumcurrency" => $new_premiumcash), "username ='{$mysidia->user->username}'");  //Update the database with the new premium currency amount
			$document->addLangvar("Congrats, you have acquired {$this->reward} {$premiumcost} by entering promocode.");
			$this->usepromo();
		    break;

...underneath the currency section you just added. (It should go under the break;)

Make sure you look through the code and see if it matches up with your own database! If you named the premium currency field (for example) anything other than 'premiumcurrency' while adding it to your site, you might have to tweak it. I'm willing to help if you need it, just comment below.

Also, this doesn't go over tweaking the descriptions that show up to say you can add currency now. That should be easy enough to do by just finding those lines in promoview.php yourself and making it say whatever you want it to! :usedusedused:

I hope this helps someone! It's very simple but I was quite proud of myself for managing it with no help lol.
 
I just tested this out, I'm surprised no one commented, this is brilliant!!

Thank you for this! :)
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,274
Messages
33,115
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Latest Posts

Top