how to get cart values in codeigniter for paypal payment gateway - php

I am stuck on integrating paypal payment gateway on codeigniter
I am using the current library
https://github.com/romaldyminaya/ci_paypal
As the library contains only static values of the items , but i need to get the items that are coming from my cart, how can i get them to the $this->paypal->add('T-shirt',2.99,6);
my controller function:
public function do_purchase(){
$config['business'] = 'babar.seller#gmail.com';
$config['cpp_header_image'] = ''; //Image header url [750 pixels wide by 90 pixels high]
$config['return'] = 'http://localhost/final-project/index.php/mysite/modify_payment';
$config['cancel_return'] = 'http://final-project/mysite/canceled.php';
$config['notify_url'] = 'process_payment.php'; //IPN Post
$config['production'] = FALSE; //Its false by default and will use sandbox
$config["invoice"] = random_string('numeric',8); //The invoice id
$this->load->library('paypal',$config);
#$this->paypal->add(<name>,<price>,<quantity>[Default 1],<code>[Optional]);
$this->paypal->add('T-shirt',2.99,6); //First item
$this->paypal->add('Pants',40); //Second item
$this->paypal->add('Blowse',10,10,'B-199-26'); //Third item with code
$this->paypal->pay(); //Proccess the payment
}
public function modify_payment(){
$received_data = print_r($this->input->post(),TRUE);
echo "<pre>".$received_data."<pre>";
$this->load->view('mysite/modify_payment',$received_data);
}
public function cart()
{
$cart = $this->cart->contents();
//print_r($cart);
//die;
//$data['user_id']=$this->mainmodel->get_user_i($user_id);
$this->load->view('mysite/cart');
$this->load->view('mysite/include/footer');
}
cart view :
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<tr>
<td> <input name="qty[<?php echo $items['rowid'];?>]" id="qty" type="text" value="<?php echo $items['qty']?>" class="input" />
</td>
<td>
<?php echo $items['name']; ?>
<?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
<p>
<?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
<?php endforeach; ?>
</p>
<?php endif; ?>

function add($item_name = '',$item_amount = NULL,$item_qty = NULL,$item_number = NULL){
$this->config['item_name_'.$this->item] = $item_name;
$this->config['amount_'.$this->item] = $item_amount;
$this->config['quantity_'.$this->item] = $item_qty;
$this->config['item_number_'.$this->item] = $item_number;
$this->item++;
}
What i did is had a paypal library class that handled the addition and just simply hard coded inn some products

Related

How to set a minimum order price within this code

