Category Archives: Magento

How Can I Export Magento Sales Order Customer Info Programmatically

I would like to export Magento Sales Order Customer info programmatically, what functions or methods should I use and what steps should I do to export the Magento sales order customer info programmatically

You need to retrieve the Magento sales orders first (with attribute filter if need), then call fputcsv to save data into excel format file

Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$orders = Mage::getModel('sales/order')->getCollection();
$orders->addAttributeToFilter('grand_total', array('gt' => '10.00'));
$fp = fopen('path/exportfile.csv', 'w');
if(count($orders) > 0){
    foreach($orders as $order) {
        $countryCode = $order->getBillingAddress()->getCountryId();
        $country = Mage::getModel('directory/country')->loadByCode($countryCode);
        $countryName = $country->getName();
        $fields = array($order->getId(), $order->getBillingAddress()->getFirstname(),$order->getBillingAddress()->getLastname(), $order->getBillingAddress()->getEmail(), $order->getBillingAddress()->getCity(),  $order->getBillingAddress()->getTelephone(), $countryName, $order->getStatus());
        fputcsv($fp, $fields);
    }
}

How Can I Execute Custom SQL Query From Magento

How can I execute custom sql query from Magento in my extension

Its based on what kind of sql query you would like to execute in Magento.

If you would like to excute a “read” custom sql query only, you need to retrieve read sql connection

$readConnection = Mage::getSingleton('core/resource')->getConnection('core_read');

If you would like to excute a “write” custom sql query only, you need to retrieve write sql connection

$readConnection = Mage::getSingleton('core/resource')->getConnection('core_write');

To execute custom fetch sql query from Magento

$read = Mage::getSingleton('core/resource')->getConnection('core_read'); 
$results = $read->fetchAll("select * from table where id = 1"); 

To execute custom insert sql query from Magento

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->insert(
        "table", 
        array("column" => value)
);

To execute custom update sql query from Magento

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->update(
        "table",
        array("name" => "value"),
        "id=1"
);

To execute custom delete sql query from Magento

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->delete(
    "table",
    "entity_id=1"
);

To execute custom raw sql query from Magento

$read = Mage::getSingleton('core/resource')->getConnection('core_read'); 
$query = $read->query("select * from table");

How Can I Create Magento Invoice Programmatically

How can I create Magento invoice programmatically

You need to load Magento order first and then use “prepareInvoice” method to create Magento invoice

$orderid = 1;
$order = Mage::getModel("sales/order")->load($orderid );
if($order->canInvoice()){
    $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
    if($invoice->getTotalQty()){
        $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
        $invoice->register();
        $transactionSave = Mage::getModel('core/resource_transaction')->addObject($invoice)->addObject($invoice->getOrder());
        $transactionSave->save();
    }
}

How Can I Add Images To Magento Product Programmatically

How can I attach the images to the Magento product programmatically

You will need to upload the Magento product images to the server first, then attach the image path to the specific Magento product programmatically

$importDir = 'productimage/1/';
$productsData = array('image1.jpg','image2.jpg');

foreach($productsData as $fileName){
    $productSKU = '1';
    $ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
    $filePath = $importDir.$fileName;
    if (file_exists($filePath)) {
        $ourProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false, false);
        $ourProduct->save();
    }
}

How Can I Update Magento Product Price Programmatically

How can I update the price of the Magento product programmatically

You need to load product object by ID first and then call “setPrice” method to change the Magento product price programmatically.

$store_id = 1;
$productId = 1;
$newprice = 1.00;

$product = Mage::getModel('catalog/product')->setStoreId($store_id)->load($productId);
$product->setPrice($newprice );
$product->save();

How To Associate Magento Simple Product To The Configurable Product Programmatically

How can I link a Magento simple product to a Magento configurable product Programmatically

You can use “setConfigurableProductsData” method to assign a Magento simple product to the

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

$configurableProductsData = array();
$configurableProductsData['2'] = array( //['2'] = id of a simple product associated with this configurable
    '0' => array(
        'label' => 'Red', //attribute label
        'attribute_id' => '3', //attribute ID of attribute 'color' in my store
        'value_index' => '6', //value of 'Red' index of the attribute 'color'
        'is_percent' => '0', //fixed/percent price for this option
        'pricing_value' => '21' //value for the pricing
    )
);
$_product->setConfigurableProductsData($configurableProductsData);
$_product->save();

configurable product Programmatically