Opencart minimum order price exclude one category - php

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.

Related

Display units of measure for a product

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) {

Change the displayed number of products in Woocommerce with Select dropdown

Trying to add a drop-down on products listing to change the displayed number of products. I am working on the latest version of Wordpress (4.8.2) and Woocommerce (3.1.2).
This is my code on functions.php
//save and load the chosen option from session
function jc_get_products_per_page(){
global $woocommerce;
$default = 5;
$count = $default;
$options = jc_get_products_per_page_options();
// capture form data and store in session
if(isset($_POST['jc-woocommerce-products-per-page'])){
// set products per page from dropdown
$products_max = intval($_POST['jc-woocommerce-products-per-page']);
if($products_max != 0 && $products_max >= -1){
$woocommerce->session->jc_product_per_page = $products_max;
return $products_max;
}
}
// load product limit from session
if(isset($woocommerce->session->jc_product_per_page)){
// set products per page from woo session
$products_max = intval($woocommerce->session->jc_product_per_page);
if($products_max != 0 && $products_max >= -1){
return $products_max;
}
}
return $count;
}
add_filter('loop_shop_per_page','jc_get_products_per_page');
//set the options for the dropdown
function jc_get_products_per_page_options(){
$options = apply_filters( 'jc_products_per_page', array(
5 => __('5', 'woocommerce'),
10 => __('10', 'woocommerce'),
15 => __('15', 'woocommerce'),
20 => __('20', 'woocommerce')
));
return $options;
}
//display the dropdown on front-end
function jc_woocommerce_products_per_page(){
$options = jc_get_products_per_page_options();
$current_value = jc_get_products_per_page();
?>
<div class="products-per-page">
<span>View:</span>
<form action="" method="POST" class="woocommerce-products-per-page">
<select name="jc-woocommerce-products-per-page" onchange="this.form.submit()">
<?php foreach($options as $value => $name): ?>
<option value="<?php echo $value; ?>" <?php selected($value, $current_value); ?>><?php echo $name; ?></option>
<?php endforeach; ?>
</select>
</form>
</div>
<?php
}
add_action('woocommerce_after_shop_loop', 'jc_woocommerce_products_per_page', 1);
So the problem is that when I change the dropdown, the page reload but the number of displayed products stay the same. The default. Any idea what is going wrong?
Edit: I tried to add a return 2; on the first row of the function and still doesn't work. So, it seems the add_filter doesn't work
I grabbed your code as is from your initial post and dropped it into a theme I'm developing for WooCommerce and it worked like a charm.
Obviously, that's not your case. But this leads me to believe that something else is likely also hooking into the loop_shop_per_page filter after your code is being called.
Before refactoring all of your code try setting the priority on your add_filter to something high, like so:
add_filter('loop_shop_per_page','jc_get_products_per_page', 99);
That should help to determine if it is an issue with your filter being overwritten by the same filter being called in another plugin or elsewhere in your theme.
Okay I've changed my answer completely and tested the solution, so it should work, only thing is that the pagination was fully working, but it would be good if someone could improve on my answer.
Looking at woocommerce Docs however you should be filtering the $cols variable for it to work. Apologises I couldn't tell exactly what you where doing with your code above so I've rewritten the scripts from scratch - but I think if you just used the $cols then you'd of probably of got there yourself.
Here's the new code:
add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 );
add_action('woocommerce_after_shop_loop', 'new_loop_shop_per_page', 1);
add_action( 'woocommerce_product_query', 'mywoocommerce_products_per_page', 1, 50 );
function new_loop_shop_per_page( $cols ) {
session_start();
$default = "5";
$cols = $default;
$submittedValue = "";
// global $submittedvalue;
$value0 = "5";
$value1 = "10";
$value2 = "15";
$value3 = "20";
if (isset($_POST["ProductsPerPage"])) {
$_SESSION['ProductsPerPage'] = $_POST['ProductsPerPage'];
$submittedvalue = $_SESSION['ProductsPerPage'];
}
?>
<form action="" name="products" method="post">
<select project="ProductsPerPage" id="ProductsPerPage" name="ProductsPerPage">
<option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedvalue)?" SELECTED":""?>><?php echo $value0; ?></option>
<option value = "<?php echo $value1; ?>"<?php echo ($value0 == $submittedvalue)?" SELECTED":""?>><?php echo $value1; ?></option>
<option value = "<?php echo $value2; ?>"<?php echo ($value0 == $submittedvalue)?" SELECTED":""?>><?php echo $value2; ?></option>
<option value = "<?php echo $value3; ?>"<?php echo ($value0 == $submittedvalue)?" SELECTED":""?>><?php echo $value3; ?></option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
<?php
global $submittedvalue;
$cols = $submittedValue;
return $cols;
}
function mywoocommerce_products_per_page( $query ) {
if ( $query->is_main_query() ) {
session_start();
global $submittedvalue;
//$submittedvalue = '5';
$_SESSION['ProductsPerPage'] = $_POST['ProductsPerPage'];
$submittedvalue = $_SESSION['ProductsPerPage'];
$query->set( 'posts_per_page', $submittedvalue );
}
}
add_filter( 'redirect_canonical', 'custom_disable_redirect_canonical' );
function custom_disable_redirect_canonical( $redirect_url ) {
if ( is_paged() && is_singular() ) $redirect_url = false;
return $redirect_url;
}
If someone could help getting the pagination to work as well, it would be helpful.
Try change your jc_get_products_per_page() function with something like this.
function() jc_get_products_max($ppp=5) {
if(!empty($ppp)) {
$ppp = (int)$ppp;
if($ppp != 0 && $ppp >= -1){
return $ppp;
}
}
}
function jc_get_products_per_page(){
global $woocommerce;
$count = jc_get_products_max();
$options = jc_get_products_per_page_options();
if(isset($_POST['jc-woocommerce-products-per-page'])){
$count = jc_get_products_max($_POST['jc-woocommerce-products-per-page']);
} else {
if(isset($woocommerce->session->jc_product_per_page)){
$count = jc_get_products_max($woocommerce->session->jc_product_per_page);
}
}
$woocommerce->session->jc_product_per_page = $count;
return $count;
}

