How to show price range of custom option in magento - magento

I am using magento 1.7, on the product detail price I have some custom option and some configurable product option.
What I want to do is that I want to show the price of the all the option in range, like if lowest total price option is 18.50 and highest total price option is 55.90 than it should show somewhere on product page like "18.50 - 55.90".
Thanks in advance.

try below code
//load configurable product
$product = Mage::getModel('catalog/product')->load(some_id);
//load all children
$childProducts = Mage::getModel('catalog/product_type_configurable')
->getUsedProducts(null,$product);
foreach($childProducts as $child){
$_child = Mage::getModel('catalog/product')->load($child->getId());
$childPrice = $_child->getPrice();
//compare the $childPrice
}
or for inline query should be like
<?php
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $db->query('SELECT a.product_id, a.product_super_attribute_id, ap.product_super_attribute_id, ap.pricing_value FROM catalog_product_super_attribute AS a INNER JOIN catalog_product_super_attribute_pricing AS ap ON a.product_super_attribute_id=ap.product_super_attribute_id WHERE a.product_id='.$_product->getId().' ORDER BY ap.pricing_value DESC LIMIT 1');
$rows = $result->fetch();
echo 'From '.Mage::helper('core')->currency($_product->getPrice()).' to '.Mage::helper('core')->currency($rows ['pricing_value']+$_product->getPrice());
?>
Edit (optional)
$prices = array();
$associated = $_product->getTypeInstance(true)->getAssociatedProductCollection($_product)
->addAttributeToSelect('special_pric$
foreach ($associated as $assoc) {
$prices[] = $assoc->getSpecialPrice();
}
if (count($prices)) {
$min_price = min($prices);
$max_price = max($prices);
} else {
$min_price = 0;
$max_price = 0;
}

Related

display parent category and sub category and sub sub category name from product collection

I am try to write product categories name into csv file. My requirement is have three column in csv "parent category name" and "subcategory name" "subsub category name"
So i get product collection and got a categories ids by below code.
$products = Mage::getModel("catalog/product")->getCollection();
foreach ($products as $product) {
$catIds = $product->getCategoryIds();
}
Example tree structure: CategoryName -- (Category Id)
Main Category --(3)
Sub_Cat_1 (4)
SubSub_Cat_1 (6)
SubSub_Cat_2 (7)
SubSub_Cat_3 (8)
Sub_Cat_2(5)
SubSub_Cat_4 (9)
SubSub_Cat_5 (10)
So i got the $catIds = array(3,4,5,6,7) but i want name with relation.
So i want below output in CSV file.
Parent_Cat | Sub_Cat | SubSub_Cat
Main Category | Sub_Cat_1,Sub_Cat_2 | SubSub_Cat_1,SubSub_Cat_2
I have tried lot of methods but no luck.
So please kindly help in advance to achieve.
Thanks,
You can have an array containing all the categories on their specific level (each key will represent a level) like this:
$categoriesLevels = array();
$categoryModel = Mage::getModel('catalog/category');
$tree = $categoryModel->getTreeModel();
$tree->load();
$catIds = $tree->getCollection()->getAllIds();
foreach ($catIds as $catId){
$category = Mage::getModel('catalog/category')->load($catId);
$catLevel = $category->getLevel();
if (!isset($categoriesLevels[$catLevel])) {
$categoriesLevels[$catLevel] = array();
}
$categoriesLevels[$catLevel] = array_merge($categoriesLevels[$catLevel],array($category->getName()));
}
$categoriesLevels = array_filter($categoriesLevels);
So $categoriesLevels[0] will contain the name of the Root Catalog, $categoriesLevels[1] will contain the name of the Default Category, $categoriesLevels[2] will contain the name of all the Subcategories, $categoriesLevels[3] will contain the name of all the SubSubcategories, etc.
edit
If you only want the categories for a specific product, just change the way the $catIds are obtained:
$categoriesLevels = array();
$catIds = $product->getCategoryIds();
foreach ($catIds as $catId){
$category = Mage::getModel('catalog/category')->load($catId);
$catLevel = $category->getLevel();
if (!isset($categoriesLevels[$catLevel])) {
$categoriesLevels[$catLevel] = array();
}
$categoriesLevels[$catLevel] = array_merge($categoriesLevels[$catLevel], array($category->getName()));
}
$categoriesLevels = array_filter($categoriesLevels);
Finally i find the solution so please check this for my answer.
$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToFilter('status', 1);
$products->addAttributeToSelect('*');
$cats = $product->getCategoryIds();
$cat_lel_one = array();
$cat_lel_two = array();
$cat_lel_third = array();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
$cat_info = $_cat->getData();
if($cat_info['level'] == 1){
$cat_lel_one[] = $_cat->getName();
}
if($cat_info['level'] == 2){
$cat_lel_two[] = $_cat->getName();
}
if($cat_info['level'] == 3){
$cat_lel_third[] = $_cat->getName();
}
}
My first level of categories in $cat_lel_one variable. My Second level
of categories in $cat_lel_two variable. My third level of categories
in $cat_lel_third variable.