Looking at a piece of code at the moment and trying to work out how to set a minimum order price of £10 within this code. I want the 'Confirm your order' CTA to be inactive unless the order is £10 - Or maybe upon selecting the CTA, no action, just a message to display saying the order should be £10 or over when the CTA is selected.
Any help would be great, I have a huge headache from this. I've been trying to learn to code but I am in way over my head with this one.
<?php
use mp_restaurant_menu\classes\models;
use mp_restaurant_menu\classes\View;
/**
* #return mixed
*/
function mprm_is_checkout() {
return models\Checkout::get_instance()->is_checkout();
}
/**
* #return mixed
*/
function mprm_get_checkout_uri() {
return models\Checkout::get_instance()->get_checkout_uri();
}
function mprm_get_checkout_cart_template() {
$data = array();
$data[ 'is_ajax_disabled' ] = models\Settings::get_instance()->is_ajax_disabled();
$data[ 'cart_items' ] = models\Cart::get_instance()->get_cart_contents();
mprm_get_template('shop/checkout-cart', $data);
}
/**
* #return mixed
*/
function mprm_checkout_button_next() {
$color = mprm_get_option('checkout_color', 'mprm-btn blue');
$color = ($color == 'inherit') ? '' : $color;
$padding = mprm_get_option('checkout_padding', 'mprm-inherit');
$style = mprm_get_option('button_style', 'button');
$purchase_page = mprm_get_option('purchase_page', '0');
ob_start();
?>
<input type="hidden" name="mprm_action" value="gateway_select"/>
<input type="hidden" name="page_id" value="<?php echo absint($purchase_page); ?>"/>
<input type="submit" name="gateway_submit" id="mprm_next_button" class="mprm-submit <?php echo $color; ?> <?php echo $padding; ?> <?php echo $style; ?>" value="<?php _e('Next', 'mp-restaurant-menu'); ?>"/>
<?php
return apply_filters('mprm_checkout_button_next', ob_get_clean());
}
/**
* Checkout purchase button
* #return mixed
*/
function mprm_checkout_button_purchase() {
$color = mprm_get_option('checkout_color', 'mprm-btn blue');
$color = ($color == 'inherit') ? '' : $color;
$style = mprm_get_option('button_style', 'button');
$label = mprm_get_option('checkout_label', '');
$padding = mprm_get_option('checkout_padding', 'mprm-inherit');
if (mprm_get_cart_total()) {
$complete_purchase = !empty($label) ? $label : __('Purchase', 'mp-restaurant-menu');
} else {
$complete_purchase = !empty($label) ? $label : __('Free Menu item', 'mp-restaurant-menu');
}
ob_start();
?>
<input type="submit" class="mprm-submit <?php echo $color; ?> <?php echo $padding; ?> <?php echo $style; ?>" id="mprm-purchase-button" name="mprm-purchase" value="<?php echo $complete_purchase; ?>"/>
<?php
return apply_filters('mprm_checkout_button_purchase', ob_get_clean());
}
/**
* #return bool
*/
function mprm_is_no_guest_checkout() {
return models\Misc::get_instance()->no_guest_checkout();
}
function mprm_checkout_tax_fields() {
if (models\Taxes::get_instance()->cart_needs_tax_address_fields() && mprm_get_cart_total()) {
mprm_default_cc_address_fields();
}
}
function mprm_checkout_submit() { ?>
<fieldset id="mprm_purchase_submit">
<?php do_action('mprm_purchase_form_before_submit'); ?>
<?php mprm_checkout_hidden_fields(); ?>
<?php echo mprm_checkout_button_purchase(); ?>
<?php do_action('mprm_purchase_form_after_submit'); ?>
</fieldset>
<?php
}
function mprm_checkout_additional_information() {
View::get_instance()->get_template('/shop/checkout-additional-information');
}
function mprm_checkout_final_total() {
?>
<p id="mprm_final_total_wrap">
<strong><?php _e('Total:', 'mp-restaurant-menu'); ?></strong>
<span class="mprm_cart_amount" data-subtotal="<?php echo mprm_get_cart_subtotal(); ?>" data-total="<?php echo mprm_get_cart_subtotal(); ?>"><?php echo mprm_currency_filter(mprm_format_amount(mprm_get_cart_total())); ?></span>
</p>
<?php
}
function mprm_checkout_hidden_fields() {
?>
<?php if (is_user_logged_in()) { ?>
<input type="hidden" name="mprm-user-id" value="<?php echo get_current_user_id(); ?>"/>
<?php } ?>
<input type="hidden" name="mprm_action" value="purchase"/>
<input type="hidden" name="controller" value="cart"/>
<input type="hidden" name="mprm-gateway" value="<?php echo models\Gateways::get_instance()->get_chosen_gateway(); ?>"/>
<?php
}
/**
* Delivery address default
*/
function mprm_checkout_delivery_address() {
if (mprm_get_option('shipping_address')): ?>
<p id="mprm-address-wrap">
<label for="shipping_address" class="mprm-label">
<?php _e('Shipping address:', 'mp-restaurant-menu'); ?>
</label>
<input type="text" name="shipping_address" value="" class="medium-text" placeholder="<?php _e('Enter your address.', 'mp-restaurant-menu'); ?>"/>
</p>
<?php endif;
}
/**
* Checkout order notes
*/
function mprm_checkout_order_note() {
?>
<p id="mprm-phone-number-wrap">
<label for="customer_note" class="mprm-label">
<?php _e('Order notes:', 'mp-restaurant-menu'); ?>
</label>
<textarea type="text" name="customer_note" id="customer_note" class="phone-number mprm-input"></textarea>
</p>
<?php }
/**
* Summary table
*/
function mprm_checkout_summary_table() { ?>
<span class="mprm-payment-details-label"><legend><?php _e('Order totals', 'mp-restaurant-menu'); ?></legend></span>
<table class="mprm-table">
<?php do_action('mprm_checkout_table_subtotal_before'); ?>
<tr>
<td class=""><span><?php _e('Subtotal', 'mp-restaurant-menu'); ?> </span></td>
<td><span class="mprm_cart_subtotal_amount"><?php echo mprm_currency_filter(mprm_format_amount(mprm_get_cart_subtotal())) ?></span></td>
</tr>
<?php do_action('mprm_checkout_table_subtotal_after'); ?>
<?php do_action('mprm_checkout_table_tax_before'); ?>
<?php if (mprm_use_taxes()) : ?>
<tr <?php if (!mprm_is_cart_taxed()) echo ' style="display:none;"'; ?>>
<td><span><?php _e('Tax', 'mp-restaurant-menu'); ?></span></td>
<td><span class="mprm_cart_tax_amount" data-tax="<?php echo mprm_get_cart_tax(); ?>"><?php echo mprm_currency_filter(mprm_format_amount(mprm_get_cart_tax())) ?></span></td>
</tr>
<?php endif; ?>
<?php do_action('mprm_checkout_table_tax_after'); ?>
<?php do_action('mprm_checkout_table_total_before'); ?>
<tr class="mprm-checkout-total">
<td>
<span><b><?php _e('Total', 'mp-restaurant-menu'); ?></b></span>
</td>
<td><span class="mprm_cart_amount"><b><?php echo mprm_currency_filter(mprm_format_amount(mprm_get_cart_total())) ?></b></span></td>
</tr>
<?php do_action('mprm_checkout_table_total_after'); ?>
</table>
<?php }
/**
* Send to success page
*
* #param string $query_string
*/
function mprm_send_to_success_page($query_string) {
return models\Checkout::get_instance()->send_to_success_page($query_string);
}
/**
* Send back to checkout
*
* #param $args
*/
function mprm_send_back_to_checkout($args) {
return models\Checkout::get_instance()->send_back_to_checkout($args);
}

