Opencart 3 Price Per Piece of Option Price - php

In Opencart 3.0.2.0 version, I am doing a calculation that divides the Option price by the Quantity.
First I take the option name as pcs and convert it to int. (Example Option Names: 25, 50, 75, 100)
Then I divide the value in the Product name by the total price written and assign it to a variable.
The first data is empty. How can I resolve this situation? I am sharing the screenshot for error;
https://www.hizliresim.com/dac9o14
My Codes:
$optionPerPrice = '';
if(isset($option['option_id']) && $option['option_id'] == 14) {
foreach($product_option_value_data as $optionPerPrice) {
$perQuantity = intval($optionPerPrice['name']);
$perPrice = floatval($optionPerPrice['price']);
$optionPerPrice = $perPrice / $perQuantity;
}
}
$product_option_value_data[] = array(
'product_option_value_id' => $option_value['product_option_value_id'],
'option_value_id' => $option_value['option_value_id'],
'name' => $option_value['name'],
'image' => $this->model_tool_image->resize($option_value['image'], 50, 50),
'price' => $price,
'price_prefix' => $option_value['price_prefix'],
'optionPerPrice' => $optionPerPrice,
);
}
}
$data['options'][] = array(
'product_option_id' => $option['product_option_id'],
'product_option_value' => $product_option_value_data,
'option_id' => $option['option_id'],
'name' => $option['name'],
'type' => $option['type'],
'value' => $option['value'],
'required' => $option['required'],
);
}`

Related

Auto fill value multidimensional array in php

Question might be difficult to answer but here it goes:-
See I have an market order book in array like this:-
$str = array(
array(
'amount' => 1.5,
'price' => 10,
'user' => 'test',
'filled' => 0,
'status' => 'active',
),
array(
'amount' => 2.5,
'price' => 10,
'user' => 'test1',
'filled' => 0,
'status' => 'active',
),
array(
'amount' => 5,
'price' =>10,
'user' => 'test2',
'filled' => 0,
'status' => 'active',
),
array(
'amount' => 10,
'price' => 10,
'user' => 'test3',
'filled' => 0,
'status' => 'active',
)
);
Here the above arrays shows the name of user selling the amount of a thing and the price per thing. Status is active as nothing is still sold to anyone(filled is 0). Let's say a user named test4 comes into the picture and wants to buy 6 things at price'10'. How do I make so that the first three orders get dynamically filled and also from 'active' status to 'done' or 'partially filled'.
Please help :)

Programatically add a new bundled item to an existing bundled product?

I have a bundled product that I would like to add a new bundled item into a specific selection, so I tried the following:
// Load existing bundled product
$productCheck = Mage::getModel('catalog/product')->load($bundled_product_id);
// Get option data
$options = $productCheck->getTypeInstance(true)->getOptionsCollection($productCheck);
// Set selection data
foreach ($options as $option) {
if ($option->getTitle() == $data['bundled_title']) {
$selection = new Mage_Bundle_Model_Selection();
$selection->addData(array(
'entity_id' => $productCheck->getId(),
'product_id' => $new_bundled_item_id,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => 0,
'is_default' => 1,
'selection_id' => '',
'selection_price_type' => 0,
'selection_price_value' => 0.0,
'option_id' => $option->getId(),
'delete' => ''
));
$option->setStoreId($data['store_id']);
$option->addSelection($selection);
$option->save();
$selection->save();
}
}
$productCheck->save();
This does not apear to be throwing an error, at the same time, nothing seems to be changing with the bundled product. The new item is not getting added.
Any ideas? I have already checked; $new_bundled_item_id is correct and the option title does match.
Edit
I tried to solve it like this: http://pastebin.com/hQbxQHak - when the code runs, it appears to be creating a new empty option but my 2nd selection does appear to be added. it looks like this:
This is not quite right, but if I re-ran the script again, this is what happens:
I am not sure what's happening, can anyone see anything wrong with my code? Or am I doing this completely wrong?
I have solved it, like this:
public function syncBundledProduct($data)
{
// Check if bundled product already exists
$bundled_product_id = Mage::getModel("catalog/product")->getIdBySku($data['sku']);
// Load new bundled item
$new_bundled_item_id = (int)Mage::getModel("catalog/product")->getIdBySku($data['bundled_sku']);
if (!$new_bundled_item_id)
throw new Exception('Product with sku '. $data['bundled_sku'] .' does not exists');
// Bundled product already exists
if ($bundled_product_id)
{
// Load existing bundled product
$productCheck = Mage::getModel('catalog/product')->load($bundled_product_id);
$productCheck->setName($data['name']);
$productCheck->setDescription($data['description']);
$productCheck->setShortDescription($data['short_description']);
$productCheck->setMetaDescription($data['meta_description']);
$productCheck->setPrice($data['price']);
}
else
{
// Create new bundled product
$productCheck = Mage::getModel('catalog/product');
$productCheck->setData(array(
'sku_type' => 0,
'sku' => $data['sku'],
'name' => $data['name'],
'description' => $data['description'],
'short_description' => $data['short_description'],
'meta_description' => $data['meta_description'],
'type_id' => 'bundle',
'tax_class_id' => 2,
'attribute_set_id' => 4,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 1,
'price' => $data['price'],
'price_view' => 0,
'status' => 1,
'created_at' => strtotime('now'),
'store_id' => $data['store_id'],
'website_ids' => Mage::getModel('core/store')->load($data['store_id'])->getWebsiteId()
));
}
// Load option & selection data
$productCheck->getTypeInstance(true)->setStoreFilter($productCheck->getStoreId(), $productCheck);
$optionCollection = $productCheck->getTypeInstance(true)->getOptionsCollection($productCheck);
$selectionCollection = $productCheck->getTypeInstance(true)->getSelectionsCollection(
$productCheck->getTypeInstance(true)->getOptionsIds($productCheck), $productCheck
);
$optionCollection->appendSelections($selectionCollection);
// Init raw option & selection data
$bundleOptions = array();
$bundleSelections = array();
// Set raw option & selection data
if (!$optionCollection->count()) {
$bundleOptions = array(
0 => array(
'title' => $data['bundled_title'],
'default_title' => $data['bundled_title'],
'option_id' => '',
'delete' => '',
'type' => 'checkbox',
'required' => '1',
'position' => '1'
)
);
$bundleSelections[0][0] = array(
'product_id' => $new_bundled_item_id,
'delete' => '',
'selection_price_value' => 0.00,
'selection_price_type' => 0,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => 0,
'is_default' => 1
);
} else {
$new_bundled_item_found = false;
$i = 0;
$option_index = null;
foreach ($optionCollection as $option) {
if ($option->getData('title') && $option->getData('title') == $data['bundled_title']) {
$option_index = $i;
$bundleOptions[$option_index] = array(
'option_id' => $option->getOptionId(),
'required' => $option->getData('required'),
'position' => $option->getData('position'),
'type' => $option->getData('type'),
'title' => $option->getData('title'),
'default_title' => $option->getData('default_title'),
'delete' => ''
);
foreach ($option->getSelections() as $selection) {
$new_bundled_item_found = (!$new_bundled_item_found && $new_bundled_item_id == (int)$selection->getProductId());
$bundleSelections[$option_index][] = array(
'product_id' => $selection->getProductId(),
'position' => $selection->getPosition(),
'is_default' => $selection->getIsDefault(),
'selection_price_type' => $selection->getSelectionPriceType(),
'selection_price_value' => $selection->getSelectionPriceValue(),
'selection_qty' => $selection->getSelectionQty(),
'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
'delete' => ''
);
}
}
$i++;
}
if (!$new_bundled_item_found && $option_index >= 0) {
$bundleSelections[$option_index][] = array(
'product_id' => $new_bundled_item_id,
'position' => 0,
'is_default' => 1,
'selection_price_type' => 0,
'selection_price_value' => 0.00,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'delete' => ''
);
}
}
// Set flags
$productCheck->setCanSaveCustomOptions(true);
$productCheck->setCanSaveBundleSelections(true);
$productCheck->setAffectBundleProductSelections(true);
// Register flag
Mage::register('product', $productCheck);
// Set option & selection data
$productCheck->setBundleOptionsData($bundleOptions);
$productCheck->setBundleSelectionsData($bundleSelections);
// Save changes
$productCheck->save();
// Success
return true;
}

add a default value to an array

I have the following code in my opencart product.php control file.
$product_option_value_data = array();
foreach ($product_option['product_option_value'] as $product_option_value) {
$product_option_value_data[] = array(
'product_option_value_id' => $product_option_value['product_option_value_id'],
'option_value_id' => $product_option_value['option_value_id'],
'quantity' => isset ($product_option_value['quantity']) ? $product_option_value['quantity'] : '1',
'subtract' => $product_option_value['subtract'],
'price' => isset ($product_option_value['price'])? $product_option_value['price'] : '27.99',
'price_prefix' => $product_option_value['price_prefix'],
'points' => $product_option_value['points'],
'points_prefix' => $product_option_value['points_prefix'],
'weight' => $product_option_value['weight'],
'weight_prefix' => $product_option_value['weight_prefix']
);
}
I need to add default value to quantity, price, subtract.
Any help will be highly appreciated.
You are almost there. You have done that for two of your elements. Just add the same check for the subtract one.
$product_option_value_data = array();
foreach ($product_option['product_option_value'] as $product_option_value) {
$product_option_value_data[] = array(
'product_option_value_id' => $product_option_value['product_option_value_id'],
'option_value_id' => $product_option_value['option_value_id'],
'quantity' =>
isset ($product_option_value['quantity']) ?
$product_option_value['quantity'] :
'1', // THE DEFAULT FOR QUANTITY
'subtract' =>
isset ($product_option_value['subtract']) ?
$product_option_value['subtract'] :
'22', // THE DEFAULT FOR SUBTRACK
'price' =>
isset ($product_option_value['price']) ?
$product_option_value['price'] :
'27.99', //THE DEFAULT FOR PRICE
'price_prefix' => $product_option_value['price_prefix'],
'points' => $product_option_value['points'],
'points_prefix' => $product_option_value['points_prefix'],
'weight' => $product_option_value['weight'],
'weight_prefix' => $product_option_value['weight_prefix']
);
}
Hope this helps

Programmatically added Bundle Products in Magento don't display correctly in the frontend

First of all: I am using Magento 1.7.0.2.
I have programmed a function, which create a bundled product on the fly and add it to the cart. This product shows in the backend correctly, but in the frontend I'am missing the options, when I want to edit this item from the cart.
I also ran into an issue, that I can't properly add this generated items to the cart. It display me an error:
Some of the products below do not have all the required options. Please edit them and configure all the required options.
If I open the generated product in the backend and save it there, it display every option in the fronted correctly, but the other error is still appearing.
Here is my code so far:
$storeID = 1;
$websiteIDs = array(1);
$cats = array( $this->categoryPresent );
$productCounter = 0;
$presentParams['additional'] = 171; //Product ID
$presentParams['box'] = 167; //Product ID
$presentParams['count'] = 3; //amount of ordered presents
//Create base bundle with all required attributes
$product = Mage::getModel('catalog/product');
$p = array(
'sku_type' => 0,
'sku' => 'present - ' . strtotime('now'),
'name' => "Present",
'description' => 'Present',
'short_description' => 'Present',
'type_id' => 'bundle',
'attribute_set_id' => 9,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 0,
'price_view' => 0,
'status' => 1,
'created_at' => strtotime('now'),
'category_ids' => $cats,
'store_id' => $storeID,
'website_ids' => $websiteIDs,
'base_price_amount' => '',
'base_price_base_amount' => 1,
'base_price_base_unit' => 'St',
'options_container' => 'container1'
);
$product->setData($p);
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));
$optionRawData = array();
$selectionRawData = array();
//Insert Box
$productCounter++;
$optionRawData[] = array(
'required' => 1,
'option_id' => '',
'position' => $productCounter,
'type' => 'select',
'title' => 'Box',
'default_title' => 'Box',
'delete' => '',
);
$selectionRawData[$productCounter-1] = array();
$selectionRawData[$productCounter-1][] = array(
'product_id' => $presentParams['box'],
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => $productCounter,
'is_default' => 1,
'selection_id' => '',
'selection_price_type' => 0,
'selection_price_value' => 0.0,
'option_id' => '',
'delete' => ''
);
//Insert Additional
$productCounter++;
$optionRawData[] = array(
'required' => 1,
'option_id' => '',
'position' => $productCounter,
'type' => 'select',
'title' => 'Zubehör',
'default_title' => 'Zubehör',
'delete' => '',
);
$selectionRawData[$productCounter-1] = array();
$selectionRawData[$productCounter-1][] = array(
'product_id' => $presentParams['additional'],
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => $productCounter,
'is_default' => 1,
'selection_id' => '',
'selection_price_type' => 0,
'selection_price_value' => 0.0,
'option_id' => '',
'delete' => ''
);
// Set the Bundle Options & Selection Data ; Save the present
Mage::register('product', $product);
Mage::register('current_product', $product);
$product->setCanSaveConfigurableAttributes(false);
$product->setCanSaveCustomOptions(true);
$product->setCanSaveBundleSelections(true);
$product->setAffectBundleProductSelections(true);
$product->setBundleOptionsData($optionRawData);
$product->setBundleSelectionsData($selectionRawData);
$product->save();
//Add Product to cart
$cart = Mage::getSingleton('checkout/cart');
//Try an other way of setting the bundled_option
/*$option_ids = $product->getTypeInstance(true)->getOptionsIds($product);
$selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection($option_ids, $product);
$bundled_items = array();
foreach($selectionCollection as $key => $option){
//$bundled_items[$key] = $option->product_id;
$bundled_items[$key] = 1;
}
$params = array(
'product' => $product->getId(),
'related_product' => null,
'bundle_option' => $bundled_items,
'qty' => $presentParams['count'],
);*/
$cart->addProduct( $product, $presentParams['count']);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
I think, there is just a little piece of code, which is missing. I hope, someone can help to solve this problem.
UPDATE
The bundled products are generated in the front-end. So one thing I've found to do then is indexing the new product.
Here is the additional code:
//Index new item
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
$stockItem->setForceReindexRequired(true);
Mage::getSingleton('index/indexer')->processEntityAction(
$stockItem,
Mage_CatalogInventory_Model_Stock_Item::ENTITY,
Mage_Index_Model_Event::TYPE_SAVE
);
$product->setForceReindexRequired(true)->setIsChangedCategories(true);
Mage::getSingleton('index/indexer')->processEntityAction($product, Mage_Catalog_Model_Product::ENTITY, Mage_Index_Model_Event::TYPE_SAVE);
With this the generated products are correctly listed in the list-view of the responding category. But the contents of the bundle are still missing.
I just want to share with you the code I use to create products.
Let's compare it with your and search for errors.
$product = Mage::getModel('catalog/product');
$product->setSku($sku);
$product->setName($product_data['name']);
$product->setDescription($product_data['description']);
$product->setShortDescription($product_data['name']);
$product->setPrice($product_data['price'] * $price_multiplier);
$product->setTypeId('simple');
$product->setAttributeSetId(9); // need to look this up
$product->setCategoryIds(implode(',', $local_categories)); // need to look these up
$product->setWeight(1.0);
$product->setTaxClassId(0); // taxable goods
$product->setVisibility(4); // catalog, search
$product->setStatus(1); // enabled
// assign product to the default website
$product->setWebsiteIds(array(1));
$product->setMediaGallery(array('images' => array(), 'values'=>array()));
$first = true;
foreach ($local_images as $local_image_fname) {
$ma = array();
if ($first) {
$first = false;
$ma = array('image', 'small_image', 'thumbnail');
}
$product->addImageToMediaGallery($local_image_fname, $ma, false, false);
}
$stockData = $product->getStockData();
$stockData['qty'] = 1;
$stockData['is_in_stock'] = 1;
$stockData['manage_stock'] = 1;
$stockData['use_config_manage_stock'] = 0;
$product->setStockData($stockData);
$product->save();
$product_id = $product->getId();
I see, you at least do not set TaxClassId.
And weight.
EDIT
One more suggestion.
You are adding a bundle products to cart.
Why aren't you setting bundle options?
You only set count.
See nice example how to add bundle product to cart programatically:
http://inchoo.net/ecommerce/magento/programatically-add-bundle-product-to-cart-n-magento/
$params = array(
'product' => 164,
'related_product' => null,
'bundle_option' => array(
21 => 58,
20 => 55,
11 => 28,
12 => array(
0 => 31,
),
13 => array(
0 => 32,
1 => 35,
),
),
'options' => array(
3 => 'olaaaaaaaa',
),
'qty' => 2,
);
$cart = Mage::getSingleton('checkout/cart');
$product = new Mage_Catalog_Model_Product();
$product->load(164);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$message = $this->__('Custom message: %s was successfully added to your shopping cart.', $product->getName());
Mage::getSingleton('checkout/session')->addSuccess($message);

Bundle products added programmatically not showing in fronend, magento issue

I have added bundled products by fetching data from the CSV. Products added in this manner are visible in admin panel but not in front end. I have gone through all the threads but got no solution. Please help me with this situation.
require_once($magentoPath . 'includes/config.php');
require_once($magentoPath . 'app/Mage.php');
$storeID = 1;
$websiteIDs = array(1);
$cats = array("12");
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$productCheck = Mage::getModel('catalog/product');
$p = array(
'sku_type' => 0,
'sku' => '687',
'name' => "BarProduct",
'description' => 'Foo',
'short_description' => 'Bar',
'type_id' => 'bundle',
'attribute_set_id' => 4,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 0,
'price_view' => 0,
'status' => 1,
'created_at' => strtotime('now'),
'category_ids' => $cats,
'store_id' => $storeID,
'website_ids' => $websiteIDs
);
$productCheck->setData($p);
Mage::register('product', $product);
$optionRawData = array();
$optionRawData[0] = array(
'required' => 1,
'option_id' => '',
'position' => 0,
'type' => 'select',
'title' => 'FooOption',
'default_title' => 'FooOption',
'delete' => '',
);
$selectionRawData = array();
$selectionRawData[0] = array();
$selectionRawData[0][] = array(
'product_id' => 337,
'selection_qty' => 1,
'selection_can_change_qty' => 1,
'position' => 0,
'is_default' => 1,
'selection_id' => '',
'selection_price_type' => 0,
'selection_price_value' => 0.0,
'option_id' => '',
'delete' => ''
);
Mage::register('productCheck', $productCheck);
Mage::register('current_product', $productCheck);
$productCheck->setCanSaveConfigurableAttributes(false);
$productCheck->setCanSaveCustomOptions(true);
// Set the Bundle Options & Selection Data
$productCheck->setBundleOptionsData($optionRawData);
$productCheck->setBundleSelectionsData($selectionRawData);
$productCheck->setCanSaveBundleSelections(true);
$productCheck->setAffectBundleProductSelections(true);
$productCheck->save();
$indexer = Mage::getSingleton('index/indexer');
foreach ($indexer->getProcessesCollection() as $process) {
$process->reindexAll();
}
Please note that wherever If I re-save my product through admin panel it starts displaying on front-end.
Mage::register('product', $product); => Mage::register('product', $productCheck);

Categories