7

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't 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;
}
5
  • 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
    – Jackson
    Commented Dec 14, 2016 at 1:39
  • Thanks, this was off. Unfortunately, this hasn't fixed the problem none of the ways above are getting the value. Commented Dec 14, 2016 at 1:46
  • Please paste your file code with path from magento root directory
    – Jackson
    Commented Dec 14, 2016 at 1:49
  • Updated post with file and directory Commented Dec 14, 2016 at 1:53
  • Hi @Nathaniel Have u tried answer?
    – Jackson
    Commented Dec 14, 2016 at 2:28

3 Answers 3

5
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.

16
  • Just gave this a go with no luck sorry :( Commented Dec 14, 2016 at 2:35
  • Ahh strage. Please make all to Yes on Admin for "Storefront Properties" & Check. Have u cleared cache?
    – Jackson
    Commented Dec 14, 2016 at 2:39
  • $shippingCode = $product->getData('shipping_code');?
    – Jackson
    Commented Dec 14, 2016 at 3:00
  • Didn't get to you
    – Jackson
    Commented Dec 14, 2016 at 3:05
  • Just figured that out about the same time as you :) Update your answer and will mark as correct you legend. Commented Dec 14, 2016 at 3:05
11
$_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.

3

The accepted answer uses the ObjectManager directly, which the author themself noted is an anti-pattern. The current best practice for this is to use DI to get the product resource model and use that rather than calling getResource() on the product model instance which is deprecated since 2.2.

@deprecated 101.0.0 because resource models should be used directly

So an example implementation might look something like this:

class CustomShipping ...
{
    protected $productResource;

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

    public function collectRates(RateRequest $request)
    {
        ...
        $attributeValue = $this->productResource->getAttribute('shipping_code')->getFrontend()->getValue($product);
        ...
    }
}

Also, another important thing to check is whether the attribute is being loaded for the quote_item context. Given that this seems to be a custom attribute, most likely not. So to make sure your attribute gets loaded in quote items, you will need to define it on catalog_attributes like this:

Magento/App/Code/module_folder/custom_shipping/etc/catalog_attributes.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="quote_item">
        <attribute name="shipping_code"/>
    </group>
</config>

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.