How to get Product listing filters through custom module of magento?

I want to create product filters in listing pages of category using our own custom apis i have tried many things like
$layer = Mage::getModel("catalog/layer");
$category = Mage::getModel("catalog/category")->load($categoryid);
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
But this only loads the Filterable atrribute code for the category But we want the Filters values and count for the product collection available in that category.
Below code will load all the available options of that attribute but I want it for product collection specific
foreach($attributes as $attribute){
$options = $attribute->getSource()->getAllOptions(false);
}
Like we have Mobiles category having 1000s of mobiles and having filterable attribute Brand so i need
Brand
Brand A (100 products)
Brand B (500 products)
Brand C (400 products)
How can this be achived if i don't want to load whole product collection as loading 1000s of product will take too much time to response.
This code works for me :
public function ws_getfiter($store_id,$categoryid){
Mage::app()->setCurrentStore($store_id);
$layer = Mage::getModel("catalog/layer");
$category = Mage::getModel("catalog/category")->load($categoryid);
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
$filter = array();
$count = 0;
foreach ($attributes as $attribute)
{
if ($attribute->getAttributeCode() == 'price') {
$filterBlockName = 'catalog/layer_filter_price';
} elseif ($attribute->getBackendType() == 'decimal') {
$filterBlockName = 'catalog/layer_filter_decimal';
} else {
$filterBlockName = 'catalog/layer_filter_attribute';
}
$filter[$count]["code"] = $attribute->attribute_code;
$filter[$count]["type"] = $attribute->frontend_input;
$filter[$count]["label"] = $attribute->frontend_label;
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
$innercount = 0;
$filter_data = array();
foreach($result->getItems() as $option) {
$filter_data[$innercount]["count"] = $option->getCount();
$filter_data[$innercount]["label"] = $option->getLabel();
$filter_data[$innercount]["id"] = $option->getValue();
$innercount++;
}
$filter[$count]["values"] = $filter_data;
$count++;
}
return $filter;
}
But using this we are only getting single level of filter but i want it for multiple level.

Magento - Get products price with tax and discounts in cart/order (Criteo tags)

