Magento: frontend text field that updates the product price? - magento

I have no idea how to do this, this does need to be added to a working store with normal products, I am ok at making an new template to do this.

Create a custom magento module with a controller
Then create a save action in your SavepriceController.php
public function saveAction(){
$websites = Mage::app()->getWebsites();
$product_id = $this->getRequest()->getParam('product_id');
// need to validate price here
$price = $this->getRequest()->getParam('price');
$product = Mage::getModel('catalog/product')->load($product_id)
if($product->getId()){
$product->setPrice($price)->save();
}
}
Then add a text field to your frontend page
<form action='http://site.com/modulename/Saveprice/save' method='post'>
<input type='hidden' name='product_id' value='add product id here' />
<input type='text' name='price' />
<input type='submit' value='submit' />
</form>

Related

Laravel - Upload to Existing Model Record

Maybe it's because I'm tired, but I can't seem to get a simple upload working for one of my models.
On the show details page of my customers (who are NOT users) model, I have a simple form where a user can upload the logo of the customer.
The form:
<form enctype="multipart/form-data" action="/customers/i/{{$customer->url_string}}" method="POST">
<input type="file" name="logoUpload">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<input type="submit" class="pull-right btn btn-sm btn-primary" value="Upload">
</form>
The Controller:
public function logoUpload(Request $request){
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
$customer->car_logo = $path;
$customer->save();
return back();
}
}
I know the issue is that I haven't defined $customer in the controller since the file does actually store itself in the correct folder after I click submit, but it does not hit the database at all.
Update
The current customer details url:
http://localhost:8000/customers/i/dsdado9a98w78721
The web definition for the post route:
Route::post('/customers/i/{customer}', 'CustomerController#logoUpload');
You have to create a hidden field in your form that contains the customer id and then use it in your controller to update it with the new file path, here is an example:
View
<form enctype="multipart/form-data" action="/customers/i/{{$customer->url_string}}" method="POST">
<input type="file" name="logoUpload">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<!-- customer_id field -->
<input type="hidden" name="customer_id" value="{{$customer->id}}">
<input type="submit" class="pull-right btn btn-sm btn-primary" value="Upload">
</form>
Controller
public function logoUpload(Request $request){
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
// get customer
$customer = Customer::find($request->customer_id);
$customer->car_logo = $path;
$customer->save();
return back();
}
}
You have some options, here is a simple one, with only adjusting that controller method:
public function logoUpload(Request $request, $customer)
{
$customer = Customer::where('url_string', $customer)->firstOrFail();
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
$customer->car_logo = $path;
$customer->save();
return back();
}
...
}
We have added a parameter to the method signature to accept the route parameter. We then find the model via url_string matching that parameter.
You also could setup route model binding as well to do the resolving of that model based on the route parameter for you.

How can I upload a picture using Codeigniter

I want to know how I can upload an image and save its name in a database when using Codeigniter.
I am using a simple form tag for this, like:
<form action="./myaccount/index/" method="post" enctype="multipart/form-data">
<div>
<label for="pic">picture</label>
<input type="file" id="pic" name="pic" />
</div>
<input id="register-btn" name="register" type="submit" value="ok" class="input-text custom-btn">
</form>
Here's my controller:
public function index() {
$user = new User($_SESSION['user_id']);
if($this->input->post()){
$user->pic = $this->input->post('pic');
if($this->input->post('password') == $this->input->post('confirm')){
$user->password=md5(sha1(sha1($this->input->post('password'))));
$user->save();
$this->data['success'] = "done";
}else{
$this->errors[] = "error";
}
}
$this->data['user'] = $user;
$this->data['errors'] = $this->errors;
$this->template->set_layout('myaccount');
$this->template->build('profile',$this->data);
}
I checked the manual but I can't understand what they are doing.
Im' trying to make a controler function for this that will insert all the values in the database and also upload the image file.

Codeigniter Cart: How to add multiple items with ajax and jquery

