user2878068
user2878068

Reputation: 13

Magento - create product and set dimensions in php

I'm already creating Magento products in php. Now I want to add the product's weight and dimensions (length, width, height), so that it can be used by a shipping plugin.

For settings the weight, I found this method: $product->setWeight(1.0); But I can't find any similar method for the dimensions.

Now my questions is: How can I set the product dimensions?

This is the code I'm using for adding a product:

umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::getSingleton('core/session', array('name'=>'adminhtml'));

$product = Mage::getModel('catalog/product');

$product->setSku("SKU");
$product->setName("name");
$product->setDescription("description");
$product->setShortDescription("short description");
$product->setPrice(9.99);

$product->setTypeId('simple');
$product->setAttributeSetId(9);
$product->setCategoryIds("0");

$product->setWeight(1.0);

[...] // setting stock and tax information

$product->save();

Thanks in advance

Upvotes: 0

Views: 1778

Answers (2)

Reza
Reza

Reputation: 30

Magento has uses Magic functions hence if the attribute is associated with a product you can simply call setATTRIBUTENAME to set its value or getATTRIBUTENAME to get its value.

So in your case if you have a attribute dimentions called width and height you can call $product->setWidth(100) and $product->setHeight(50) to set its height and weight.

Upvotes: 0

Marius
Marius

Reputation: 15216

You can set any attribute in the same way as you set the weight. Let's say the attribute code is some_attribute you can do this:

$product->setData('some_attribute', 'value_here');
//or
$product->setSomeAttribute('value_here');

Upvotes: 1

Related Questions