Question

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;
}
Was it helpful?

Solution

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.

OTHER TIPS

$_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.

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top