Magento Coding Solutions

How can we add in code a product to the cart with a custom option type file in Magento?

It works in Magento 1.7 but I haven't tested it on another versions

// the path of the file, relative to Magento base directory.
// For example /media/image.jpg
$image = "YOURFILE.JPG";
// the ID of the product
$product_id  = XXX;

$product     = Mage::getModel('catalog/product')->load($product_id);

$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty' => 1,
    'options' => array(
        12345 => array(
                'quote_path' => $image,
                'secret_key' => substr(md5(file_get_contents(Mage::getBaseDir() . $image)), 0, 20)),
    )
);

$cart->addProduct($product, $params);
$cart->save();

Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

delete order items by order Id in magento

<?php
       $order_id=7;
$order = Mage::getModel('sales/order')->load($order_id);
$base_grand_total = $order->getBaseGrandTotal();
    $base_subtotal = $order->getBaseSubtotal();
$grand_total = $order->getGrandTotal();
    $subtotal = $order->getSubtotal();
    $base_subtotal_incl_tax = $order->getBaseSubtotalInclTax();
    $subtotal_incl_tax = $order->getSubtotalInclTax();
    $total_item_count = $order->getTotalItemCount();
    $items = $order->getAllItems();
    foreach($items as $item){      

        if($item->getParentItemId() == '' || $item->getParentItemId() == null){

            $product_id = $item->getProductId();

/************Deleted Condition *************/
$catId=$item->getQuote_item_id();
$read= Mage::getSingleton('core/resource')->getConnection('core_read');
$value=$read->query("Select * from  mpb_sales_flat_quote_item_option where item_id='".$catId."'");
$row = $value->fetch();
 //print_r($row);

/**************************/
//echo "CODE=====".$row['code'];
                      /************ Apply your won condition ************************/
if($row['code']!='incart'){        
            //remove item price from total price of order
            $item_price = $item->getPrice();
            $item->delete();

            $order->setBaseGrandTotal($base_grand_total-$item_price);
            $order->setBaseSubtotal($base_subtotal-$item_price);
                $order->setGrandTotal($grand_total-$item_price);
            $order->setSubtotal($subtotal-$item_price);

            $order->setBaseSubtotalInclTax($base_subtotal_incl_tax-$item_price);
            $order->setSubtotalInclTax($subtotal_incl_tax-$item_price);
            $order->setTotalItemCount($total_item_count-1);
            $order->save();

            }

        }

    }
?>

Magento Ordered Items delete

<?php
       $order_id=7;
$order = Mage::getModel('sales/order')->load($order_id);
$base_grand_total = $order->getBaseGrandTotal();
    $base_subtotal = $order->getBaseSubtotal();
$grand_total = $order->getGrandTotal();
    $subtotal = $order->getSubtotal();
    $base_subtotal_incl_tax = $order->getBaseSubtotalInclTax();
    $subtotal_incl_tax = $order->getSubtotalInclTax();
    $total_item_count = $order->getTotalItemCount();
    $items = $order->getAllItems();
    foreach($items as $item){      

        if($item->getParentItemId() == '' || $item->getParentItemId() == null){

            $product_id = $item->getProductId();

/************Deleted Condition *************/
$catId=$item->getQuote_item_id();
$read= Mage::getSingleton('core/resource')->getConnection('core_read');
$value=$read->query("Select * from  mpb_sales_flat_quote_item_option where item_id='".$catId."'");
$row = $value->fetch();
//print_r($row);

/**************************/
//echo "CODE=====".$row['code'];
                      /************ Apply your won condition ************************/
if($row['code']!='incart'){        
            //remove item price from total price of order
            $item_price = $item->getPrice();
            $item->delete();

            $order->setBaseGrandTotal($base_grand_total-$item_price);
            $order->setBaseSubtotal($base_subtotal-$item_price);
                $order->setGrandTotal($grand_total-$item_price);
            $order->setSubtotal($subtotal-$item_price);

            $order->setBaseSubtotalInclTax($base_subtotal_incl_tax-$item_price);
            $order->setSubtotalInclTax($subtotal_incl_tax-$item_price);
            $order->setTotalItemCount($total_item_count-1);
            $order->save();

            }

        }

    }
?>

how to get attribute position by attribute option id in magento

      $attributeId = 134;
$collection =Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->setPositionOrder('asc')
                ->setAttributeFilter($attributeId)
->addFilter('option_id',5)
               // ->setStoreFilter(0)
                ->load();
                echo "<pre>";
                //print_r($collection);
foreach($collection as $va){
print_r($va->getData());
}


-------------------------------------------------
$attributeId = 134;
$collection =Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->setPositionOrder('asc')
               // ->setAttributeFilter($attributeId)
->addFilter('option_id',5)
               // ->setStoreFilter(0)
                ->load();
                echo "<pre>";
                //print_r($collection);
foreach($collection as $va){
print_r($va->getData());
}

Get Associated Products By product id

$pid=5; //Note: should be Conferrable product Id