For the implementation of Criteo tags, I'm trying to get the price (among other things) of all products in the cart (and success page) with tax and discounts.
I'm currently doing something like this, but it only displays price with discount and without tax :
$cartAllItems = Mage::getModel('checkout/cart')->getItems();
foreach ($cartAllItems as $item){
$price = Mage::helper('tax')->getPrice($item->getProduct(), $item->getProduct()->getFinalPrice());
// other things
}
I've been testing around a lot of things and can't make it work.
Thx for the help
I think you can use,
Mage::helper('checkout')->getQuote()->getShippingAddress()->getData('tax_amount')
This will return you total tax amount.
or you can use
$totalItemsInCart = Mage::helper('checkout/cart')->getItemsCount();
$totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals();
$subtotal = round($totals["subtotal"]->getValue());
$grandtotal = round($totals["grand_total"]->getValue());
if(isset($totals['discount']) && $totals['discount']->getValue()) {
$discount = round($totals['discount']->getValue());
} else {
$discount = '';
}
if(isset($totals['tax']) && $totals['tax']->getValue()) {
$tax = round($totals['tax']->getValue());
} else {
$tax = '';
}
Modified
I guess for your requirement
foreach ($productIds as $productId) {
$_product = Mage::getModel('catalog/product')->load($productId);
$productsPrice = floatval($_product->getData("price"));
// Get the product's tax class' ID
$taxClassId = $_product->getData("tax_class_id");
echo 'Tax Class ID '.$taxClassId.'
';
// Get the tax rates of each tax class in an associative array
$taxClasses = Mage::helper("core")->jsonDecode( Mage::helper("tax")-
>getAllRatesByProductClass() );
echo 'Tax Classes '.$taxClasses.'
';
// Extract the tax rate from the array
$taxRate = $taxClasses["value_".$taxClassId];
echo 'Tax Rate '.$taxRate.'
';
}
?>
You can get the discount and tax value per item and make the calculation.
$_item->getDiscountAmount
$_item->getTaxAmount
$totalItemPrice = $_item->getPrice - $_item->getDiscountAmount + $_item->getTaxAmount

get qty of products in an order with several products

Hi i would like to know how to get the qty_ordered of a specific product in an order with several products.
i have tried these
$orders = Mage::getModel('sales/order')->getCollection()->
addAttributeToSelect('shipping_description')->
addAttributeToSelect('increment_id')->
addAttributeToSelect('base_grand_total')->
addAttributeToSelect('total_qty_ordered')->
addAttributeToSelect('shipping_address_id')->
addAttributeToSelect('billing_address_id')->
addAttributeToSelect('created_at')->
addAttributeToSelect('shipping_incl_tax')->
addAttributeToFilter('status', 'pending');
but addAttributeToSelect('total_qty_ordered')-> which total? which product?
"total_qty_ordered" give total no orders qty of all product in an order
If,you want item qty of product then
you can try
<?php $order_id = 2314; //use your own order id
$order = Mage::getModel("sales/order")->load($order_id); //load order by order id
$ordered_items = $order->getAllItems();
foreach($ordered_items as $item){ //item detail
echo $item->getItemId();
//product id
echo $item->getSku();
echo $item->getQtyOrdered();
//ordered qty of item
echo $item->getName();
// etc.
} ?>
for me it worked like this:
<?php
$_order = $this->getOrder(); //call the order
$order_qty = floor($_order->getData('total_qty_ordered')); //get qty of all items rounded to full number (without 3.0000 or so)
echo $order_qty;
?>
used it on template/sales/order/totals.phtml
You can Try this also:-
$orderCollection = Mage::getModel('sales/order');
$order_data = $orderCollection->getAllVisibleItems();
foreach ($order_data as $key) {
$id = $key->getProductId();
$orderItems = Mage::getResourceModel('sales/order_item_collection')
->addAttributeToFilter('product_id', $id);
foreach ($orderItems as $key ) {
echo $number_of_prod = $key->getQtyOrdered();
}
}

Check If Product Exists

I have some php code which I use SKU for it:
$sku = $id
$_product=Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); //Get Product by ID (ASIN)
$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); //if in stock
$_prodcats = $_product->getCategoryIds();
What I would like to add to this code if $sku is wrong (Not found in database with $_product line) to be able to change it manually for example if the $sku is not existent change it to $sku = "909010";
Thank you for the help
function get_product($sku){
$product=Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if (!$product){
$new_sku = '909010'; // If Real SKU doesn`t exsist, change to '909010'
$product=Mage::getModel('catalog/product')->loadByAttribute('sku',$new_sku);
}
return $product;
}
var_dump(get_product('your-sku')); // Real Sku
$sku = $id
$_product=Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); //Get Product by ID (ASIN)
if(!is_object($_product) || !$_product->getId()){
//do your stuff here
}
$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); //if in stock
$_prodcats = $_product->getCategoryIds();
$sku = $id
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); //Get Product by ID (ASIN)
$_product = !empty($product) ? $product : Mage::getModel('catalog/product')->loadByAttribute('sku','YOUR-ERROR-SKU');
$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); //if in stock
$_prodcats = $_product->getCategoryIds();

Resources