Sum of an elemnt in option section of codeigniter cart - codeigniter

Hello All Knowledgeable persons in codeigniter,
I am using codeigniter cart system.
Here's is the structure of my codeigniter cart
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'options' => array(
'picture'=>$img,
'item_slug'=>$slug,
'item_color'=>$color,
'item_size'=>$size,
'unit_price'=>$price,
'order_type'=>$order_type,
'product_type'=>$pro_type,
'unit_discount' => 0.00,
'total_discount' => 0.00,
'discount_type' => '',
)
);
Now I want to get the total total_discount on the cart, which i can do by a code-snippet like this
$tot_discount = 0.00;
foreach($this->cart->contents() as $ci)
{
$tot_discount = floatval($tot_discount + $ci['options']['total_discount'];
}
Now my question is, is there any inbuilt cart function to get sum of any numeric field of the options section in codeigniter? say like $this->cart->total() gives the total price of the cart.
Or I need to use the above code snippet?
Thanks in advance

While working with cart library in codeigniter, there are some fix parameters for cart that you need to set and some are optional.
Fixed Parameters
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name
Optional Parameters
'options' => array();
Actually options variable holds additional product specific information as you have specified and they may change subject to your business requirement. Now we have a common function total() as mentioned below:
$this->cart->total()
If you'll read the method definition then you'll find that it utilizes 'price' parameter for getting the total cart amount. Now as you have declared the 'total_discount' inside 'options' array, so there is a custom business requirement. So you can utilize the "total()" method definition to create your own function like "totalDiscount" because it varies product to product.

Related

Add array to cookie codeigniter

I am using code igniter. I have to make cart feature and I have to save product ids to cookie but I am not able to add array to cookie. My controller code to add cookie is
public function add_to_cart(){
$product_id = $this->uri->segment(3);
$cookie= array(
'name' => 'cookie_product',
'value' => $product_id,
'expire' => '3600',
);
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('cookie_product',true));die;
}
$product_id has my product id for example 41 . need help !
You do not have to use cookies directly. Use cart library which will handle cookies for you.
https://www.codeigniter.com/userguide3/libraries/cart.html
$this->load->library('cart');
$data = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
);
$this->cart->insert($data);
And to view items
<?php print_r($this->cart->contents()); ?>
Cart sounds like a good solution.
The reason why your original code wasn't working is you were trying to pass an array instead of breaking it into individual parameters.
If you did want to fix it, your code should be:
$this->input->set_cookie($cookie['name'], $cookie['value'], $cookie['expire']);
Incidentally, you can store an array in a cookie by using serialize and unserialize to store the cookie as a string instead of an array:
$this->input->set_cookie('cookie_product', serialize($cookie));
var_dump(unserialize($this->input->cookie('cookie_product',true)));

ajax call in magento 2

I have created a custom module in Magento2 where currently I'm selecting a customer and entered multiple order numbers, it will saved into a custom table in the database.
Currently I have to entered order numbers in a textarea but now I want to display all orders after choosing the customer which are not in Complete status.
From this section admin choose orders by clicking on the right side checkbox and Saved it.
Can anyone help me to solve this problem?
Existing Module’s Layout
Want to modify the above layout to the following one: In this desired system when admin change the customer name order numbers associated with that customer will be listed.
New invoice
Code I have used to create the customer dropdown field:
$fieldset->addField('customer_id', 'select', array(
'label' => __('Customer'),
'name' => 'customer_id',
'title' => __('Customer'),
'values' => $this->getCustomerOptionArray()
));
protected function getCustomerOptionArray()
{
$options = array();
$options[] = array(
'value' => 0,
'label' => __('Select Customer'),
);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')->getCollection();
foreach ($customerObj as $customerObjdata ){
$options[] = array(
'value' => $customerObjdata->getId(),
'label' => $customerObjdata->getName(),
);
}
return $options;
}

magento show content according to condition in admin grid