PHP Codeigniter - How to set value to create database by option select

I try to make a shipping method using Rajaongkir API, but I need to add some data to my database that contain the shipping cost <?php echo $data['rajaongkir']['results'][$k]['costs'][$l]['cost'][0]['value'] ?>
but, it is always show an"0" in my database
this is my view
<?php echo form_open('orders/proceed'); ?>
<select class="form-control">
<option name="ongkir" selected="" disabled="">Pilih Layanan</option>
<?php
for ($l=0; $l < count($data['rajaongkir']['results'][$k]['costs']); $l++) {
?>
<?php echo "<option value='".$data['rajaongkir']['results'][$k]['costs'][$l]['cost'][0]['value']."'>" ?>
Layanan <?php echo $data['rajaongkir']['results'][$k]['costs'][$l]['service'] ?> |
<?php echo $data['rajaongkir']['results'][$k]['costs'][$l]['cost'][0]['etd'] ?> Hari | Rp.
<?php echo $data['rajaongkir']['results'][$k]['costs'][$l]['cost'][0]['value'] ?>
<?php "</option>"; ?>
<?php
}
?>
</select>
and this is the controller
$carts = $this->cart->contents();
$ongkir = $this->input->post('ongkir');
foreach ($carts as $item) {
$product = $this->Product->getProductByCode($item['id']);
$detail['code'] = $item['id'];
$detail['name'] = $item['name'];
$detail['price'] = $item['price'];
$detail['qty'] = $item['qty'];
$detail['discount_percent'] = $item['options']['discount_percent'];
$detail['net_price'] = $product['net_price'];
$detail['weight'] = $item['options']['weight'];
$detail['order_id'] = $orderId;
$detail['ongkir'] = $ongkir;
$this->Order_detail->create($detail);
}
this is the screenshot
I cannot add the shipping cost to my value.
How to resolve it and and those shipping cost value to my database?

pagination not working properly in searching

