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 :(

2
  • Where is define $item->getProduct()? Dec 14, 2016 at 5:01
  • Please check updated question @Rakesh
    – Jackson
    Dec 14, 2016 at 6:01

1 Answer 1

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.

2
  • 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
    – Jackson
    Dec 14, 2016 at 6:03
  • It's js error , not because of my code Dec 14, 2016 at 7:16

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.