I have some intriguing questions related to Custom Options of Product:-
Is there any difference between Options & Custom Options? This is because I have found two different properties for each Product details, in almost all product-related modules:
options
custom_options
However there is only one class for only the Product Option, which tends to take care of the Custom Options. I am seeking clarification on this point.
I am trying to fetch the Custom Options of an Ordered Item, including the Custom Option Price and Price Type. The problem is that Magento only stores the Option Value for the corresponding Ordered Item, and not all its details (like Custom Option Price & Price Type).
So I created an object of this class Mage_Catalog_Model_Product_Option_Value
, considering only the drop_down
Custom Option Type. I've provided my code below, but it is still in vain and not fetching the desired results. How can I rectify this code?
Code for point #2:
echo "<pre>";
// $collection contains the whole Order Collection
foreach ($collection as $order) {
foreach ($order->getAllItems() as $item) {
$customOptions = $item->getProductOptions();
foreach ($customOptions['options'] as $_eachOption) {
// Value ID is stored in this field "option_value"
$objModel = Mage::getModel('catalog/product_option_value')->load($_eachOption['option_value']);
// This should provide all the details of this particular Option Value as chosen by the Customer when ordering this Product, but unfortunately it doesn't
print_r($objModel->getData());
/**
* This gives the output as, without any details on Price and Price Type:-
* Array
* {
* [option_type_id] => 13014
* [option_id] => 4921
* [sku] => XBPS22
* [sort_order] => 0
* }
*/
unset($objModel);
}
}
}
echo "</pre>";
After doing some checking, I found that the price related to each Option Values are stored in catalog_product_option_type_price
database table, and the price related to each Options are stored in catalog_product_option_price
database table. So there must be some way as to how Magento fetches the corresponding Custom Option Value prices.
When you add custom options to a product in Magento 2, you give your customers the opportunity to choose product options as to their needs without relying on the product attributes. Customizable options allow you to categorize products, create cart price rules and dynamic categories rules.
In this code, I load the product by id, get the option collection, which in my tests only contains the custom options for a product, not attributes or other options, iterate through the options and load the values collection for each option. This demo code should be testable pretty much anywhere as long as you have a product ID.
<?php
$productID = $this->getProductId(); //Replace with your method to get your product Id.
$product = Mage::getModel('catalog/product')->load($productID);
$options = Mage::getModel('catalog/product_option')->getProductOptionCollection($product);
foreach ($options as $option) {
Mage::log('Name: ' . $option->getDefaultTitle());
Mage::log(' Type: ' . $option->getType());
Mage::log(' Class: ' . get_class($option));
Mage::log(' Price/Type: ' . ($option->getPrice() ? $option->getPrice() : '0.00') . ' / ' . $option->getType());
if ($option->getType() === 'drop_down') {
$values = Mage::getSingleton('catalog/product_option_value')->getValuesCollection($option);
Mage::log(' Values: (name/price/type)');
foreach ($values as $value) {
Mage::log(' ' . $value->getTitle() . ' / ' . $value->getPrice() . ' / ' . $value->getPriceType());
}
}
}
?>
Example Log Output:
2014-02-18T20:15:25+00:00 DEBUG (7): Name: Turtle Color
2014-02-18T20:15:25+00:00 DEBUG (7): Type: drop_down
2014-02-18T20:15:25+00:00 DEBUG (7): Class: Mage_Catalog_Model_Product_Option
2014-02-18T20:15:25+00:00 DEBUG (7): Price/Type: 0.00 / drop_down
2014-02-18T20:15:25+00:00 DEBUG (7): Values: (name/price/type)
2014-02-18T20:15:25+00:00 DEBUG (7): Red / 0.0000 / fixed
2014-02-18T20:15:25+00:00 DEBUG (7): White / 0.0000 / fixed
2014-02-18T20:15:25+00:00 DEBUG (7): Blue / 0.0000 / fixed
Example available data for $option Mage::log($option->toArray())
:
note: price and price_type are only available on the option values for drop_down type options.
2014-02-18T20:19:44+00:00 DEBUG (7): Array
(
[option_id] => 135
[product_id] => 80
[type] => field
[is_require] => 0
[sku] =>
[max_characters] => 25
[file_extension] =>
[image_size_x] =>
[image_size_y] =>
[sort_order] => 90
[description] =>
[default_title] => Turtle Name
[store_title] =>
[title] => Turtle Name
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] =>
[store_price_type] =>
[price] => 0.0000
[price_type] => fixed
)
Example available data for $values Mage::log($values->toArray())
:
2014-02-18T20:25:21+00:00 DEBUG (7): Array
(
[totalRecords] => 2
[items] => Array
(
[0] => Array
(
[option_type_id] => 1149
[option_id] => 229
[sku] =>
[sort_order] => 10
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] => 0.0000
[store_price_type] => fixed
[price] => 0.0000
[price_type] => fixed
[default_title] => 31"
[store_title] => 31"
[title] => 31"
)
[1] => Array
(
[option_type_id] => 1150
[option_id] => 229
[sku] =>
[sort_order] => 20
[default_price] => 0.0000
[default_price_type] => fixed
[store_price] => 0.0000
[store_price_type] => fixed
[price] => 0.0000
[price_type] => fixed
[default_title] => 31.5"
[store_title] => 31.5"
[title] => 31.5"
)
)
)
First load the products from the collection then loop as follows :
$product = 100; // product id, you should get first
foreach($product->getOptions() as $options)
{
$options->getType(); // get option type
$optionValues = $options->getValues();
foreach($optionValues as $optVal)
{
print_r($optVal->getData());
// or $optVal->getData('option_id')
}
}
* Modified *
$prdSku = 125; // sample sku
$product = Mage::getModel('catalog/product');
$prdId = $product->getIdBySku($prdSku);
$product->load($prdId);
if ($product->getId()) {
if ($product->hasCustomOptions()) {
foreach ($product->getOptions() as $opt) {
$optionType = $opt->getType();
if ($optionType == 'drop_down') {
$values = $opt->getValues();
foreach ($values as $k => $v) {
Mage::log("Array Key = $k;");
Mage::log("Array Value: $v");
}
}
}
}
A similar question was asked here:
get selected custom option price for simple product in observer
I gave the following answer there:
If you have the option value ID you can also do I direct query to get the option price. I know this is not completely the Magento way and you might have to do some custom calculating (for procent prices for example), but you could do something like this:
$optionValueId = 1234; // replace with you option value ID
$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('read');
$optionvaluePrice = $connection->fetchRow(
sprintf('SELECT * FROM %1$s WHERE option_type_id = %2$d',
$resource->getTableName('catalog/product_option_type_price'),
$optionValueId
)
);
Sadly, Magento doesn't seem to have a model to load a single option price separately.
$session= Mage::getSingleton('checkout/session');
$getotal = Mage::helper('checkout')->getQuote()->getGrandTotal();
foreach($session->getQuote()->getAllItems() as $item)
{
$options = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getProductOptionsCollection();
foreach ($options as $o)
{
$title = $o->getTitle();
$values = $o->getValues();
foreach($values as $v)
{
$mydata = $v->getPrice();
}
}
}
You can use this code in order to get price set for custom options for products in shopping cart.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With