How to show all subcategories of a particular category in alphabetical order with Magento

I want to show all subcategories of a particular category in an alphabetical order just like I added in admin.
I am showing my all subcategories in this page app/design/frontend/default/mytheme/template/catalog/product/product-listing.phtml
The code is
<?php
$categories = Mage::getSingleton('catalog/layer')->getCurrentCategory()->getChildren();
$catArray = explode(',', $categories);
?>
<div class="categories_list">
<?php
foreach($catArray as $child){
$parentCategoryId = $child;
$categoriesgot = Mage::getModel('catalog/category')->load($parentCategoryId)->getChildren();
$catArraygot = explode(',', $categoriesgot);
$categoriesCount = count($catArraygot);
//echo $categoriesCount;
$_child = Mage::getModel( 'catalog/category' )->load($child );
$products_count = Mage::getModel('catalog/category')->load($child)->getProductCount();
//############################################################################################################
//$parentCategoryId = $child;
//$categoriesgot = Mage::getModel('catalog/category')->load($parentCategoryId)->getChildren();
$category_name = Mage::registry('current_category')->getName();
if($categoriesCount >= 1 AND $products_count < 1)
{
$value = $categoriesCount." Categories";
}
else if($categoriesCount == 1 AND $products_count > 0)
{
$value = $products_count." Products";
}
else if ($categoriesCount >= 1 AND $products_count > 1)
{
$value = $categoriesCount." Categories";
}if($categoriesCount == 1 AND $products_count == 0)
{
$value = $products_count." Products";
}
//echo $products_count;
?>
<div class="listing">
<a href="<?php echo $_child->getUrl() ?>">
<img src="<?php echo $_child->getImageUrl();?>" alt="<?php echo $_child->getName()?>" width="135" height="135"/>
<h3 class="product-name"><?php echo $_child->getName() ?></h3></a>
<?php if($category_name != "Brands"){?> <p><?php echo $value;?></p><?php }?>
</div>
<?php
}
?>
</div>
I don't know in which order subcategories are showing because sub-categories are all mixed up.
If anyone knows this,please help me out.
Thanks!
i think this will work for you
<?php
$cats =Mage::getModel('catalog/category')->getCategories(10);
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = $category->getUrl();
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php foreach($categories as $name => $url): ?>
<li>
<?php echo $name; ?>
</li>
<?php endforeach; ?>
</ul>
Here is how you can get the list of subcategories in an alphabetical order or in the order entered in the backend, provided you have the parent category.
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$children = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('parent_id', $category->getId()) //get only direct children of main category
->addAttributeToFilter('is_active', 1) //in case you want only the active categories
->addAttributeToSort('name') //sort by name
//->addAttributeToSort('position') //in case you want to sort by position
;
foreach ($children as $child) {
//do something with $child
}
This way you avoid calling load in a loop that can cause performance issues.

Search results error

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();
}
?>

display the number of items in the cart on virtuemart joomla 2.5?

I'm using the code below to display a number based on the number of items in the cart. If there's 1 item, then the number 1 is generated, 2 items and 2 is displayed etc etc.
the problem is that it displays the quantity for the product with an id of [1]. how can change this to make it work for all product ids?
<?php $array = unserialize($_SESSION['__vm']['vmcart']);
$amount = $array->products[1]->amount;
if ($amount != 0){ echo $amount; } else { echo 0; } ?>
the [1] is the product id. how do i change it to accept all product ids?
Are you wanting to loop through all products? Something like ...
<?php
$array = unserialize($_SESSION['__vm']['vmcart']);
foreach($array->products as $product){
$amount = $product->amount;
if ($amount != 0){ echo $amount; } else { echo 0; }
}
?>
Adding all products ...
<?php
$array = unserialize($_SESSION['__vm']['vmcart']);
$total = 0;
foreach($array->products as $product){
$total += $product->amount;
}
echo "Total Products: " . $total;
?>

Categories