Opencart2 restrict user group to change stock in edit product page - opencart2.x

Is it possible to disallow access to quantity fields in opencart 2 admin product edit (general quantity and option values quantity)?
I want only the admin to be able to change the quantities.
Can i use something like if user_group_id!=1 (not admin) echo "readonly" to the input field of quantity?
I've tried for test to add a readonly, without if , to the input field in admin/view/template/catalog/product_form.tpl but nothing changed to the code as seen in view source.
Which file can i edit ?
Thank you,

Try following code. I think this will help you
admin/controller/catelog/product.php
Add following code before : if (!isset($this->request->get['product_id'])) {
// load user group model
$this->load->model('user/user');
$user_details = $this->model_user_user->getUser($this->session->data['user_id']);
$data['user_group_id'] = $user_details['user_group_id'];
admin/view/template/catalog/product_form.tpl
replace quantity html to following code
<div class="form-group">
<label class="col-sm-2 control-label" for="input-quantity"><?php echo $entry_quantity; ?></label>
<div class="col-sm-10">
<?php if(isset($user_group_id) && $user_group_id == 1) { ?>
<input type="text" name="quantity" value="<?php echo $quantity; ?>" placeholder="<?php echo $entry_quantity; ?>" id="input-quantity" class="form-control" />
<?php } else {
echo $quantity;
} ?>
</div>
</div>
Note : This is based on admin user group.

Related

How to add a new image field in opencart

Hello guys I want to add a new image field in opencart2 on return product page
How can I add image field in opencart
I have done as far :
view : return_form.tpl
<div class="form-group">
<label class="col-sm-2 control-label" for="input-comment1"><?php echo $entry_image; ?></label>
<div class="col-sm-10">
<input type="file" name="error_img">
</div>
</div>
In controller return.php in function add
if (isset($this->request->post['error_img'])) {
$data['error_img'] = $this->request->post['error_img'];
} else {
$data['error_img'] = false;
}
but when I am trying to print the value of $this->request->post['error_img'] it is returning blank
what I am missing
:)
you need to use $this->request->files['error_img'] instead of $this->request->post['error_img']
in Opencart you can say
$_POST == $this->request->post;
$_GET == $this->request->get;
$_FILES == $this->request->files;
and many more. Please add enctype="multipart/form-data" to your form too.

should I use different nonces for multiple forms on same page?

