Magento adding a bundled product to cart with 'bundle_option'? - php

I can't seem to find anywhere documentation of the 'bundle_option' field in the product options array, when adding a bundled product to the cart programatically in Magento. So I can't be sure how to do this correctly.
But this is my attempt:
$json_obj = json_decode($json_string, true);
//define cart
$cart = Mage::getSingleton('checkout/cart');
$bundle = array();
$bundle_qty = array();
for ($i=0; $i<count($json_obj['basket']['products']); $i++) {
$product_id = int($json_obj['basket']['products'][$i]['id']);
#add individual products to cart
#$product = new Mage_Catalog_Model_Product();
#$product->load($product_id);
#$params = array('product'=>$product_id,'qty'=>1);
#if ($product->getName()) $cart->addProduct($product, $params);
#add products to bundle
$bundle[$i] = $product_id;
if (isset($bundle_qty[$product_id])) $bundle_qty[$product_id] += (int)1;
else $bundle_qty[$product_id] = (int)1;
}
#add to bundled product to cart
$product = new Mage_Catalog_Model_Product();
$product->load(833); #833 = test bundle
$cart->addProduct($product, array('product'=>833,
'qty'=>min(1,int($json_obj['basket']['quantity'])),
'bundle_option'=>$bundle,
'bundle_option_qty'=>$bundle_qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$message = $this->__('Notice: %s item(s) were successfully added to your shopping cart.', $i);
Mage::getSingleton('checkout/session')->addSuccess($message);
}
So the commented out code is adding products individually which works correctly. Now I'm trying to add the products to a 'Test Bundle' product instead.
What I'm now doing in the loop is compiling the arrays for 'bundle_option' and 'bundle_option_qty' fields. Once the loop has finished I'm adding the bundle product (ID:833) to the cart with the options array of the bundled items.
The result is that nothing is added to the cart. I've also played around with the code a bit to no success.
Can anyone see where I'm going wrong or if you could point me to a doc/tutorial of the product options parameter that details the bundle_option array (what the indexes are, and what the values are) that would also help?

I had to check the POST variables sent to the cart URL from the front-end to figure this one out.
These were the variables posted for one bundle:
bundle_option[1][] 17
bundle_option[1][] 19
bundle_option_qty[1][17] 1
bundle_option_qty[1][19] 1
product 833
qty 2
related_product
From that I figured out that bundle_option[1] referred to Option 1 in the bundle.
I also figured that the values of the indexes bundle_option[1][0]=17 and bundle_option[1][1]=19 - the 17 and 19 referred to selection_id.
Analysing the form on the front-end revealed my list of selection_id's. I figured that the selection ID's would change once the bundle was altered in Admin>Manage Products so I used a look-up to get the selection IDs rather than hard-coding them in.
The code I ended up with was this:
$json_string = isset($_POST["json"])? $_POST["json"] : null;
if (!is_null($json_string)) {
$json_obj = json_decode($json_string, true);
#define cart
$cart = Mage::getSingleton('checkout/cart');
#look-up bundle selection ids
$bundled_product = new Mage_Catalog_Model_Product();
$bundled_product->load(833); #833 = test bundle
$selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
$bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
);
$bundled_items = array();
foreach ($selectionCollection as $option) {
$bundled_items[$option->product_id] = $option->selection_id;
}
#get bundle items, quantities
$bundle = array();
$bundle_qty = array();
for ($i=0; $i<count($json_obj['basket']['products']); $i++) {
$product_id = (int)$json_obj['basket']['products'][$i]['id'];
$selection_id = $bundled_items[$product_id];
if(!in_array($selection_id,$bundle)) array_push($bundle,$selection_id);
if (isset($bundle_qty[$selection_id])) $bundle_qty[$selection_id] += (int)1;
else $bundle_qty[$selection_id] = (int)1;
}
#add to bundled product to cart
$options = array('product'=>833,
'related_product'=>null,
'bundle_option'=>array(1=>$bundle),
'bundle_option_qty'=>array(1=>$bundle_qty),
'qty'=>(int)$json_obj['basket']['quantity']
);
$cart->addProduct($bundled_product, $options);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$message = $this->__('Notice: %s item(s) were successfully added to your shopping cart.', $i);
Mage::getSingleton('checkout/session')->addSuccess($message);
}
I hope this saves somebody a lot of time!
Edit
Still trying to solve why bundle_option_qty is not setting the quantity (all items are qty:1 added to the bundle product)
Edit 2
It turned out the built-in front-end bundle add-to-cart feature couldn't add multiple quantities of items to the cart either! Looking into the issue I found that the bundle-quantity feature was an extension called Kabel BundlePlus, it probably wasn't installed correctly by the previous developers so I downloaded it again and reinstalled the plugin and now the bundle_option_qty is working in both the frontend and my plugin!

