3

I'm building a custom shipping method for Magento 2. And I couldn't seem to find product dimensions within the standard attributes

However, when receiving product data information from the checkout in my plugin it does show width, height and length. (dump shown below)

{"street":"Test street 23","postcode":"1122AB","country":"NL","city":"Amsterdam","packages":{"height":null,"width":null,"length":null,"qty":1,"value":45,"weight":0}}

As I want to make this shipping method as general as possible, where can these 'standard' attributes for dimensions be set?

Thanks.

1
  • Height is default attribute. But, width and length do not exist. Commented Dec 2, 2017 at 22:25

2 Answers 2

2

These default product dimensions I was looking for, are now available since magento version 2.2.5 and are retrievable like so;

$product->getTsDimensionsLength();
$product->getTsDimensionsWidth();
$product->getTsDimensionsHeight();
2
  • These methods don't exist... may have been an older version, but I couldn't find them, even grep-ed 'ts_dimension' for data key/db-columns, still nothing. (I did find it in Temando module, but it's not part of core.)
    – Mac A.
    Commented Jan 27, 2020 at 18:08
  • @MacA. That is indeed the case on newer versions of Magento (2.3.3), it is a bug which can be tracked here github.com/magento/magento2/issues/24948
    – Dion
    Commented Jan 28, 2020 at 14:11
1

It is recommended to add width, length, height attributes to product from admin panel. To create a new attribute login to your website’s admin dashboard and navigate to the Stores>Attributes>Product section of Store

This will open the list of all attributes currently available on store. To create a new one click on the big orange Add New Attribute button.

Now you will be able to configure the attribute. Attribute Code – The name of the attribute that will be used by the system Scope – allows you to declare the attribute value saving scope like Website - the value of this attribute for a product can be different in different Websites but have to stay the same between Store Views contained under the same Website. Store View - the value of this attribute for a product can be different in all Websites and all Store Views Website - the value of this attribute for a product can be different in different Websites but have to stay the same between Store Views contained under the same Website Global - the value of this attribute for a product must be the same for all websites and stores

Unique Value – Set this to Yes, if you want the data saved in this attribute to be unique for each product. If you have different products made of the same material or having the same feature, leave this to No Input Validation for Store Owner – If you want to validate the data before applying changes to the attribute, select Yes Add to Column Options – To add this attribute to the list of column options please select Yes Use in Filter Options – If you set this to Yes the attribute will be added to the list of filter options

From the menu on the left, click on the Storefront Properties tab to configure the frontend behavior of the attribute.

To save your new attribute click on the Save Attribute button on the top right corner of your screen. This will bring you back to the attributes list page on which you should see your new attribute.

For more refer to this short tutorial : https://www.fastcomet.com/tutorials/magento2/product-attributes

5
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Jarnail S
    Commented Dec 3, 2017 at 2:11
  • yes you have raison Commented Dec 4, 2017 at 8:42
  • This answers how to do it manually, im looking for the already build in height, width and length whose are retrieveable by calling getPackageHeight(), getPackageWidth(), and getPackageWidth()
    – Dion
    Commented Dec 13, 2017 at 18:52
  • $this->getProduct()->getAttributeText('attribute_text'); e.g: $this->getProduct()->getAttributeText('widht'); Commented Dec 15, 2017 at 8:36
  • or $_product = $block->getProduct(); $attribute_code="description"; // attribute with text value $description_attribute = $_product->getResource()->getAttribute($attribute_code); $description= $description_attribute ->getFrontend()->getValue($_product); Commented Dec 15, 2017 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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