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.
Related
I am unable to display data from a table from a MYSQL database using PHP OOP. However, I'm not sure if I'm unable to display the data because I'm not actually fetching the data in the first place.
I've tried fetching and displaying the data using only a PHP array and no HTML in my method and I figured this wouldn't be working because I wasn't using HTML list tags to format the data from the database. I've considered using a HTML table but I have seen displays from databases using lists work a few times before and I want to know why this doesn't work how it should.
I've tested for MYSQL connection and it does exist.
* M_PRODUCTS.PHP *
<?php
class Products
{
private $Conn;
private $db_table = "toys";
function __construct() {
// here we're making sure that the connection from $conn in "init.php" is transferred
// into our own private $conn property for usage in this object
global $Conn;
$this->Conn = $Conn;
}
// fetches and displays all products from db
public function fetch_all_products($id = NULL)
{
if ($id == NULL)
{
$data = [];
if ($result = $this->Conn->query("SELECT * FROM " . $this->db_table . " ORDER BY name"))
{
if ($result->num_rows > 0)
{
while ($row = $result->fetch_array())
{
$data = array(
"id" => $row["product_id"],
"name" => $row["name"],
"price" => $row["price"],
"image" => $row["image"]
);
}
return $data;
}
else
{
return "<h1>Oops... Something went wrong!</h1>";
}
}
}
}
}
* INDEX.PHP *
<?php include("init.php");
include("models/m_products.php");
?>
<body>
<div id="whitespace">
<?php
$products = fetch_all_products();
?>
<?php foreach($products as $row) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->price; ?></td>
<td><img src="<?php echo $row->image_path(); ?>" alt="<?php echo $row->name; ?>" width="100" /></td>
</tr>
<?php } ?>
</div>
</body>
I expect the images of my products to be displaying in my index.html file. However, nothing appears.
I also get this error message in the JavaScript console: Failed to load resource: the server responded with a status of 404 (Not Found). This would explain a lot but as I said I tested my database connection and it works. I'm not sure how or why this message is coming from the JavaScript console if I'm not using JavaScript. I thought it would be worth mentioning anyway.
According to your code the function returns some data, but the thing is that you have not printed it, so instead of return in the function you can use echo or just put
echo $Products->fetch_all_products();
Strange code, I would do something like that.
1. First the function find all()
2. index.php echo - output
<?php
$products = find_all_products();
?>
<?php foreach($products as $row) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->price; ?></td>
<td><img src="<?php echo $row->image_path(); ?>" width="200" /></td>
</tr>
<?php } ?>
There are 2 major issues I can see in your code
1) you need to update your comparison operator from
if ($id = NULL)
to
if ($id == NULL)
2) you need to update your code in index.php from
<body>
<div id="whitespace">
<h1><?php echo shop_name ?></h1>
<?php
echo $Products->fetch_all_products();
?>
</div>
</body>
to
<body>
<div id="whitespace">
<?php
$products = find_all_products();
?>
<?php foreach($products as $row) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->price; ?></td>
<td><img src="<?php echo $row->image_path(); ?>" alt="<?php echo $row->name; ?>" width="100" /></td>
</tr>
<?php } ?>
</div>
</body>
I hope it will sort out your issue
Correct your comparison operator it should be.
if ($id == NULL)
instead of
if ($id = NULL)
in your function fetch_all_products().
correct the code in index.php to print an array use print_r() or loop through the result array.
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
This is all about updating the information on category of an item.
I have the textbox containing the current category of an item and selectbox where the list of available categories can be selected ( take note that this is connected to the database and that the list of categories in the database can be updated).
Example 1:
Item A
Textbox: Cleaning Category
Selectbox:
-Food Category
-Technology Category
Example 2:
Item B
Textbox: Food Category
Selectbox:
-Cleaning Category
-Technology Category
As you can notice whenever the item belongs to that category (example: cleaning category) the choices for categories will not include what is selected (in that case, cleaning category will not be included as it is the selected category.)
Right now my code is this
<?php
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
$image_path = $row['image_path'];
$category = $row['category'];
$paypal_code = $row['paypal_code'];
?>
Product Name: <?php echo $name ?>"><br><br>
Category: <?php echo $category ?>"><br><br>
<select name="product_category[]">
<?php
$result = mysqli_query($db_link, "SELECT * FROM product_category");
while ($row = mysqli_fetch_array($result)) {
$categories = $row['category'];
?>
<option value="<?php echo $categories; ?>">
<?php echo $categories; ?>
</option><?php
}
?>
</select><br><br><?php
}
mysqli_close($db_link);
But this code doesnt remove the value of category per item in the choicest of categories. Please help, thanks!
Base on the suggestion of #Vld, he said what I need is PHP not jQuery. This is the solution code for my problem.
<div id="links">
<?php
$img_start = 1;
$img_end = 19;
while ($img_start <= $img_end){ ?>
<a href="<?php echo base_url(); ?>images/events_images/2013_Halloween/halloween (<?php echo $img_start?>).JPG">
<img src="<?php echo base_url(); ?>images/events_images/2013_Halloween/halloween (<?php echo $img_start?>).JPG" width="50" height="50">
</a>
<?php
$img_start +=1;
}
?>
</div>
I need to show a single attribute out of many attributes on product page. For example, I have author, publisher and format attributes for a product; and want to show only author in product page. Following code is showing all attributes in tabular format:
<?php foreach ($attribute_groups as $attribute_group) { ?>
<tbody>
<?php foreach ($attribute_group['attribute'] as $attribute) { ?>
<div>
<?php echo "<strong>",$attribute['name'], ":\n</strong>"; ?>
<?php echo $attribute['text']; ?>
</div>
<?php } ?>
A Better way to show single attribute for a product is to go to your product edit panel and remove all the attributes except the one you want to show
If you still want to do it in code which is not recommended then you can use and if your desired attribute
<?php foreach ($attribute_group['attribute'] as $attribute) {
if($attribute['name']=="Clockspeed") {
/* your attribute name for e.g i have used Clockspeed */
?>
<tr>
<td><?php echo $attribute['name']; ?></td>
<td><?php echo $attribute['text']; ?></td>
</tr>
<?php } } ?>
I have following code construction. It gives out an image of the product, which was even added to the shopping card. Images appear in the adding order: the image on top is the image, which was added last.
How can i change this code so, that i get showing only one image, from recently added product (but not all images from all added products)? I guess, i need change something in foreach ($items as $item) so, that the image is showing not for each $item, but only for one $item, but don't know how to modify this code exactly...
Thanks for your help and advises!
Here is the code:
<table>
<?php $i=0; $k=0; $subtotal = 0;?>
<?php foreach ($items as $item) : ?>
<?php
$link = K2StoreItem::getK2Link($item->product_id);
$link = JRoute::_($link);
$image_path = K2StoreItem::getK2Image($item->product_id, $this->params);
?>
<tr class="row<?php echo $k; ?>">
<?php if($this->params->get('show_thumb_cart')) : ?>
<td class="warkorb2">
<?php if(!empty($image_path)) : ?>
<img src="<?php echo $image_path; ?>" class="itemImg<?php echo $this->params->get('cartimage_size','small') ?>" />
<?php endif;?>
</td>
<?php endif; ?>
</tr>
<?php ++$i; $k = (1 - $k); ?>
<?php endforeach; ?>
<table>
You can use a Query with a limit or do something like this
<?php $lastItem = end($items) ?> <!-- return the last item in the $items array -->
<?php
$link = K2StoreItem::getK2Link($lastItem->product_id);
$link = JRoute::_($link);
$image_path = K2StoreItem::getK2Image($lastItem->product_id, $this->params);
?>
in the query that you did not post you can do a LIMIT 0,1 and also add in a SORT BY .. be it id, time etc..