Related

How to add Custom option value on coupon rules Magneto 2

How to add Custom option value on coupon rules Magneto 2. I have a product which have some custom option like size, color etc. with different value, I want give discount on basic of size = 7'. I don't want use sku custom option.. Let me know how I fix it.
https://github.com/Turiknox/magento2-custom-total
Moulde I use for Discount but i have face a problem.
one Data I can't get cart information
$obj = $bootstrap->getObjectManager();
// Set the state (not sure if this is neccessary)
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
// Getting the object managers dependencies
$quote = $obj->get('Magento\Checkout\Model\Session')->getQuote();
$helper = $obj->get('\Magento\Checkout\Helper\Cart');
// Get quote and cart items collection
$quote = $helper->getQuote();
$quoteitems = $quote->getAllItems();
$coupon_code = 'Test23';
// Get cart contents
$cart= $helper->getCart();
foreach ($quoteitems as $item)
{
}
It goes to the infinity loop.
This one Run fast that's why here we can't load quote total, For Discount given you must set session core cookies which allow giving discount...

Magento 1.9.3 Won't save customer group prices

I was assigned a Magento 1.9.3 project that would take in products in JSON format, and save customer group prices. However, even though there are no errors, nothing gets saved. The group prices won't show in the admin panel nor in the catalogue as expected.
Why is this? I've looked up every single article or stackexchange cases concerning group prices, but none of these seem to solve my problem.
Currently using the catalog_block_product_list_collection event.
$group_prices = $theProduct->getData('group_price');
if (is_null($group_prices)) {
$attribute = $theProduct->getResource()->getAttribute('group_price');
if ($attribute) {
$attribute->getBackend()->afterLoad($theProduct);
$group_prices = $theProduct->getData('group_price');
}
}
if(!is_array($group_prices)){
$group_prices = array();
$theProduct->addData(array('group_price' => $group_prices));
$theProduct->save();
}
$new_price = array(array (
'website_id'=>Mage_Core_Model_App::ADMIN_STORE_ID,
'customer_group_id'=>$customerGroupId,
'price'=> $singleProduct->Price));
$group_prices = array_merge($group_prices, $new_price);
$currentStore = Mage::app()->getStore()->getId();
try{
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
// First Delete all the group prices of product
$theProduct->setData('group_price', array());
$theProduct->save();
// Again save the old prices and new prices
$theProduct->setData('group_price', $group_prices);
$theProduct->save();
} catch(Exception $e) {
echo $e->getMessage();
}
Mage::app()->getStore()->setId($currentStore);
And yet, no group prices are saved when checking in the admin panel. Any help is appreciated.
For customer group field use cust_group instead of customer_group_id.
$new_price = array(array (
'website_id'=>Mage_Core_Model_App::ADMIN_STORE_ID,
'cust_group'=>$customerGroupId,
'price'=> $singleProduct->Price));
For details see:
app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Groupprice/Abstract.php line 299
there is condition:
if ($hasEmptyData || !isset($data['cust_group']) || !empty($data['delete'])) {
continue;}

Magento - Get the associated product attributes of an item in the wishlist

