get id of simple product of any configurable product on cart.phtml magento - magento-1.7

I am working with configurable product, I want simple product id of configurable product on cart.phtml, I am using this code
<?php foreach($this->getItems() as $_item): ?>
$_product = $_item->getProduct();
echo $_product->getId();
<?php endforeach ?>
but it always gives main product id, but I need its simple product id , any one help please.

I found solution
<?php foreach($this->getItems() as $_item): ?>
$_COnfigproduct = $_item->getProduct();
$simpleProduct=Mage::getModel('catalog/product')->loadByAttribute('sku',$_item->getSku());
echo $simpleProduct->getId();
<?php endforeach ?>
it works for me.

you can get simple product id associated to configurable product on cart page..
by below code
<?php $_item = $block->getItem();
$product = $_item->getProduct();
if($product->getTypeId() == 'configurable') {
echo $_item->getOptionByCode('simple_product')->getProduct()->getId();
} ?>

Related

Save data into 2 tables cakephp 3

I am very new to Cakephp 3. I try to do saveAll function in Cakephp 3, but unsuccessful.
I have 2 tables : Merchants and Users
Relationship between this 2 tables as below:
Users hasMany Merchants,
Merchants belongsTo Users
I want the create a merchant's registration form that will insert the data to both tables.
Can someone help me how to insert data into 2 different tables from 1 form using Cakephp 3?
My code as below:
<?= $this->Form->create($merchant); ?>
<fieldset>
<legend><?= __('Add Merchant') ?></legend>
<?php
echo $this->Form->input('User.id');
echo $this->Form->input('User.username');
echo $this->Form->input('User.password');
echo $this->Form->input('merchant_type_id', ['options' => $merchantTypes]);
echo $this->Form->input('name');
echo $this->Form->input('identity_number');
echo $this->Form->input('phone_number');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
Controller:
public function registration()
{
$merchant = $this->Merchants->newEntity();
if ($this->request->is('post')) {
$merchant = $this->Merchants->patchEntity($merchant, $this->request->data, [
'associated' => ['Users']
]);
if ($this->Merchants->saveAll($merchant)) {
$this->Flash->success('The merchant has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The merchant could not be saved. Please, try again.');
}
}
$users = $this->Merchants->Users->find('list', ['limit' => 200]);
$merchantTypes = $this->Merchants->MerchantTypes->find('list', ['limit' => 200]);
$this->set(compact('merchant', 'users', 'merchantTypes'));
}
Your helps is very appreciated. Thanks.
I think your problem is that your fields aren't named by lower-underscored version of the association name.
Instead of put:
echo $this->Form->input('User.username')
Which would result:
<input type="text" name="User[username]" >
You should try to put something as:
echo $this->Form->input('user.username');
Which would result:
<input type="text" name="user[username]" >
I hope it would be useful :)
PS: Sorry if there's any grammar mistakes but English is not my native language ;)
Please follow the documentation for saving data:
http://book.cakephp.org/3.0/en/orm/saving-data.html
If you are coming from 2.x, read the ORM migration guide, it should answer most of your questions:
http://book.cakephp.org/3.0/en/appendices/orm-migration.html

cakephp multiple forms with same action

I've got on my page several News, to every News we can add comment via form.
So actually I've got 3 News on my index.ctp, and under every News is a Form to comment this particular News. Problem is, when i add comment, data is taken from the last Form on the page.
I don;t really know how to diverse them.
i've red multirecord forms and Multiple Forms per page ( last one is connected to different actions), and i don't figure it out how to manage it.
Second problem is, i can't send $id variable through the form to controller ( $id has true value, i displayed it on index.ctp just to see )
This is my Form
<?php $id = $info['Info']['id']; echo $this->Form->create('Com', array('action'=>'add',$id)); ?>
<?php echo $this->Form->input(__('Com.mail',true),array('class'=>'form-control','field'=>'mail')); ?>
<?php echo $this->Form->input(__('Com.body',true),array('class'=>'form-control')); ?>
<?php echo $this->Form->submit(__('Dodaj komentarz',true),array('class'=>'btn btn-info')); ?>
<?php $this->Form->end(); ?>
and there is my controller ComsController.php
class ComsController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Session');
public function index()
{
$this->set('com', $this->Com->find('all'));
}
public function add($idd = NULL)
{
if($this->request->is('post'))
{
$this->Com->create();
$this->request->data['Com']['ip'] = $this->request->clientIp();
$this->request->data['Com']['info_id'] = $idd;
if($this->Com->save($this->request->data))
{
$this->Session->setFlash(__('Comment added with success',true),array('class'=>'alert alert-info'));
return $this->redirect(array('controller'=>'Infos','action'=>'index'));
}
$this->Session->setFlash(__('Unable to addd comment',true),array('class'=>'alert alert-info'));
return false;
}
return true;
}
}
you are not closing your forms
<?php echo $this->Form->end(); ?>
instead of
<?php $this->Form->end(); ?>
for the id problem you should write
echo $this->Form->create(
'Com',
array('action'=>'add/'.$id
)
);
or
echo $this->Form->create(
'Com',
array(
'url' => array('action'=>'add', $id)
)
);

