2

I have written custom wishlist which will display its items when customer logins. If customer adds product other than config products, my wishlist is working fine, but when customer adds configurable product, I am getting error.

Fatal error: Call to undefined method Magento\Catalog\Model\Product\Type\Virtual::getConfigurableAttributes() in C:\xampp\htdocs\cw1\campingworld2\vendor\magento\module-configurable-product\Model\Plugin\AfterProductLoad.php on line 66

I checked with this file, this line is getting Configurable product attributes. My code is

<span><?php echo $block->escapeHtml($product->getSku()); ?></span>
<?php
//  $priceBlock =  $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
//  getting $product object, passed $product object inside function
//  echo $priceBlock->getProductPrice($product);

$priceHelper = $objectManager->get('Magento\Framework\Pricing\Helper\Data'); // Instance of Pricing Helper
//$finalPrice = $block->getItemPrice($product->getFinalPrice());
$finalPrice = $priceHelper->currency($product->getFinalPrice(), true, false);
$oldPrice = $priceHelper->currency($product->getPrice(), true, false);
echo __("SALE ");
echo $block->escapeHtml($finalPrice);
?>
<br>
<?php if ($product->getFinalPrice() < $product->getPrice()): ?>
    <span class="price-off">
        <span>
            <?php $oldPrice = $priceHelper->currency($product->getPrice(), true, false);
            echo $block->escapeHtml($oldPrice);
            ?>
        </span>
        <?php if ($finalPrice && $oldPrice): ?>
            <?php echo __("SAVE ") ?>
            <?php $priceOff = $block->getPriceOff($product->getPrice(), $product->getFinalPrice());
            $priceOff = $block->escapeHtml($priceHelper->currency($priceOff, true, false));
            echo substr($priceOff, 1);
            echo __("%") ?>
        <?php endif; ?>
    </span>
<?php endif; ?>

I debug it and found, If i will use $Product->getSku();

I am getting error, but if used $Product->getData('sku'); its working. Please tell me how to get configurable product data.

1
  • anyone getting idea about this error. May 19 '16 at 8:18
1

I solved it; I was invoking product model for getting item in wishlist to my own custom module. my block:

public function getItemProduct($id){
    $this->_coreRegistry->unregister('product');
    $product = $this->getProductModel()->load($id)->setFinalPrice(null);
    $this->_coreRegistry->register ( 'product',$product);
    return $this->getProduct();
}

I have used setFinalPrice(null) since in wishlist for every product, first product which was added to wishlist, its price is displaying only for every product.
For example there are 3 products in wishlist as
P1: $40 P2: $60 P3: $80 but in wishlist for all product price will be $40.
I am calling this block method(getItemProduct()) in my template and was passing product id fetched from wishlist item collection to it as argument;

<?php foreach ($block->getWishlistItemCollection() as $item): ?>
  <?php $product = $block->getItemProduct($item->getProductId()); ?>
   <span><?php echo $block->escapeHtml($product->getSku()) ?></span>
<?php endforeach; ?>

now when i was adding simple or other product type first and then config product, to wishlist I was getting error. Then I debug it and use this code direct in my template file:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
<?php foreach ($block->getWishlistItemCollection() as $item): ?>
<?php
  $productModel = $objectManager->create('Magento\Catalog\Model\Product');
  $product = $productModel->load($item->getProductId()); ?>
  <span><?php echo $block->escapeHtml($product->getSku()) ?></span>

now it works. But now also have some some doubts.

1
  • Don't use object manager. It is not good Practice. Try to inject Magento\Catalog\Model\Product in your constructor Jun 28 '17 at 17:47

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.