In app/code/local/Mage/Catalog/Product/Type/Configurable/Price.php, I am trying to get the attribute values of an associated product within the wishlist. I've attempted several approaches but I can only seem to produce data for the parent product.
Latest attempt
$customer = Mage::getSingleton('customer/session')->getCustomer();
if($customer->getId()) {
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $wlitem) {
$wishitem = Mage::getModel('catalog/product')->setStoreId($wlitem->getStoreId())->load($wlitem->getProductId());
//echo $wishitem->getId() . '<br>';
if($product->getId() == $wishitem->getId()) { //if the current product id equals the wishlist product id
echo $wishitem->getSku()."</br>";
}
}
}
That only gets me the parent product's sku. What I ultimately want to get is the attribute value for 2 attributes that I added for configurable products (not super attributes) but it seems that $product in Price.php only has the parent product collection.
Other Attempts:
$item_s = Mage::getModel('wishlist/item')->loadWithOptions($product->getId(), 'simple_product')->getOptionsByCode();
$simple_product = $item_s['simple_product']->getData();
$simple_product_id = $simple_product['product_id'];
$sim_product = Mage::getModel('catalog/product')->load($simple_product_id);
print_r($sim_product);
This only resulted in an error on the page.
Also:
$_item = Mage::getModel('catalog/product')->load($product->getId());
//echo $_item->getData('ppuom');
//print_r($_item);
$simpleProduct = $_item->getOptionsByCode()['simple_product']->getItem()->getProduct();
print_r($simpleProduct);
Seems as if you were most of the way there. I've tested this on my Magento site and it worked for me. It's pretty simple actually, you just have to grab the right model for that collection. Also, it seems like you're changing the pricing?!?! Be careful that your wishlist items contain the necessary attributes used in your logic.
$_item = Mage::getModel('catalog/product')->load($product->getId());
$attribute1 = $_item->getData('attribute1_code'); //see admin for attribute code
$attribute2 = $_item->getData('attribute2_code'); //see admin for attribute code
OR
Make changes to your template's wishlist files rather than the pricing logic in the code folder. You'll have access to all the data you need and it won't interfere with the price.php file which is relied on heavily in the cart and other critical areas of the website. The price in the wishlist is recalculated when it's moved to the cart anyway.

Magento - How to convert simple products to grouped ones programmat​ically?