Why is CakePHP 2.3.0 adding a '1' to my Form Post Values?

I'm using cakephp 2.3.0. I searched in the manual for quite awhile, but I haven't found the answer. Also, I've searched the Internet, but still haven't found what I'm looking for. SO, I'm posting my question here. Note, I'm fairly new to cakephp.
Scenario:
I have a simple form with two fields: activity and zip code.
I'm using POST on the form.
When I type in some value in those fields and submit, I echo those 'post' values/parameters and display in the browser screen. What I typed in, I can see on the screen, but the number '1' is added to the end of what I typed in the form.
Here is an example. I type in these values in the form, 'walk' and '44555'. Then I click 'Submit'. The post goes to my controller's action, which then calls my view. My view is displayed on the browser screen and I echo out those 'post' values. The results on screen are 'walk1' and '445551'.
Example #2: If I follow the steps above and don't enter any values in my form (I'll add error checking later), what I see on the browser screen is '1' and '1'.
I am unable to figure out why I am getting the value of '1' added to my form's POST values?
I'll be glad to include any other additional php code to this posting, if requested by someone trying to help.
Here is my FORM code (from my view)...I know there are DIV helpers, but I'll get to that later:
echo $this->Form->create(null, array('url' => array('controller'=>'activities', 'action'=>'results'))); ?>
<div class="box1" style="position:relative; top:10px; left:10px; float: left;">
Search here.... <br>
<hr>
<?php echo $this->Form->input('activityName', array('size'=>'30',
'label'=>'Activity Name:', 'value'=>'i.e. walking, etc.'));?>
<br>
<?php echo $this->Form->input('zip', array('size'=>'7', 'label'=>'Postal Code:')); ?>
<br>
</div>
<div class="box1" align="right">
<?php echo $this->Form->end('Go Search');?>
</div>
Here is my controller code:
<?php
class ActivitiesController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
//other code....
}
public function results() {
$this->layout = 'second';
$name = $this->request->data['Activity']['activityName'];
$pCode = $this->request->data['Activity']['zip'];
$this->set('theName', $name);
$this->set('theZip', $pCode);
$this->set('results', $this->Activity->
find('all', array('conditions' => array('name' => $name, 'postal_code' => $pCode))));
$this->set('title_for_layout', 'Results');
$this->render();
}
}
?>
My final view code. I left off some of the code...just showing the part that matters:
<div style="position:relative; top:10px; left:5px; ">
<?php echo print_r($theName); ?>
<br>
<?php echo print_r($theZip); ?>
Thanks
The 1 comes from printing the return value of print_r() which is true (i.e. 1).
In other words: you shouldn't do echo print_r(), just do print_r(). The function handles the printing by itself, you don't have to print the results manually.
(Also, print_r() is almost never the best choice to print out values except when debugging and even then CakePHP's debug() is much more suitable.)

Form validation problem with CodeIgniter

I have this view:
<?php echo form_open(); ?>
<?php echo form_input('username', '', ''); ?>
<?php echo form_submit('submit', 'Submit'); ?>
<?php echo form_close(); ?>
<?php echo validation_errors(); ?>
and this controller method:
function test() {
$username = $this->input->post('username');
$this->form_validation->set_rules($username, 'Username', 'required|min_length[4]');
if ($this->form_validation->run() !== FALSE) {
echo 'Tada!';
}
$this->load->view('test');
}
but when I leave the username field blank, nothing happens. However, if I type in something in it will tell me the field is required. I've downloaded and given CI a fresh install almost ten times now, trying to load with and without different helpers etc. It's becoming really frustrating, please help.
Try using this:
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]');
The problem lies in the first parameter of set_rules, which is the field name.
The $username you're passing is basically setting the field name to validate as whatever the user puts in the input field. If you were to type 'username' into the input box, you'd see that your form validates.
Change the line
$this->form_validation->set_rules($username, 'Username', 'required|min_length[4]');
to
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]');
Maybe is the problem of this line
<?php echo form_open(); ?>
If you leave it blank it basically send back to the controller itself and calling the construct and index function only. In this case your function dealing with form processing is "test()"
try this
<?php echo form_open('yourControllerName/test'); ?> //test is the function dealing with
if it is not working try on this
<?php echo form_open('test'); ?>

symfony 1.2 forms renderRow error message problem

Im trying to render a row field in a template with some extra styles, like this:
<?php echo $form['email']->renderRow(array('class' => 'text')) ?>
<?php echo $form['email']->renderError() ?>
The problem occurs when my form doesnt validate on this field... then it displays the error message 2 times!, i.e the renderRow renders one errorMsg string, and the renderError does it again... How can i stop renderRow from displaying the error message?
If I just do this, then it works:
<?php echo $form['email'] ?>
But in that case I cant style the field as I want....
thanks!
I am pretty sure this is also valid for 1.2. Instead of using renderRow, use something like this:
<?php echo $form['FormElementName']->renderLabel() ?> //display form element label
<?php echo $form['FormElementName']->renderError() ?> //display form element error (if exist)
<?php echo $form['FormElementName']->render(array('class' => 'text')); ?> //display form element
renderRow does them all at once.
EDIT From comments (Flask) - added ->render(array('class' => 'text'));