3

I have made a shipping Model in Magento 2 . Inside the collectRates function. I am getting all items in the cart and looping through them.

I can set the price based on the product title. But I can seem to get the value of the attribute this product has for example "shipping_code".

$products = $request->getAllItems();

foreach($products as $product){
   $productName = $product->getName();
}

I have tried everything.

$attributeValue = $product->getData('shipping_code');
$attributeValue = $product->getResource()->getAttribute('shipping_code');
$attributeValue = $product->getAttribute('shipping_code');

What am I doing wrong?

Magento/App/Code/module_folder/custom_shipping/Model/Carrier/custom_shipping.php

public function collectRates(RateRequest $request){

    $products = $request->getAllItems();
    foreach($products as $product)
    {
       $productName = $product->getName();
       $attributeValue = $product->getData('shipping_code');
    }



     $shippingPrice = "100";

    if ($productName == "test") {
        $shippingPrice = "200";
    }

    if ($attributeValue == "A") {
        $shippingPrice = "300";
    }

    $method->setPrice($shippingPrice);
    $result->append($method);

    return $result;
}
  • For shipping_code attribute make sure from Admin -> Stores -> Product Attributes -> Edit your attribute & Go to Storefront Properties & set Used in Product Listing to Yes – Ankit Shah Dec 14 '16 at 1:39
  • Thanks, this was off. Unfortunately, this hasn't fixed the problem none of the ways above are getting the value. – Nathaniel Meyer Dec 14 '16 at 1:46
  • Please paste your file code with path from magento root directory – Ankit Shah Dec 14 '16 at 1:49
  • Updated post with file and directory – Nathaniel Meyer Dec 14 '16 at 1:53
  • Hi @Nathaniel Have u tried answer? – Ankit Shah Dec 14 '16 at 2:28
3
foreach($products as $product)
{
   $productName = $product->getName();

   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $product = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId());
   $shippingCode = $product->getData('shipping_code');
}

If somone gives answer without objectManager then it will be better.

  • Just gave this a go with no luck sorry :( – Nathaniel Meyer Dec 14 '16 at 2:35
  • Ahh strage. Please make all to Yes on Admin for "Storefront Properties" & Check. Have u cleared cache? – Ankit Shah Dec 14 '16 at 2:39
  • $shippingCode = $product->getData('shipping_code');? – Ankit Shah Dec 14 '16 at 3:00
  • Didn't get to you – Ankit Shah Dec 14 '16 at 3:05
  • Just figured that out about the same time as you :) Update your answer and will mark as correct you legend. – Nathaniel Meyer Dec 14 '16 at 3:05
5
$_product = $block->getProduct();

$myattribute = $_product->getResource()->getAttribute('Your_Attribute_Code')->getFrontend()->getValue($_product);

echo $myattribute;

if want to show value of custom attribute in product page can use this code.

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.