My client and I are trying to achieve something that we are maybe not supposed to do. We have a demanding set of non-standard products that include different types of product conditions, e.g. grade B ware etc. In addition product data is coming in in form of feeds from several suppliers. In order not to clutter up the product listing we would like to convert existing, manually edited simple products into grouped ones (or whatever is appropriate) with associated new simple products programmatically on the fly and vice versa whenever necessary, i.e. when there are at least two different sub products.
So far we managed to do this somehow, a test scenario produces the seemingly right product structure – correctly associated products are displayed in the backend, the quantities have been correct before. The problem occurs with the frontend check on 'isSaleable()' for both the grouped product and the associated products, it returns 'false' no matter what we do. When we enforce the display of the qty input boxes and add-to-cart button they will (naturally?) just product the error message 'Please specify the quantity of product(s)'. We tried to set 'is_salable' by different means throughout the code. What else is missing? Any little hint, any advice here would be appreciated!
The code is quite lengthy already, and probably there's already some redundancy in there because of our attempts to somehow fix this. There's a main script that fetches a product collection and compares each entry with the existing supplier feeds. The essential part then is the function call to convert the product:
if (sizeof($newProducts[$productsSku]) > 1 && $_product->getTypeId() == 'simple') {
$newProductQtys = createGroupedProduct($_product, $newProducts[$productsSku]);
}
There are 2 functions for the actual conversion process, one which creates duplicates of the master products (type = simple) and sets their values accordingly:
function createDuplicate($product, $values) {
global $suppliers, $conditions_arr;
$sku = $product->getSku();
$newSku = $sku.'-v'.$suppliers[$values['feed']]['id'];
$qty = $values['qty'];
$feed = $values['feed'];
$cost = $values['cost'];
$item_condition = $values['item_condition'];
$box_condition = $values['box_condition'];
$notes = stripslashes($values['notes']);
$newProduct = $product->duplicate();
$newProduct->setTypeId('simple');
$newProduct->setStoreId(0);
$newProduct->setWebsiteIds(array(1));
$newProduct->setVisibility(2);
$newProduct->setSku($newSku);
$newProduct->setData('media_gallery', array());
$newProduct->setStatus(Mage_Catalog_Model_Product_​Status::smileyfrustrated:TATUS_ENABLED);
$newProduct->setPrice((real)($cost + $cost*$suppliers[$feed]['price_increase']));
$newProduct->setTaxClassId(2);
$newProduct->getResource()->save($newProduct);
$productId = $newProduct->getId();
// Check if there is a stock item object
$stockItem = Mage::getModel('cataloginventory/stock_item')->loa​dByProduct($productId);
$stockItemData = $stockItem->getData();
if (empty($stockItemData)) {
// Create the initial stock item object
$stockItem->setData('inventory_manage_stock_defaul​t', 1);
$stockItem->setData('manage_stock',1);
$stockItem->setData('is_in_stock', $qty ? 1 : 0);
$stockItem->setData('is_salable', 1);
$stockItem->setData('use_config_manage_stock', 1);
$stockItem->setData('stock_id',1);
$stockItem->setData('product_id',$productId);
$stockItem->setData('qty',$qty);
$stockItem->setTypeId( $newProduct->getTypeId() );
$stockItem->save();
// Init the object again after it has been saved so we get the full object
$stockItem = Mage::getModel('cataloginventory/stock_item')->loa​dByProduct($productId);
}
// Set the quantity
$stockItem->setData('qty',$qty);
$stockItem->setData('is_salable', 1);
$stockItem->save();
// $newProduct->assignProduct($newProduct, 1, 1);
$newProduct->setIsSalable(1);
$newProduct->setStatus(Mage_Catalog_Model_Product_​Status::smileyfrustrated:TATUS_ENABLED);
$newProduct->save();
$event = Mage::getSingleton('index/indexer')->logEvent($new​Product,$newProduct->getResource()->getType(),Mage​_Index_Model_Event::TYPE_SAVE,false);
Mage::getSingleton('index/indexer')->getProcessByC​ode('cataloginventory_stock')->setMode(Mage_Index_​Model_Process::MODE_REAL_TIME)->processEvent($even​t);
return $productId;
}
and
function createGroupedProduct($product, $newProductsArr) {
global $suppliers;
$origProductId = $product->getId();
$sku = $product->getSku();
$newProductId = array();
$newProductQtys = array();
$qty = 0;
$price = 999999;
$product->setTypeId('grouped');
$product->save();
foreach($newProductsArr as $key => $values) {
$id = createDuplicate($product, $values);
$newProductId[] = $id;
$newProductQtys[$id] = $values['qty'];
// add up all stock quantities of all slave products for master
$qty += $values['qty'];
// determine lowest price among all slave products
$subItemPrice = (real)($values['cost'] + $values['cost']*$suppliers[$values['feed']]['price​_increase']);
if ($subItemPrice < $price) $price = $subItemPrice;
}
if (sizeof($newProductId) > 1) {
$products_links = Mage::getModel('catalog/product_link_api');
foreach($newProductId as $key => $id) {
$products_links->assign('grouped', $origProductId, $id);
}
}
// set new values for grouped product according to slave products values
$product->setPrice($price);
$product->getResource()->save($product);
// Check if there is a stock item object
$stockItem = Mage::getModel('cataloginventory/stock_item')->loa​dByProduct($origProductId);
$stockItemData = $stockItem->getData();
if (empty($stockItemData)) {
// Create the initial stock item object
$stockItem->setData('manage_stock',1);
$stockItem->setData('is_in_stock', $qty ? 1 : 0);
$stockItem->setData('is_salable', 1);
$stockItem->setData('use_config_manage_stock', 1);
$stockItem->setData('stock_id',1);
$stockItem->setData('product_id',$origProductId);
$stockItem->setData('qty',$qty);
// $stockItem->setTypeId( $product->getTypeId() );
$stockItem->setTypeId('grouped');
$stockItem->save();
// Init the object again after it has been saved so we get the full object
$stockItem = Mage::getModel('cataloginventory/stock_item')->loa​dByProduct($origProductId);
}
// Set the quantity
$stockItem->setData('qty',$qty);
$stockItem->setData('is_in_stock', $qty ? 1 : 0);
$stockItem->setData('is_salable', 1);
$stockItem->save();
$product->setStatus(Mage_Catalog_Model_Product_Sta​tus::smileyfrustrated:TATUS_ENABLED);
$product->save();
$event = Mage::getSingleton('index/indexer')->logEvent($pro​duct,$product->getResource()->getType(),Mage_Index​_Model_Event::TYPE_SAVE,false);
Mage::getSingleton('index/indexer')->getProcessByC​ode('cataloginventory_stock')->setMode(Mage_Index_​Model_Process::MODE_REAL_TIME)->processEvent($even​t);
$cache = Mage::getSingleton('core/cache');
$cache->flush();
return $newProductQtys;
}
We tried using the store ID '1' as well and had some success with the shopping cart when the front end used the same kind of mass-product-add-to-cart form that we integrated into the product listing. But the associated products then showed no product name and no image, and not in the admin at all.
If more code from the main script is necessary I will happily provide this.

