I want to add products in cart with custom labels on cart page - magento

I have tried several methods to add products to cart programatically but i want to show the quote item labels as well one cart page. Can anyone help me to do this or any references?

You can use the below code in your observer.
$item = ( $item->getParentItem() ? $item->getParentItem() : $item);
$additionalOptions = array(array(
'code' => 'option_code',
'label' => 'Some_option_Label',
'value' => 'Option_Value'
));
$item->addOption(array(
'code' => 'additional_options',
'value' => serialize($additionalOptions),
));
// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);

here is the code you are looking for;)
<?php
$loadProductData = Mage::getModel('catalog/product')->load($productId);
$quote = Mage::getSingleton('checkout/session')->getQuote();
$OrderquoteItem = Mage::getModel('sales/quote_item');
$quoteItem = $OrderquoteItem->setProduct($loadProductData);
//custom options to show user on cart page
$a_options = array(
'options' => array(
'label' => 'OptionLabel :',
'value' => "OptionaValue",
));
//add above options array to this cart item which is going to get added on cart
$quoteItem->addOption(array(
'code' => 'additional_options',
'value' => serialize($a_options),
));
// set price and quantity
$quoteItem->setQuote($quote)
->setQty($productOptions['qty'])
->setOriginalCustomPrice($productOptions['price'])
->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

Related

Why does the second product dynamically added to the cart loose it's options in Magento2

I am dynamically adding products to the cart in Magento2 with some custom options. Every product has the same base product id with different options. Represent Product has been properly overridden so that all products added to the cart are separate. However with this code, the second product added will lose it's custom options:
$magento_product = $this->productRepository->get('simple-product-1');
$params = array(
'product' => $magento_product->getId(),
'qty' => intval(5),
'options' => array(
'cr_price' => 12.0,
'Product' => "Test P",
'cr_XML' => '<root></root>'
),
);
$this->cart->addProduct($magento_product, $params);
$params = array(
'product' => $magento_product->getId(),
'qty' => intval(10),
'options' => array(
'cr_price' => 14.0,
'Product' => "Test P2",
'cr_XML' => '<root></root>'
),
);
$this->cart->addProduct($magento_product, $params);
$this->cart->save();
Only the first product has an entry in the quote_item_option table.
Any thoughts on why or how to fix would be appreciated.
Force reloading the product between each add fixes this issue.
$this->productRepository->get('simple-product-1', false, null, true);
The last true parameter is forceReload.

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 - add to cart a product with advanced custom options setting option QTY

I am running Magento 1.9 with Advanced Custom Options by MageWorx. I am creating a module that needs to add a product with custom options to the cart. With the code below I can add the product to the cart with 1 option. Yet I can't figure out how to add a certain quantity for the custom option.
$product = $this->getProduct();
$product_id = $product->getId();
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
'product' => $product_id,
'qty' => 1,
'options' => array(
'1' => 1,
)
);
try {
$cart->addProduct($product, $params);
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$cart->save();
echo '<div class="shiptext">Your order has been add to your cart.</div><br clear="all">';
}
catch (Exception $ex) {
echo $ex->getMessage();
}
Maybe I can't increase the QTY this way as Magento core doesn't support QTY for custom options? Which case I am guessing I may need to use some class inside of the Advanced Custom Options module but I am not sure how I would do that. If anyone has experience with Advanced Custom Options, it would be greatly appreciated as to how one might do this.
Found the answer. You need to add the to the prams options_groupid_qty.
$params = array(
'product' => $product_id,
'qty' => 1,
'options' => array(
'1' => 1,
),
'options_1_qty' => 30
);

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.

magento: add attibute to product, but not show up when editing the product

As per bens comment and answer I updated my script, comments indicate changes
{Magento 1.4.0.1} currently i have an installer script:
$installer = $this;
$installer->startSetup();
//commented out to use factory method
//$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$setup = Mage::getResourceModel('catalog/setup','core_setup');
if(!$setup->getAttribute('catalog_product','attribute_code')){
$newFields = array(
'attribute_code' => array(
'type' => 'text',
'label' => 'Attribute Label',
//added visible option
'visible' => false,
),
);
$entities = array(
'catalog_product',
);
foreach($newFields as $attributeName => $attributeDefs) {
foreach ($entities as $entity) {
$setup->addAttribute($entity, $attributeName, array(
'type' => $attributeDefs['type'],
'label' => $attributeDefs['label'],
//added visible option
'visible' => $attributeDefs['visible'],
'class' => '',
'required' => false,
));
}
}
}
$installer->endSetup();
It works wonderfully! Except the attribute shows up in the General attribute group when editing the product and I don't want it to show up at all (its a secret ninja attribute) is there something I'm doing wrong? or perhaps something I should be doing to let Magento know not its not supposed to show up?
Using addAttribute() you can set the 'visibleindex tofalse. UsingupdateAttribute()` you should do the following:
$setup->updateAttribute('catalog_product','attr_code','is_visible',false);
Let me know if I'm wrong.

Resources