$product = Mage::getModel('catalog/product')->load($pid);
$associatedProducts=$product->getTypeInstance()->getUsedProducts();
foreach($associatedProducts as $associatedProduct){
              // print_r($associatedProduct->getData());
$cid=$associatedProduct->getEntity_id();

                }

same product Add to cart as different product in magento

$asspId=2; //product Id
"testpricez" behave as option

$proAdd=Mage::getModel('catalog/product')->load($asspId);
$randT=rand(time(),10);
$renDta=$asspId.'_'.$randT;
$proAdd->addCustomOption('testpricez', $renDta);
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct($asspId, array(
'qty' => $qty,
)
);
$cart->save();

magento redirect url

$redirectUrl=abc.com/module name
Mage::app()->getFrontController()->getResponse()->setRedirect($redirectUrl);

with arguments
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl($path, $arguments));

magento getUrl custom module without index/index/

"designeruser" ia a custom module rewrite Url
http://localhost/magentotest/designeruser/index/index/id/1/ to
http://localhost/magentotest/designeruser/id/1/

$store_id=1;
$request_path = 'designeruser/id/2/' ;
$target_path='designeruser/index/index/id/2';
$oUrlRewriteCollection = Mage::getModel('core/url_rewrite')->getCollection()->addFieldToFilter('target_path', $target_path);
if(count($oUrlRewriteCollection)==0){
    echo "target path does exist";
$rewriteModel = Mage::getModel('core/url_rewrite');
$id_path ='designeruser/index/index/id/2';
$rewriteModel->setData('request_path', $request_path);
$rewriteModel->setData('target_path', $target_path);
$rewriteModel->setData('id_path',$id_path);
// $rewriteModel->setOptions('RP');
$rewriteModel->setData('store_id', $store_id);
$rewriteModel->setData('is_system', 0);
$rewriteModel->save();
}

magento rewrite url

"designeruser" ia a custom module rewrite Url
http://localhost/magentotest/designeruser/index/index/id/1/ to
http://localhost/magentotest/designeruser/id/1/



$store_id=1;

$request_path = 'designeruser/id/2/' ;
$target_path='designeruser/index/index/id/2';
$rewriteModel = Mage::getModel('core/url_rewrite');
$id_path ='designeruser/index/index/id/2';
$rewriteModel->setData('request_path', $request_path);
$rewriteModel->setData('target_path', $target_path);
$rewriteModel->setData('id_path',$id_path);
$rewriteModel->setData('store_id', $store_id);
$rewriteModel->setData('is_system', 0);
$rewriteModel->save();




$store_id=1;
$request_path = 'designeruser/id/2/' ;
$target_path='designeruser/index/index/id/2';
$oUrlRewriteCollection = Mage::getModel('core/url_rewrite')->getCollection()->addFieldToFilter('target_path', $target_path); //check rewrite url exit or not
if(count($oUrlRewriteCollection)==0){
    echo "target path does exist";
$rewriteModel = Mage::getModel('core/url_rewrite');
$id_path ='designeruser/index/index/id/2';
$rewriteModel->setData('request_path', $request_path);
$rewriteModel->setData('target_path', $target_path);
$rewriteModel->setData('id_path',$id_path);
// $rewriteModel->setOptions('RP');
$rewriteModel->setData('store_id', $store_id);
$rewriteModel->setData('is_system', 0);
$rewriteModel->save();
}

Magento get url with or without parameters

Magento lets you get url with or without parameters as below:
// get base url
Mage::getUrl()
// http://dltr.org/

// get base url and index.html page
Mage::getUrl('index.html')
// http://dltr.org/index.html

// get brands page with id parameter
Mage::getUrl('brands/brand', array('id' => 1));
// http://dltr.org/brands/brand/id/1

// get secure url
Mage::getUrl('brands/brand', array('id' => 1,'_secure' => true));
// https://dltr.org/brands/brand/id/1

// or get secure url based on current page. good for ajax urls.
Mage::getUrl('brands/brand', array('id' => 1,'_secure' => Mage::app()->getStore()->isCurrentlySecure()));
// http(s)?://dltr.org/brands/brand/id/1



 But i used this code not working 
when add magento "rewrite" then working propely

You can use Below code 

$store_id=1;
$request_path = 'designeruser/id/2/' ;
$target_path='designeruser/index/index/id/2';
$oUrlRewriteCollection = Mage::getModel('core/url_rewrite')->getCollection()->addFieldToFilter('target_path', $target_path);
if(count($oUrlRewriteCollection)==0){
    echo "target path does exist";
 $rewriteModel = Mage::getModel('core/url_rewrite');
 $id_path ='designeruser/index/index/id/2';
 $rewriteModel->setData('request_path', $request_path);
 $rewriteModel->setData('target_path', $target_path);
 $rewriteModel->setData('id_path',$id_path);
 $rewriteModel->setOptions('RP');
 $rewriteModel->setData('store_id', $store_id);
 $rewriteModel->setData('is_system', 0);
 $rewriteModel->save(); 
}