I have created some code of searching and pagination in it. If a user wants to search using some industry then data is shown against it. But whenever he presses any option in the pagination then it shows all the data related to every industry. How can i show pagination related to search result. And if user has selected any industry then it should be selected if user move to page 2 or page 3 or any.
Code of page
<?php
$pd=(isset($script['details']) ? $script['details']:array()) ;
//jobs search and normal page view
$qstr=Querystring();
$obj_pagination = new Pagination();
$obj_pagination->limit = (($FP_LIMIT!="") ? $FP_LIMIT:10);
$pagid=1;
if(isset($qstr["page"])) {
$pagid=$qstr["page"];
}
$obj_pagination->page = $pagid;
$srcqry="";
$industry=0;
if(isset($_POST['industry'])){
if(isset($_POST['industry'])){
if($_POST['industry']!="0") {
$srcqry=$srcqry." AND j.industry ='".(int)$_POST['industry']."'";
$industry=(int)$_POST['industry'];
}
}
$nrs = $obj->query("SELECT * FROM jobs j WHERE j.status='1' ".$srcqry);
} else {
$nrs = $obj->query("SELECT * FROM jobs j WHERE j.status='1' ".$srcqry);
}
$tot_rec= count($nrs->rows);
$obj_pagination->total = $tot_rec;
$obj_pagination->url = "jobs?page={page}";
$num_pages = ceil($obj_pagination->total / $obj_pagination->limit);
$start = ($obj_pagination->page-1)*$obj_pagination->limit;
$end = $obj_pagination->limit;
$limit = "LIMIT $start,$end";
$check;
?>
<h3>Search</h3>
<form name="srchjobs" action="/jobs" method="post">
<div class="col1">
<select class="dropdown1 spacer_side" name="industry">
<option value="0">All Industries</option>
<?php $indrow=getAllIndustries();
if(count($indrow) > 0) {
foreach($indrow as $tr) {?>
<option value="<?php echo $tr['id']; ?>" <?php if($tr['id']==$industry) { ?> selected="selected"<?php } ?>><?php echo $tr['name']; ?></option>
<?php }
$check=$tr['name'];
}
?>
</select>
</div>
<button type='submit'>Search</button>
</form>
<?php
$indrow=getAllIndustries();
$sql="SELECT j.*,u.url FROM jobs as j, aliases as u WHERE j.status='1' ".$srcqry."
AND (j.id=u.slog_id AND u.slog='jobs') ORDER BY j.id DESC ".$limit;
$jresults = $obj->query($sql);
if($jresults->rows) {
foreach($jresults->rows as $j){
$empd=array();
$empd=getIndinfo($j['industry']);
$emplogo="/uploads/no-image.png";
?>
<img src="<?php echo $emplogo;?>" alt="dell1" height="66" width="66" />
<h5><?php echo $j['title']; ?></h5>
APPLY
<?php } ?>
<ul class="pagination">
<?php
$obj_pagination->text_next = ">";
$obj_pagination->text_prev = "<";
$obj_pagination->text_first = "«";
$obj_pagination->text_last = "»";
$obj_pagination->text = "Showing {start} to {end} of {total}";
echo $obj_pagination->render();
?></ul>
<?php } else { ?>
<span class="latest_job">No jobs Found</span>
<?php } ?>
querystring function code
function Querystring(){
$_G=array();
$REQUEST_URI = $_SERVER['REQUEST_URI'];
$REQUEST_URI_ARR = explode("?",$REQUEST_URI);
$REQUEST_URI_ARR_All = explode("&",$REQUEST_URI_ARR[1]);
foreach($REQUEST_URI_ARR_All as $key=>$val){
$gs = explode("=",$val);
if(isset($gs[0]) && isset($gs[1]))
$_G[$gs[0]] = urldecode($gs[1]);
}
return $_G;
}
Output your results in a table and just use a jquery enabled table class. That way pagination will be automatic. Ensure you have a tag as well.

Sum from specific element of a multiple array

