1

I have achieved

Magento 2: How to change Weight field for DHL Shipping Method [SOLVED]

I tried to put

protected $_product;

public function __construct(\Magento\Catalog\Model\Product $product) {
    $this->_product = $product;
}

public function _getAllItems() {

    $allItems = $this->_request->getAllItems();
    $fullItems = [];

    foreach ($allItems as $item) {
        $this->_product->load($item->getProduct()->getId());
        $itemWeight =  $_product->getData('weight_with_package');
    }
}

It's not working :(

  • Where is define $item->getProduct()? – Rakesh Jesadiya Dec 14 '16 at 5:01
  • Please check updated question @Rakesh – Ankit Shah Dec 14 '16 at 6:01
0

You can use product API to get product data :

protected $_productRepository;

public function __construct(\Magento\Catalog\Api\ProductRepositoryInterface $productRepository) {
     $this->_productRepository = $productRepository;
}

public function _getAllItems() {
     $allItems = $this->_request->getAllItems();
     $fullItems = [];

    foreach ($allItems as $item) {
         $this->_product = $this->productRepository->getById($item->getProduct()->getId());        
        $itemWeight =  $_product->getData('weight_with_package');
    }  
}

Hope It works for you, If you will get any issue please contact.

  • It's giving window.checkoutConfig = <br /> <b>Fatal error</b>: Uncaught Error: Call to a member function getValue() on null in /var/www/html/magento/vendor/magento/module-shipping/Model/Carrier/AbstractCarrier.php:132 – Ankit Shah Dec 14 '16 at 6:03
  • It's js error , not because of my code – Ronak Chauhan Dec 14 '16 at 7:16

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.