Create a new Prestashop cart in custom php

In Prestashop, I've created a custom form where I show a list with all products and the user can fill the corresponding quantities. By submitting the form, I clear the cart and fill it with the new values and finally redirect to the checkout page.
Everything's working fine, but only when the cart already exists. In a case of an empty cart (cart_id==null), I cannot add the products. I tried with several ways to create a $cart but I didn't manage to do so.
I don't get any exceptions, the code is executed without errors, it's just that at the end, in the checkout page, the cart remains empty; I repeat, only when the cart was already empty. In the case of a cart with products in it, then the process is just perfect!
I would appreciate some help on this.
Here is my small controller that gets the products and quantities from the form and adds them in the cart:
include('../../config/config.inc.php');
include('../../header.php');
// Filling the array of products
$products = array();
foreach ($_POST as $key => $value)
if (substr($key, 0, 9) === 'quantity_')
$products[substr($key, 9)] = $value;
// First of all, we remove all products
$prods = $cart->getProducts();
foreach ($prods as $prod)
$cart->deleteProduct($prod['id_product']);
// Adding the new products
foreach ($products as $product_id => $quantity)
if ($quantity > 0)
$cart->updateQty($quantity, $product_id);
// Redirecting to the checkout page.
header("Location: " . $_POST['redirect']);
exit();`
Thank you in advance!
I had the same problem with Prestashop not correctly creating a new cart upon calling the CartCore in many different ways - neither context or direct calls did work out.
Found this gem from someone else over here:
// get cart id if exists
if ($this->context->cookie->id_cart)
{
$cart = new Cart($this->context->cookie->id_cart);
}
// create new cart if needed
if (!isset($cart) OR !$cart->id)
{
$cart = new Cart();
$cart->id_customer = (int)($this->context->cookie->id_customer);
$cart->id_address_delivery = (int) (Address::getFirstCustomerAddressId($cart->id_customer));
$cart->id_address_invoice = $cart->id_address_delivery;
$cart->id_lang = (int)($this->context->cookie->id_lang);
$cart->id_currency = (int)($this->context->cookie->id_currency);
$cart->id_carrier = 1;
$cart->recyclable = 0;
$cart->gift = 0;
$cart->add();
$this->context->cookie->id_cart = (int)($cart->id);
$cart->update();
}
This is working for me now. As you see it goes a little more in depth than just asking the context to retrieve or create a new cart. hth.
You probably need to include this line when you are outside of a class
$context = Context::getContext();
And then add a cart variable that defines cart attributes ( in ur case assigns a id )
$cart = $context->cart; // gets the cart id or creates a new one
Should work fine now
BR's ( dont forget to accept the answer if it helps you :) )
As a complement to previous posts.
If you set that configuration : Configuration::updateValue('PS_CART_FOLLOWING', 1) (display the last cart of the customer instead of creating a new one each time).
You will always get the cart id $this->context->cart->id at NULL when trying to create a new cart at the customer's first log in (after creating a new account) using the following code:
if (is_null($this->context->cart)) {
$this->context->cart =
new Cart($this->context->cookie->id_cart);
}
I manage to solve the problem by adding some code to after the invalid cart creation.
/**
* Manage the possible errors when trying to create
* a new cart for a customer.
*
* The new cart is saved in the current context ($this->context->cart).
*/
private function createCart()
{
if (is_null($this->context->cart)) {
$this->context->cart =
new Cart($this->context->cookie->id_cart);
}
if (is_null($this->context->cart->id_lang)) {
$this->context->cart->id_lang = $this->context->cookie->id_lang;
}
if (is_null($this->context->cart->id_currency)) {
$this->context->cart->id_currency = $this->context->cookie->id_currency;
}
if (is_null($this->context->cart->id_customer)) {
$this->context->cart->id_customer = $this->context->cookie->id_customer;
}
if (is_null($this->context->cart->id_guest)) {
if (empty($this->context->cookie->id_guest)){
$this->context->cookie->__set(
'id_guest',
Guest::getFromCustomer($this->context->cookie->id_customer)
);
}
$this->context->cart->id_guest = $this->context->cookie->id_guest;
}
if (is_null($this->context->cart->id)) {
$this->context->cart->add();
$this->context->cookie->__set('id_cart', $this->context->cart->id);
}
}
After initializing the cart that way, I'm now able to add products to the cart with no problem.
Good success everyone

Categories