I am developing a plugin, where I am using bootstrap nav tabs function to display different forms on selecting menu . I want to use nonce function . Should I give seperate nonce function for every form or same nonce for all the forms as they are on the same page. Pls guide.?
Each form should have its own nonce, even those forms are on the same page.
If the form is present in admin pages then we should use check_admin_referer function to verify the nonce.
example:
<html>
<div>
<form id="form1"method="post" action="">
<input type="text" name="something" value=""/>
<?php wp_nonce_field('some_form','some_form_nonce');?>
<input type ="hidden" name="form_name" value ="1"/>
<input type="submit" name="submit"/>
</div>
<div>
<form id="form2"method="post" action="">
<input type="text" name="someother" value=""/>
<?php wp_nonce_field('some_other','some_other_nonce');?>
<input type ="hidden" name="form_name" value ="2"/>
<input type="submit" name="submit"/>
</div>
</html>
after that on processing page you should add the code like this
<?php
if(!isset($_POST['form_name'])){
if($_POST['form_name']==1){
if ( ! empty( $_POST ) && check_admin_referer( 'some_form', 'some_form_nonce' ) ) {
// process form1 data
}else if($_POST['form_name']=2){
if ( ! empty( $_POST ) && check_admin_referer( 'some_other', 'some_other_nonce' ) ) {
// process form2 data
}
}
?>
This code I tried in my app and is working as I expected...

Change default country in OpenCart

I'm using latest version of OpenCart 2.0.2.0 whenever I go to register page site/index.php?route=account/register it shows United states as default. I know how to change this default country to another default country. Here what I need is, if a user go to register page it will show please select instead of any default country.
It is not option available. but, You can do with modified following code.
Find code: catalog\controller\account\register.php
if (isset($this->request->post['country_id'])) {
$data['country_id'] = $this->request->post['country_id'];
} elseif (isset($this->session->data['shipping_address']['country_id'])) {
$data['country_id'] = $this->session->data['shipping_address']['country_id'];
} else {
$data['country_id'] = $this->config->get('config_country_id');
}
Remove following code from above code.
else {
$data['country_id'] = $this->config->get('config_country_id');
}
I'm presently working with custom built OpenCart theme, whenever I visit the register page, by default US is showing. If I want to change the default country (US) to another I need to change it via backend. But I would like to display --Please Select -- option on register page country section. Is there any way to change any default country to please select option?
Goto System -> Settings
You can find a list.
click the edit button
then click on the 'Local' Tab
Change your default country from here.
Another option how to do this is going to the template file, and modifying the loop there to not select the current country.
The area where you have:
<select name="country_id" id="input-country" class="form-control">
<option value="">
<?php echo $text_select; ?>
</option>
<?php foreach ($countries as $country) { ?>
<?php if ($country['country_id'] == $country_id) { ?>
<option value="<?php echo $country['country_id']; ?>" selected="selected">
<?php echo $country['name']; ?>
</option>
<?php } else { ?>
<option value="<?php echo $country['country_id']; ?>">
<?php echo $country['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
Just remove the part where the script is going to make a select element with "selected", like this, this should be the end code:
<select name="country_id" id="input-country" class="form-control">
<option value="">
<?php echo $text_select; ?>
</option>
<?php foreach ($countries as $country) { ?>
<option value="<?php echo $country['country_id']; ?>">
<?php echo $country['name']; ?>
</option>
<?php } ?>
</select>

Magento product view page quantity box always adds 1 to the cart issue

I'm developing a magento store and in the product view page/details page i have added a jquery quantity box.It works fine.But when click on the add to cart button always add "1" to the cart although i changed the quantity amount in the quantity box.I searched this issue in every place possible sites.But still no luck.And i have placed the input quantity box at the correct place.That means inside the form.I can't figure it out the issue.Hope a help.And my Magento version is 1.8.
This works with form tag... you can edit your default/theme/template/catalog/product/view/addtocart.phtml like this.
<form>
<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>
<?php if($_product->isSaleable()): ?>
<div class="add-to-cart">
<?php if(!$_product->isGrouped()): ?>
<label class="qty_label" for="qty"><?php echo $this->__('Quantity') ?>:</label>
<div class="qty_pan">
<input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
</div>
<?php endif; ?>
<button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
<?php echo $this->getChildHtml('', true, true) ?>
</div>
<?php endif; ?>
</form>
From Magento version 1.8 onwards, you need to add formkey value inside your form.
If formkey is not set in the form, then always the default qty 1 will be added to cart.
Also, if you try to update the qty from cart page, it will not get updated and will always be set to 1.
To solve this issue, add the following code inside the <form > tag:
<?php echo $this->getBlockHtml('formkey'); ?>
This code is similar to:
<input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>" />
Porduct View Page
app/design/frontend/YOUR_PACKAGE/YOUR_THEME/catalog/product/view.phtml
<form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form">
<?php echo $this->getBlockHtml('formkey'); ?>
Shopping Cart Page
app/design/frontend/YOUR_PACKAGE/YOUR_THEME/checkout/cart.phtml
<form action="<?php echo $this->getUrl('checkout/cart/updatePost') ?>" method="post">
<?php echo $this->getBlockHtml('formkey'); ?>

How do I send parameters to advanced search in Magento?

I have the following search form in Magento:
<form action="catalogsearch/advanced/result/" method="get" id="form-validate">
<input name="name" type="text" id="textfield" value="" />
<select name="platform" id="select">
<option value='6'>XBox</option>
<option value='5'>XBox 360</option>
</select>
<input type="submit" name="button" id="button" value="Search" />
</form>
Please note the “platform” field is a product attribute. While "name" field is product name.
No matter what I search, the results page always say “No items were found using the following search criteria”
I can see that two parameters are being passed:
result/?name=Logitech&platform;=5
Even if I change this to following, it doesn’t work:
result/?name=Logitech&platform;=XBox
Changing this to doesn’t work either:
result/?name=Logitech
The product is there in database I have checked. What am I doing wrong?
Thanks
This is quick a cut/paste from a code I used for a client whose attribute was a dropdown-type attribute.
Your attribute code must be "platform" for this to work.
<?
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', 'platform');
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
$_platforms = $attribute->getSource()->getAllOptions(false);
?>
<select onchange="location.href='<?php echo $this->getUrl('catalogsearch/advanced/result') . '?platform[]='; ?>'+this.value;">
<option selected><?php echo $this->__('Search by platform') ?></option>
<?php foreach($_platforms as $_platform) : ?>
<option value="<?php echo $_platform['value']; ?>"><?php echo $_platform['label']; ?></option>
<?php endforeach; ?>
</select>

Resources