I'm building a ajax based shopping cart with Codeigniter, and the add / remove functions work perfectly. I am now trying to add an option for adding multiple items, and can't get it to work.
Here's the markup I'm using. I'm not sure if it's the best design, but it's working with the non-ajax function, so I guess it should be fine.
<form action="cart/add_multiple" method="post" accept-charset="utf-8">
<input type="hidden" name="items[0][id]" value="3571310" />
<input type="hidden" name="items[0][qty]" value="1" />
<input type="hidden" name="items[0][price]" value="59.00" />
<input type="hidden" name="items[0][name]" value="London" />
<input type="hidden" name="items[0][heb_name]" value="לונדון" />
<input type="hidden" name="items[0][full_price]" value="59.00" />
<input type="hidden" name="items[0][discount_price]" value="59.00" />
<input type="hidden" name="items[1][id]" value="7397903" />
<input type="hidden" name="items[1][qty]" value="1" />
<input type="hidden" name="items[1][price]" value="29.00" />
<input type="hidden" name="items[1][name]" value="London Triple" />
<input type="hidden" name="items[1][heb_name]" value="לונדון טריפל" />
<input type="hidden" name="items[1][full_price]" value="29.00" />
<input type="hidden" name="items[1][discount_price]" value="29.00" />
<input type="submit" name="add_multi" value="add to cart" /></form>
The ajax script is as follows:
$(document).ready(function() {
$(document).on("submit", "div#winning_combo_small form", function () { //catches every click on the submit button of the "add to cart" form
var items = $(this).serialize();
alert(items);
$.post(base_url + "cart/add_multiple", {items: items, ajax: '1' },
function(data){
if (data =='true')
{ // Interact with returned data
$.get(base_url + "cart", function(cart){ // Get the contents of the url cart/show_cart
$("#cart_sidebar").html(cart);
})
$.get(base_url + "cart/count_items", function(items){
$("#cart_items").html(items);
})
}
});
return false;
})
});
But it's not working, because the add_multiple function receives the data as a string, not an array. Do I have to decode the data somehow to convert it to an array? Do the Hebrew characters get in the way and mess it all up?
I should say that when posting the form the regular way, without ajax, the items are added to the cart and all works well. So what is the difference between the regular post and the ajax post?
Well, I got it to work, though I'm not sure if it's the most elegant way.
Here's what I did:
In the ajax script, I changed var items = $(this).serialize(); to var items = $(this).serializeArray();. I now get an array instead of a string, but it's not the format I need to insert into the cart. So I looped over this array to create an array in the desired format, and used that new array to insert into the cart.
This is my add_multiple function under the cart controller:
function add_multiple()
{
$items = $this->input->post('items');
$ajax = $this->input->post('ajax');
// Check if user has javascript enabled
if($ajax != '1'){
$this->cart->insert($items); //if posted the regular non-ajax way, the fields will be in an array in the correct format
echo 'false';
redirect('cart'); // If javascript is not enabled, reload the page with new data
}else{
$i = 0;
foreach($items as $key=>$form_field)
{
$field_name = $form_field['name'];
$from_char = strrpos($field_name, '[') +1 ;
$length = strlen($field_name)-$from_char-1;
$field = substr($field_name,$from_char,$length);
$data[$i][$field] = $form_field['value'];
if ($field == "discount_price") $i+=1; // I know 'discount price' is always the last field
}
$this->cart->insert($data);
echo 'true'; // If javascript is enabled, return true, so the cart gets updated
}
}

How to pre populate my custom form in magento

I've created a custom form, similar to a customer registration form, in Magento.
i want to prepopulate this form when an error messages arises.
Does anyone know how I can prepopulate a custom form in Magento?
Without seeing you code it is hard to say which method will method will work, but one of these method should work.
Using JavaScript Validation - client side validate, only post if all require info is correct
<form name="my-form" id="my-form" method="post">
<label for="username">
< ?php echo $this->__("User name") ?> <span>*</span></label><br />
<input type="text" id="username" name="username" class="input-text required-entry"/>
.....
</form>
<script type=”text/javascript”>
//< ![CDATA[
var customForm = new VarienForm('my-form');
//]]>
</script>
See a list of all the validation class name
Repopulate field
To get a list of all the post variable information that the customer fill out
Mage::app()->getRequest()->getPost();
To get a form field (eg. <input type='text' name='firstname' ... />)
Mage::app()->getRequest()->getPost('firstname');
So in you .phtml try (this will not work if you doing a redirect on error)
<input type='text' name='firstname' value="<?php echo Mage::app()->getRequest()->getPost('firstname');?>" />
If the above doesnt work then you need the save the post info into a session.
In your controller if validation fail save the information to customer session
Mage::getSingleton( 'customer/session' )->setData( 'yourFormName', Mage::app()->getRequest()->getPost() );
In your phtml template, print the data if 'yourFormName' variable session exist.

How do i restrict the users of other domain apart from **abc.com**

I have to build a custom module in which i have added a custom fields on to the customer registration page a fields like alternate mail id,mobile number & etc which is successfully getting saved in my database & here i have to implement a one more feature that is
on registration page when user enters his email id that should check for the domain name.
Ex: my domain name is abc & the users mail address will be user#abc.com
& the users of "abc.com" should only has to get register into my online store
How do i restrict the users of other domain apart from abc.com
Please help me in doing this.....
You can use regular expression for this. Just use the below function:
function checkEmail($email) {
if(preg_match("/^([a-zA-Z0-9\._-])*#abc.com$/",$email)){
return true;
}
return false;
}
Code Edited
<script type="text/javascript">
function validateEmail(elementValue){
var emailPattern = /^[a-zA-Z0-9._-]+#abc.com$/;
return emailPattern.test(elementValue);
}
function checkForm(){
var emailId = document.getElementById('email_address').value;
if(! validateEmail(emailId)) {
alert("Email id is not valid");
}
}
</script>
<div class="input-box"style="width: 550px;">
<input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" />
<input type="submit" value="Click here" onclick="checkForm()" />(There will be be an submit button put the onclick event in it)
</div>

Resources