Good day. Please help me to display units of measurement for goods. OcStore 2.3 engine.
By default, units of measurement are not displayed next to the product, they helped me to display them on the product page in this way:
In
catalog/model/catalog/product.php
Find line:
public function getProduct($product_id) {
Before the line mentioned above add the following code:
public function getProductWeightWithUnit($product_id) {
$product_info = $this->getProduct($product_id);
$query = $this->db->query("SELECT unit FROM `" . DB_PREFIX . "weight_class_description` WHERE
weight_class_id='".$product_info['weight_class_id']."'");
if ($query->num_rows) {
return number_format($product_info['weight'],2) . " " . $query->row['unit'];
} else {
return false;
}
}
Save changes and close the file.
Now, open file
catalog/controller/product/product.php
Find line:
if ($product_info['minimum']) {
Before the line mentioned above add the following code:
if ($product_info['weight']) {
$data['weight'] = $this->model_catalog_product->getProductWeightWithUnit($this->request->get['product_id']);
} else {
$data['weight'] = false;
}
Now, the backend code is ready. Based on the theme that you use, you need to edit the correct product.tpl file from your theme. For example if you use the default theme, then the file to edit is the following:
catalog/view/theme/default/template/product/product.tpl
Find the line:
<li><?php echo $text_stock; ?> <?php echo $stock; ?></li>
and add the following code after:
<li><?php echo $weight; ?></li>
Fine! Everything works on the product page. But you need it to work the same in modules (for example Featured Products).
I do all the same steps for the module
Backend:
catalog/controller/extension/module/featured.php
and front end:
catalog/view/theme/default/template/extension/module/featured.tpl
But I get the error:
Notice: Undefined index: product_id in C:\OSPanel\domains\mywebsite.com\catalog\controller\extension\module\featured.php on line 43
That's what it says:
if ($product_info['weight']) {
LINE 43 $data['weight'] = $this->model_catalog_product->getProductWeightWithUnit($this->request->get['product_id']);
} else {
$data['weight'] = false;
}
Why not seen product_id in ??
Open catalog/controller/extension/module/featured.php.
Find
if ($this->config->get('config_tax')) {
Add before
if ($product_info['weight']) {
$weight = $this->model_catalog_product->getProductWeightWithUnit($product_info['product_id']);
} else {
$weight = false;
}
Basically, here I've replaced $this->request->get['product_id'] with $product_info['product_id'], like #K.B. said in his answer, but made more accurate example.
Then, in the same file find
'tax' => $tax,
Add after
'weight' => $weight,
Now go to catalog/view/theme/default/template/extension/module/featured.tpl
Find
<?php if ($product['tax']) { ?>
<span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
<?php } ?>
Add after
<?php if ($product['weight']) { ?>
<span><?php echo $product['weight']; ?></span>
<?php } ?>
You must use $product_info['product_id'] or just $product_id instead $this->request->get['product_id'] Because you can't get product_id's for several products using this expression, which are in this loop foreach ($products as $product_id) {
Related
In my Magento store I have added downloadable products and if user want to download this product user need to subscribe our newsletter. for that I have added newsletter subscriber block in view.phtml file.code is below.
<?php $download = Mage::registry('current_product')->getTypeId();?>
<?php
if($download == 'downloadable')
{ ?>
<div class="pop-upss">
<input type="button" onclick="showMrnlePopup();" value="To Download This Product You Need To Subscribe Our Newsletter" name="submit"/>
</div>
<?php } ?>
so I have set onclick function and when user will click on this button newsletter pop-up will open.
in newsletter PHTML file I get current product's id using below code
<input type="hidden" name="pro_id" value="<?php echo Mage::registry('current_product')->getId();?>">
ok now we go to SubscriberController.php file and it's newAction(). I get current product id in newAction() using below code.
$product_id = $this->getRequest()->getPost('pro_id');
Now what I want using this product Id is :
1). I want all data about current product. -> I got this is using below code :
if($product_id) {
$obj = Mage::getModel('catalog/product');
$_product = $obj->load($product_id);
echo '<pre>';
print_r($_product->getData());
die;
}
2). if this product is downloadable then I want it's sample data and it's link to download product but I can not get current product's download link.my code for get download link is below.
if($_product->hasSamples()) {
$_samples = $this->getSamples();
foreach ($_samples as $_sample){
echo $samplePath = $_sample->getBasePath();
echo $sampleFile = $_sample->getSampleFile();
}
} else {
echo 'not getting anything here...';
}
when I run above code it's go into else part and echo 'not getting anything here...'.
so please anyone who know that how can I get downloadable product link please help me.
Thanks.
Getting these links is a bit complicated. I prepared a little snippet how to get the sample files and the normal links.
public function indexAction()
{
$product_id = $this->getRequest()->getPost('pro_id');
if($product_id) {
/** #var Mage_Catalog_Model_Product $product */
$product = Mage::getModel('catalog/product')->load($product_id);
if($product->getTypeId() == 'downloadable') {
/** #var Mage_Downloadable_Model_Resource_Sample_Collection $samples */
$samples = Mage::getModel('downloadable/sample')->getCollection()->addProductToFilter($product_id);
if($samples->count() > 0) {
foreach($samples as $sample) {
/** #var Mage_Downloadable_Model_Sample $sample */
$url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'downloadable/files/samples' . $sample->getSampleFile();
echo 'Sample URL: ' . $url . "<br>";
}
}
/** #var Mage_Downloadable_Model_Resource_Link_Collection $links */
$links = Mage::getModel('downloadable/link')->getCollection()->addProductToFilter($product);
if($links->count() > 0) {
foreach($links as $link) {
/** #var Mage_Downloadable_Model_Link $link */
$url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'downloadable/files/links' . $link->getLinkFile();
echo 'Link URL: ' . $url;
}
}
}
}
}
You need to load collections of downloadable/sample and downloadable/link and pass a product model or a product id to it. That's the Magento way ;-)
I am attempting to create a shopping basket using PHP, I can add items, clear the whole basket however I am stuck when trying to remove just one item from the basket. I try to send a GET variable into the URL and then decrease the quantity of the specified item based on it's ID. Although at the moment it doesn't seem to work and if I click multiple times the URL gets huge and the GET variables keep getting added to the end rather than changing the whole URL.
Here is my PHP
<?php if(isset($_SESSION["cart"])) {
foreach ($_SESSION["cart"] as $id => $value) {
$ids .= $id . ',';
$count += $value['quantity'];
$totalPrice += $value['price'];
}
$query = $database->find_item_db($ids);
foreach ($query as $single_query) {
$id = $single_query['Sweet_ID'];
echo $single_query['Description']; ?> x <?php echo $_SESSION['cart'][$id]['quantity'] . '<a href=' . $_SERVER['REQUEST_URI'] .'&idToRemove=' . $id . '&action=remove> Remove </a>' . '</br>';
} ?>
<h3>Currently <?php echo $count; ?> Items in the basket</h3>
<h4> Total Price £<?php echo $totalPrice; ?> </h4>
<?php
} else {
echo "Your cart is empty";
}
?>
<?php
session_start();
if ($_GET['action'] == "add") {
$idNumber=intval($_GET['id']);
if (isset($_SESSION['cart'][$idNumber])) {
$_SESSION['cart'][$idNumber]['quantity']++;
} else {
$sql = $database->display_single($idNumber);
$second_id = $sql[0]['Sweet_ID'];
$price = $sql[0]['Price'];
$_SESSION['cart'][$second_id]=array(
"quantity" => 1,
"price" => $price
);
}
} else if ($GET['action'] == 'remove') {
$idNumber=intval($_GET['idToRemove']);
$_SESSION['cart'][$idNumber]['quantity']--;
} else if ($_GET['action'] == 'clear') {
unset($_SESSION['cart']);
}
?>
Change this:
'<a href=' . $_SERVER['REQUEST_URI'] .'&idToRemove=' . $id . '&action=remove> Remove </a>'
into this:
' Remove '
as $_SERVER['REQUEST_URI'] already contains all the parameters that are currently present in the URL.
This will create a blank URL (pointing to the current page) with only the idToRemove and action parameters.
(And add double quotes, to keep the URL nicely contained within the href attribute)
The problem comes from your HTML code.
I think you have something like this in your PHP :
<form action="<?php echo $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']?>"
And as you always (re)call the same HTML form, the previous posted GET vars a posted again and again ....
I've programmed a basket class based on session you can use it , any question ask me please
class basket {
function add($pid,$qty){
if(!isset($_SESSION['basket'])){
$_SESSION['basket']=array();
$_SESSION['basket'][0]['productid']=$pid;
$_SESSION['basket'][0]['qty']=$qty;
echo 'The product was successfully added';
}
else {
if($this->isexist($pid,$qty)){
echo 'The quantity was successfully updated';
}
else{
$m=$_SESSION['basket'];
$max=count($m);
$_SESSION['basket'][$max]['productid']=$pid;
$_SESSION['basket'][$max]['qty']=$qty;
echo 'The product was successfully added';
}
}
}
function isexist($pid,$qty) {
$m=$_SESSION['basket'];
$max=count($m);
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['basket'][$i]['productid']){
$_SESSION['basket'][$i]['qty']=$qty;
return true;break;}
}
return false;}
function delete($pid){
$m=$_SESSION['basket'];
$max=count($m);
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['basket'][$i]['productid']){
unset($_SESSION['basket'][$i]);
$_SESSION['basket']=array_values($_SESSION['basket']);
$_SESSION['basket'.'num']-=1;echo 'The product was successfully delete';
break;}
}
}
function modify($pid,$qty){
$m=$_SESSION['basket'];
$max=count($m);
if($qty>0){
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['basket'][$i]['productid']){
$_SESSION['basket'][$i]['qty']=$qty;break;}
}
}
else $this->delete($pid);
}
function show_basket() {
$max=count($_SESSION['basket']);
for($i=0;$i<$max;$i++){
echo 'id=>'.$_SESSION['basket'][$i]['productid'].'qty=>'.$_SESSION['basket'][$i]['qty'];
}
}
}
I am using opencart and successfully added minimum order price for all transactions.
This is the code I used:
<?php if ($this->cart->getSubtotal() >= 10) { ?>
<div id="payment"><?php echo $payment; ?></div>
<?php } else { ?>
<div class="warning">Minimum 10 Euro to checkout</div>
<?php } ?>
Now I want to exclude one category out of it so that $9 product from that category can be bought.
Update 1:
Thank you so much for the help shadyyx
I tried shadyyx method but I am getting this error:
unexpected T_BOOLEAN_OR in this line
<?php if ($this->cart->getSubtotal() >= 10 || $this->cart->productsAreInCategory(1)) { ?>
Update 2: I tried this but it gave a pop up saying just error and ok button
<?php if (($this->cart->getSubtotal() >= 10) || $this->cart->productsAreInCategory(1)) { ?>
I tried this
<?php if (($this->cart->getSubtotal() >= 10) || ($this->cart->productsAreInCategory(1))) { ?>
it did not give any error and does same work (min amount for all orders regardless of category id)
I would go this way:
Extend the system/library/cart.php and add a method:
public function productsAreInCategory($category_id) {
$product_ids = array();
foreach($this->getProducts() as $product) {
$product_ids[] = $product['product_id'];
}
$categories = $this->db->query('SELECT category_id FROM ' . DB_PREFIX . 'product_to_category WHERE product_id IN (' . implode(',', $product_ids) . ')')->rows;
$category_ids = array();
foreach($categories as $category) {
$category_ids[] = $category['category_id'];
}
if(in_array($category_id, $category_ids) {
return true;
}
return false;
}
This method should accept a $category_id parameter to test against and should load categories for all products in cart. After first match a true is returned, if no match, a false is returned. You can now use this method this way:
<?php if (($this->cart->getSubtotal() >= 10) || $this->cart->productsAreInCategory(1)) { ?>
<div id="payment"><?php echo $payment; ?></div>
<?php } else { ?>
<div class="warning">Minimum 10 Euro to checkout</div>
<?php } ?>
Just replace the category ID in $this->cart->productsAreInCategory(1) with the correct one.
I have a search field in the page. But if there is no matching results, it is throwing an error "Fatal error: Call to a member function show() on a non-object". I have attached the screenshot.
<?php
if ( isset($_REQUEST['usersearch']) && $_REQUEST['usersearch'] )
printf( '<span class="subtitle">' . __('Search results for “%s”') .
'</span>', esc_html( $_REQUEST['usersearch'] ) );
?>
But the error line is (228th line):-
<div class='tablenav-pages'>
<?php echo $p->show(); // Echo out the list of paging. ?>
</div>
I need to remove the error on search result. It should simply show "No items found".
function pager($items)
{
global $limit;
global $p;
global $searchTerm;
global $pageLimit;
if($items > 0) {
$p = new pagination;
$p->items($items);
$p->limit($pageLimit); // Limit entries per page
$p->target("admin.php?page=User Control&usersearch=".$_REQUEST['usersearch']."&page-limit=".$_REQUEST['page-limit']);
$p->currentPage($_GET[$p->paging]); // Gets and validates the current page
$p->calculate(); // Calculates what to show
$p->parameterName('paging');
$p->adjacents(1); //No. of page away from the current page
if(!isset($_GET['paging'])) {
$p->page = 1;
} else {
$p->page = $_GET['paging'];
}
//Query for limit paging
$limit = "LIMIT " . ($p->page - 1) * $p->limit . ", " . $p->limit;
} else {
echo "No Record Found";
}
}
Your getting the error because your attempting to show a object that is undefined...use a statement to check...this is the premise.
<div class='tablenav-pages'>
<? if(is_object($p)){echo $p->show();} ?>
</div>
<?php
if (isset($p) && is_object($p)){
$p->show();
}
?>
I have this site and I want to put some in header for SEO purposes. The H1 must contain manufacturer name (brand) and product code (model).
So the problem is that I'm not sure how to put this variables in controller's file so I could call them in template file next.
Any ideas? :)
The best way around this would be to edit the product controller
catalog/controller/product/product.php
and change the heading there. It's set with
$this->document->setTitle($product_info['name']);
so you just need to prepend/append the manufacturer name there, such as
$this->document->setTitle($product_info['name'] . ' ' . $product_info['manufacturer']);
Did it! Added this code to controller/common/header.php
if (isset($this->request->get['product_id'])) {
$product_id = $this->request->get['product_id'];
} else {
$product_id = 0;
}
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct($product_id);
$this->data['product_info'] = $product_info;
if ($product_info) {
$this->data['manufacturer'] = $product_info['manufacturer'];
$this->data['model'] = $product_info['model'];
}
And this to theme/default/template/common/header.tpl
<?php echo $product_info['manufacturer']; ?> <?php echo $product_info['model']; ?>