I have developed custom admin module. I have used the usual methods _prepareCollection and _prepareColumns to show the data in Grid.
protected function _prepareCollection()
{
$collection = Mage::getModel("wallets/sellerrequest")->getCollection();
$collection->getSelect()
->join( array('ce1' => 'customer_entity_varchar'), 'ce1.entity_id=main_table.seller_id and ce1.attribute_id = "5"', array('seller_name' => 'value'));
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
protected function _prepareColumns()
{
$helper = Mage::helper('sellers');
$currency = (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
$this->addColumn('id', array(
'header' => $helper->__('Request No'),
'index' => 'id'
));
$this->addColumn('Requested Amount', array(
'header' => $helper->__('Requested Amount'),
'index' => 'request_amount'
));
$this->addColumn('Seller Name', array(
'header' => $helper->__('Seller Name'),
'index' => 'seller_name',
));
$this->addColumn('Status', array(
'header' => $helper->__('Status'),
'index' => 'status_flag'
));
All the data shows correctly according to the table values. But I want to show the Request Amount column values preceding with $ sign, e.g. $300 etc. Also, I want to show the status flag according to condition. Means if the status flag is 1 then I want to show the value as "Approved", if flag is 2 then "Pending" etc. How should I customize the collection data and show in grid according to my requirement? Help appreciated.
Thanks.
I have answered to a question similar to your requirement
How to properly add a shipping_description column in magento order grid?
Check my answer and try to compare with your problem. In this example there is the solution for our currency problem too.
So check this out.Hope it will be helpful.
Here you should implement Grid Renderer.
Here is complete tutorial for that : http://inchoo.net/magento/how-to-add-custom-renderer-for-a-custom-column-in-magento-grid/
You can customize the value of any colum

Magento Admin formfield multiselect selected

I´m currently developing a custom module for magento thats going to list employees.
I have figured out almost everything. The only thing I got left is how the selected values is going to be highlighted.
The problem I´m having is for the backend.
I got 2 tabs per employee, one for employee data and one tab for magento categories.
1 employee can have 1 or more categories.
The database table that the categories are stored in are a non-eav table.
So my question is
What in a multiselect determines which values are selected? As it is now, only one value is selected.
I think you can do this by simply passing in an array of the id's to be selected into the 'value' attribute of the field being added for the multiselect in the _prepareForm() method. Something like the following.
$fieldset->addField('category_id', 'multiselect', array(
'name' => 'categories[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('mymodule/mymodel')->getMymodelValuesForForm(),
'value' => array(1,7,10),
));
The id of the form element (e.g. category_id) must not be an attribute in your model, otherwise when the form values get set with $form->setValues() later on, the attribute value will be overwritten.
I normally store multiple selections as a text column separated by commas much like most magento modules handles stores which requires a slightly different approach as shown below.
In the form block for the tab with the multiselect, you firstly define the element to be displayed like so in the _prepareForm() method. You then get the values from the model and set put them into the form data.
protected function _prepareForm()
{
...
$fieldset->addField('store_id', 'multiselect', array(
'name' => 'stores[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
));
...
if ( Mage::getSingleton('adminhtml/session')->getMymodelData() )
{
$data = Mage::getSingleton('adminhtml/session')->getMymodelData();
} elseif ( Mage::registry('mymodel_data') ) {
$data = Mage::registry('mymodel_data')->getData();
}
$data['store_id'] = isset($data['stores']) ? explode(',', $data['stores']) : array();
$form->setValues($data);
}
I normally store the selected stores (categories as in your case) in the main model as a text column and comma separated values of ids, hence the explode.
In the controller for for the edit action, I put the model being edited into the mage registry so we can load it and it's values in the step above.
Mage::register('mymodel_data', $model);
Thanks for answering.
This is how my field looks like:
$fieldset->addField('npn_CatID', 'multiselect', array(
'label' => Mage::helper('employeelist')->__('Kategori'),
'class' => 'required-entry',
'required' => true,
'name' => 'npn_CatID',
'values' => $data,
'value' => array(3,5)
));
npn_CatID is the value in my db where the category id is saved.
I have tried to change the name and field ID but cant get it working.
When its the field id is like above ONE value is selected and its the last one inserted for the chosen employee
My data array looks likes
array(array('value' => '1', 'label' => 'USB'), array('value' => '2', 'label' => 'Memories'))

Custom Add to Cart With Products Custom Atribute In Magento

I have tried to add the products to cart using custom module. Below is the code i used
$product_id = $this->getRequest()->getParam('product');
$product = Mage::getModel('catalog/product')->load($product_id);
$param = array( 'product' => $product->getId(), 'qty' => 2,'options["'.$option_id.'"]' => $option_type_id );
$cart = Mage::getModel('checkout/cart')->init();
$cart->addProduct($product, new Varien_Object($param));
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$cart->save();
I can add the products name, quantity to cart using product id, But i cannot able to add the products custom options in cart.
Please give me a hand on this.
Thanks,
Prakash
You're so close! The main thing that you need to change is your $param, as it is not structured the way Magento would like. This should do the trick:
$param = array(
'product' => $product->getId(),
'qty' => 2,
'options' => array(
$option_id => $option_value,
$option_id2 => $option_value2,
),
);
Note that any required custom options on your product will need to have values to avoid a fatal error whilst adding to the cart. Also, no need to cast $param as a Varien_Object - Magento understands the array just fine.

Resources