Opencart display attrbutes of related products - php

I need help with a loop in opencart.
I want to display 2 - 3 product next to each other other on the product page. similar to how products are displayed in on the compare page. I've achieved the product limit via code already and I've managed to display everything else, pictures, price etc. My issue is the attributes.
I know that i need to reach into this array
$this->data['products'][] = array(
......
'attribute' => $attribute_data,
I've also included this line of code
$attribute_groups = $this->model_catalog_product->getProductAttributes($product_id);
my loop is
<?php foreach ($attribute_group['attribute'] as $key => $attribute) { ?>
<tbody>
<tr>
<td><?php echo $attribute['name']; ?></td>
<td><?php echo $attribute['text']; ?></td>
<?php } ?>
<?php } ?>
This gets me all attributes of the current product but displays errors for any attributes not filled in if there are any related products.
Then I have no idea on how to reach into the array and display the related product attribute details.
please help

<?php if isset($attribute['name'];); {?>
<td><?php echo $attribute['name']; ?></td>
<?php } ?>
Try this; the error will not be shown.

Related

Display sum of item quantity in PDF export

I'm having issues displaying total quantity of all items at the bottom of my PDF export script I'm using with my Wordpress site.
I've been able to display total quantity of each item using
<td class="qty"><?php echo esc_html( $item->get_quantity() ) ?></td>
Then I want to calculate the sum of all item quantities and show that as a single figure at the bottom of the table
Would I use
echo sum_array ( $item->get_quantity() )
I hope you are showing each quantity via some loop, if yes then before loop define a variable like
$sum = 0;
Now change <td> code like this (inside loop):
<td class="qty">
<?php
$sum += (int)$item->get_quantity();
echo esc_html( $item->get_quantity() ) ?>
</td>
And now in the end use this $sum variable to show total quantity.
<?php echo $sum;?>

Nested php loop only runs once when connecting to sql

I'm trying to learn php and I have this exercise I'm working on. I have an SQL db with two tables: categories and items. I want to loop through categories and create an html table for each category. I can do that part fine. Then for every category, I want to loop through every item and check to see if it belongs to that category. If it does, then it gets passed into the table. However, for some reason the second loop only gets run for the first category.
I've tried using a foreach loop on the items but then nothing shows up.
<?php while($category = mysqli_fetch_assoc($category_result)) : ?>
<button class="collapsible"><?= $category[Name]; ?></button>
<div class="link-data">
<table class="link-table">
<?php while($item = mysqli_fetch_assoc($items_result)) : ?>
<?php if (item[category_id] = category[id] ) : ?>
<tr>
<td><?= $item[name]; ?></td>
<td><?= $item[price]; ?></td>
<td><?= $item[description]; ?></td>
</tr>
<?php endwhile; ?>
<?php endif; ?>
</table>
</div>
<?php endwhile; ?>
I then expect a result like this for example:
Phones :
- Samsung
Toys :
- Rubber ball
Books :
- Harry Potter
Instead I'm getting this:
Phones :
- Samsung
- Rubber ball
- Harry Potter
Toys :
Books :
for the inside loop after first time, item_result pointer goes to end and you have to reset it if you wanna loop again.
so before the inside while add this line
mysqli_data_seek($items_result, 0);
this line set the item_result pointer to first row

Show available stock quantity on product page

Firstly I would like to apologise if this question has been answered somewhere else but I am unable to find what I am looking for as I am new to PHP and assume I need this to solve my problem.
I have built a website and am using Mals-e shopping cart. I have everything up and running but I would like to show how many products are still in stock under the product description and if there are no items in stock. For example:
Available stock: 2
or
Sold Out
I have read that I need a text file with product name, price and quantity, a PHP file to read and rewrite the quantity available and out put the results on the product page and a mypage.php page but I really don't know where to start. I've spent days trying to sort this out.
I have Mysql database with some items in table called (items) with available quantity but don't know how to go about sorting it out. Any help would be most appreciated.
Thank you.
Without seeing the actual code you're using to display the product, it's hard to say buy all you should need is something like:
<?php
// get the product and stock level
if($product->numberInStock > 0) {
echo 'Available: ' . $product->numberInStock;
} else {
echo 'Out of stock';
}
If you're editing a phtml type template (HTML with embedded PHP), you might display it like:
<? if($product->numberInStock > 0): ?>
<p>Available: <?= $product->numberInStock; ?></p>
<? else ?>
<p>Out of stock</p>
<? endif; ?>
Had same issue, found out following.
Inside your catalog/product type template you can use this:
<?php
$_product = $this->getProduct();
$_qty = $_product->getStockItem()->getQty();
?>
<p>
<?php if($_qty > 0): ?>
Available: <?php echo $_qty; ?>
<?php else: ?>
Out of stock
<?php endif; ?>
</p>
session_start($_POST['quantity']);
if(isset($_POST['quantity']))
{
$postedquantity=$_POST['quantity'];
$productQuantity="20";
if($postedquantity<$productQuantity){
echo "In stock";
echo "<br/>";
}
$productQuantity=$productQuantity-$postedquantity;
echo $productQuantity."Remaining";
}
You can even do like this,storing quantity value in session and everytime it's posted it will check whether it is in stock or not and it will show how much quantity is remaining.

php removing item/variable from php array using button

Im looking for help with this. I've tried unset and other options and nothing seems to be working. I hate to bother people but ive searched google till the cows come home and im doing it wrong.
Here is my code:
<?php
$orderArray = $_SESSION['orderVal'];
foreach($orderArray as $orderVal)
{
$valInvent = getOrderInventories($orderVal['invId']);?>
<tr><td><?php echo $valInvent['itemnumber'];?></td>
<td><?php echo $valInvent['description'];?></td>
<td><?php echo $valInvent['cp'];?></td>
<td><?php echo $orderVal['price'];?></td>
<td><?php echo $orderVal['qty'];?></td>
<td></td>
</td>
I want to remove an item from this array using one of the variables.
This should work.
I am using
unset($valInvent['itemnumber']);

PHP Symfony Convert Propel Choice to String

I am using a propel form to allow a user to create an account, the form has two parts; the entering and then the preview.
On my preview page I declare the values of the form as usual and these are provided by the previous form
public function configure() {
//Preview page no fields are displayed anyway xD
$this->useFields(array('email', 'user_gender_id', 'search_gender_id', 'content', 'age', 'location'));
But instead of outputting the fields I am trying to render the values on the page:
<?php echo $form->renderHiddenFields(); ?>
<?php foreach($form as $field): ?>
<?php if(!$field->isHidden()): ?>
<tr>
<th><?php echo $field->renderLabel() ?></th>
<td><?php echo $field->getValue(); ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
<?php echo $form; ?>
<?php endif; ?>
Unfortunately for my sfWidgetFormPropelChoice / sfWidgetFormChoice fields it just outputs the chosen ID rather than a string representation of it.
Is there a proper way in Symfony to output the text representation of a widget's value? Or do I have to hack something together? (any ideas?)
Many thanks,
Pez,
Try:
$obj = $form->getObject();
to have the values of your object. Then:
echo $obj[$field->getName()];
this can work in most cases. But, if it won't, you'll have to write all the preview fields by hand and display them using the $obj above.
I got around this problem by doing the following:
<td><?php if(method_exists($field->getWidget(), "getChoices")) {
$choices = $field->getWidget()->getChoices();
echo $choices[$field->getValue()];
} else
echo $field->getValue();
?></td>
I hope this is of use to someone as it has been driving me crazy!

Categories