0

I'm trying to work out how to create a downloadable CSV file that contains fields for Product SKU, Quantity (should be set to 5 if more than 5 in stock) and Price in Magento 1.x. Price in this instance is in a custom attribute called ChannelPrice and not the Magento attribute Price. This CSV will be produced every 5 minutes or so using a Cron. I'm not a PHP wizard unfortunately, so I am not sure where to start here - be it via some sort of API, or directly via the database. Any things I need to consider or help will be most gratefully received! Many thanks in advance. Andrew

  • Please specify magento version? – Amit Bera Jun 5 at 10:53
  • Currently it is 1.9.3.8 – AtotheK Jun 5 at 12:24
0

This should set you on the right path. You'll need to adjust the collection to get products with a quantity of more than 5 though. Save this as a php file and then call it with a cron job every 5 minutes.

<?php
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/html; charset=utf-8');
//header('Content-Disposition: attachment; filename=report.csv');
// Load the Magento core
require_once 'app/Mage.php';
umask(0);
$store_id = '1';
Mage::app()->setCurrentStore($store_id);

//Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);

// output the column headings
echo "'sku', 'price', 'stock'";
echo "\n";

// Load the product collection
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect(array('sku','price'))
->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    );
//$products ->getSelect()->limit(20);

foreach ($products as $product) {
        $sku=$product->getSku();
        //$price=Mage::helper('core')->currency($product->getPrice(), true, false), //." ".$currencyCode;
    $price = Mage::helper('tax')->getPrice($product, $product->getPrice(), true)*1.2;
    $qty=$product->getQty();
    echo $sku . ",";
    echo $price . ",";
    echo $qty . "\n";
    $sku='';
    $price='';
    $qty='';
}

?>
  • Thanks for this. I've had a play around with the code and got everything working how I would like apart from one aspect. I want to be able to get the value of a custom attribute storing a price rather than the Magento default price attribute. I'm not sure how to go about that as there are no predefined get/set methods for individual custom attributes, unlike the default attributes Magento provides and has get/set methods for. If you had any suggestions on where I need to be looking for this I'd be most grateful. – AtotheK Jun 7 at 11:21
  • Add your attribute label to the addAttributeToSelect array and then you should be able to use $product->getYourAttribute(); where YourAttribute is your attribute label. – Denial Design Jun 7 at 21:45
  • 1
    That's brilliant - I've got it all working now! Thanks so much for your excellent help, suggestions and script. It is most appreciated :-) – AtotheK Jun 8 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.