I am trying to get a sum in my variable session basket (it is a multiple array), I want to sum the elements that represent the cost of product.
Page where I add elements into the basket
<?php
session_start();
include('link.php');
if(!isset($_SESSION['basket'])){
$_SESSION['basket']=[];
}
// $id_permit=$_POST['id_fp'];
// $chosen_date=$_POST['date_permit'];
$_SESSION['little_basket']= array (
'itemIdPermit'=> "'".$_POST["id_fp"]."'",
'itemChosenDate'=> "'".$_POST["date_permit"]."'",
'itemFinalPrice'=> "'".$_POST["final_price"]."'",
'itemFinalPriceNotDiscount'=>"'".$_POST["price_not_discount"]."'"
);
// $_SESSION['basket'][] = $_SESSION['little_basket'];
array_push($_SESSION['basket'], $_SESSION['little_basket']);
print_r($_SESSION['basket']);
// $_SESSION['basket']=[];
// echo $_SESSION['basket'];
// header('location:index.php?area=fishing_permit');
?>
Page where I show the contents of the basket
<?php
// session_start();
// include('link.php');
// include('add_basket.php');
// $ids = '';
/* foreach($_SESSION['basket'] as $id){
$ids = $ids . $id . ",";
};*/
$lista_de_items_no_carrinho = $_SESSION['basket'];
?>
<ul id="list_product">
<?php
$basketLength=count($lista_de_items_no_carrinho);
for($i=0;$i< $basketLength;$i++){
$sqlListChosenPermits = "select * from type_of_permits
where id_type_of_permit =".$lista_de_items_no_carrinho[$i]['itemIdPermit'];
$outcomeListChosenPermits = $link->query($sqlListChosenPermits);
$line = mysqli_fetch_assoc($outcomeListChosenPermits);
?>
<li>
<div class="box_info"><h4><?php echo utf8_encode($line['type']); ?></h4><br>Validity: <?php echo utf8_encode($line['validity']);?> - Price: €<?php echo utf8_encode($line['price_eur']); ?> </div>
<div class="map">Map info</div>
<div class="rules">Rules</div>
<div class="permit_facsimile">Permit facsimile</div>
<div class="chosen_date"><?php echo trim ($lista_de_items_no_carrinho[$i]['itemChosenDate'], "'") ;?></div>
<div class="price">Euro <?php
$var= trim ($lista_de_items_no_carrinho[$i]['itemFinalPrice'],"'");
if (!empty($var) ){
echo ($var);
} else {
echo trim($lista_de_items_no_carrinho[$i]['itemFinalPriceNotDiscount'],"'");
} ?></div>
<div class="add_basket"><i class="fa fa-shopping-cart"></i>Remove from the Basket</div>
<input type="hidden" name="last_price" id="last_price" value="<?php
if (!empty($lista_de_items_no_carrinho[$i]['itemFinalPrice'])) {
echo trim ($lista_de_items_no_carrinho[$i]['itemFinalPrice'],"'");
} else {
echo trim($lista_de_items_no_carrinho[$i]['itemFinalPriceNotDiscount'],"'");
} ?>">
</li>
<?php
}
?>
</ul>
<p>Total price:<span class="total_price"><?php
$lista_de_items_no_carrinho = $_SESSION['basket'];
$listaPrice=$lista_de_items_no_carrinho[$i]['itemFinalPriceNotDiscount'];
$basketLength=count($listaPrice);
$sum=0;
for($k= 0; $k < $basketLength; $k++){
$sum += ($basketLength[$k]);
echo ($sum);
}?>
</span></p>
<div class="buying"><button type="submit"><i class="fa fa-shopping-cart"></i>Conclude the purchase</button></div>
</div>
I would like to sum the numerics value of 'ItemFinalPrice' and 'ItemFinalPriceNotDiscount'.
Thanks in advance.
Fanjo
You should take a look at array_column and array_sum:
$finalPriceSum = array_sum(array_column($basket, 'itemFinalPrice'));
$finalPriceNoDiscountSum = array_sum(array_column($basket, 'itemFinalPriceNotDiscount'));

Customizing the cart of "OpenCart"

I am trying to make a few changes to the actual cart in OpenCart.
As for now when adding a product to the cart information will be shown the following way:
Product1 Amount:
Size: XL 2
And a new row is created if the same product, but with a different size, is added to the cart.
My wish is to get the following design of the cart
Product1 S M L XL
1 2
So, that each product will only create one, single line and then show the correct amount under the corresponding size categories.
This is my code so far:
<?php
$saveName = array();
$sizesArray = array("S","M","L","XL");
$x = 0;
if($products || $vouchers) {
foreach ($products as $product){
foreach ($product['option'] as $option) {
$option['value'];
}
$saveName[$x] = $product['name'].'|'.$option['value'].'|'.$product['quantity'];
$seperateValues = explode("|",$saveName[$x]);
?>
<tr>
<td class="image"><?php if ($product['thumb']) { ?>
<img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" title="<?php echo $product['name']; ?>" />
<?php } ?></td>
<td class="name"><?php echo $product['name']; ?></td>
<?php
foreach($sizesArray as $sizes){
if($sizes == $seperateValues[1]){
echo '<td>'.$seperateValues[0].' '.$seperateValues[2].'</td>';
}
else{
echo '<td>0</td>';
}
}
?>
<td class="total"><?php echo $product['total']; ?></td>
<td class="remove">Remove</td>
</tr>
<?php
$x++;
}
?>
The code formats the amount under the right category, but I can't get it to produce only one row for each product. Any help is much appriciated.
Thanks!
Modifying the template is not going to do the job, because Opencart internally stores products in array in a format product_id:serialized_array_of_options, so products with the same id but different option combinations are stored separately. You can see that in /system/library/cart.php, in the getProducts method:
foreach ($this->session->data['cart'] as $key => $quantity) {
$product = explode(':', $key);
$product_id = $product[0];
$stock = true;
// Options
if (isset($product[1])) {
$options = unserialize(base64_decode($product[1]));
} else {
$options = array();
}
So to achieve, what you want, you will have make changes to the way Cart class stores products in the session.

Categories