Magento: Images and the product_media API

Introduction

I built an image importer and have a few notes about using Magento’s Product Images API. I have a large number of images from our paper catalog, and also from our old cart solution. Thousands of images, really. A lot.

So I built a little php ditty that would recurse directories, looking for the best image it could find. In my old cart, this was an image called 0500.jpg. Directories are constructed like this: /wpi/s/sku9999/. When I find 0500.jpg under this directory I put it into magento using the Product Images API. I found out a couple of things:

Notes from the Field

1. The API is hardcoded to call the uploaded file “image_XX.(mime type).” I could see no way to change that; the net effect is that every image you add to Magento via the Product Images API ends up in media/catalog/product/i/m.

In order to more evenly distribute my product images across directories, I hacked the Core API. I know it’s a no-no, but I’ll hack it right back. I only needed it for this mass upload – 5,000+ items. Dear Magento: Please make my hack an option – or even permanent change – to Magento.

In app/code/core/Mage/Catalog/Model/Product/Attribute/Media/Api.php, At line 137, I changed this:

$fileName  = 'image.' . $this->_mimeTypes[$data['file']['mime']];

To this:

$fileName  = $productId.'.'. $this->_mimeTypes[$data['file']['mime']];

2. The Administration Backend disassociates an image from its item when you say “Remove,” but it does not actually delete it from disk.

I Call the Magento Product API – at Last

Here’s the function I called to do the upload. It’s essentially the same as teh Product Images API documentation example, except that I am using a single image for all “types.” It’s a bit fuzzy in the documentation, but using this function, you’ll end up with 1 image for Base Image, Small Image and Thumbnail Image. I’m printing out some extra debugging information as well.

function
uploadImage( $sku, $imagePath ) {
    global $proxy;
    global $sessionId;
 
    $sku = strtoupper($sku );
    $newImage = array(
        'file' => array(
            'content' => base64_encode(file_get_contents($imagePath)),
            'mime'    => 'image/jpeg'
        ),
        'label'    => "Item:" . $sku,
        'position' => 0,
        'types'    => array('image', 'small_image', 'thumbnail' ),
        'exclude'  => 0
    );
    try {
        $imageFilename = $proxy->call($sessionId, 'product_media.create', array($sku, $newImage));
        var_dump($proxy->call($sessionId, 'product_media.list', $sku));
    }
    catch(Exception $e) {
        print( "Error assigning image to $sku: " . $e->getMessage() . "\n" );
    }
}

See ya next time!

Magento: Show Quantity Box in Category View

Thanks to nebil and pastanislas over on the Magento board for this one.

In developing a template for a Wholesale Site, I want to be able to have a Quantity Box in each category view. Typically, a wholesale customer is placing an order for large numbers of items. Forcing him to drill down into an individual product is exactly perfectly not what we want.

Rather, we want our customer to be able to specify quanities and click the Add to Cart button, and keep moving right down the page.

The gang over at the Magento board had this solved for me in no time!

In my theme’s /template/catalog/product/list.phtml file, I changed the <button> line near line 108 from this:

107
108
<?php if($_product->isSaleable()): ?>
<button class="form-button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><?php echo $this->__('Add to Cart') ?></span></button>

to this:

108
109
110
111
112
113
114
115
116
117
118
119
<form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId(); ?>">
109 <input name="qty" type="text" class="input-text qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" />
<button class=form-button" onclick="productAddToCartForm_<?php echo $_product->getId(); ?>.submit()"><span><?php echo $this->__('Add to Cart') ?></span></button>
</form>
<script type="text/javascript">
     var productAddToCartForm_<?php echo $_product->getId(); ?> = new VarienForm('product_addtocart_form_<?php echo $_product->getId(); ?>');
     productAddToCartForm_<?php echo $_product->getId(); ?>.submit = function(){
     if (this.validator.validate()) {
             this.form.submit();
         }
     }.bind(productAddToCartForm_<?php echo $_product->getId(); ?>);
</script>

NOTE!!! This is in TWO places! You’ll have to make a similar change around line 62 as well. A search on isSaleable() should do the trick.

The only real change from the code found in the Magento thread is that I made the button class “form-button” it was just plain “button” in the original.

Auuugghh! I Thought This Eagles Team Was Different

The Eagles Duped Us Again
The Eagles Duped Us Again

“[T]o lowest pitch of abject fortune thou art fallen” – John Milton

The current Eagles regime is without doubt, its most successful in its history. But look at that history – there’s not much there.

They lured us in with a late season throttling of the hated Cowboys;  a workmanlike dispatch of the Who CaresVikings; and then a divisional playoff win over the loathsome Giants … oh, how they sucker-punched us yesterday. Not only did they lose the game, they lost it twice.

Wooing us with a thrilling comeback from and 18 point deficit! Left for dead they rallied, soaring to a 25-24 lead.

Only to let the Cardinals (the Cardinals. Does anyone remember when they were in the NFC East, and were totally inept and yet – they still beat us just about every year?) march right down the field.

“Ha ha! Fooled ya!”

And yet, I will undoubtedly be all a-twitter when draft time rolls around again.