0

Here is My Block.php file

app/code/Smartwave/Porto/Block/Catalog/Product/View/AddTo/Wishlist.php

namespace Smartwave\Porto\Block\Catalog\Product\View\AddTo;

/**
 * Product view wishlist block
 *
 * @api
 * @since 100.1.1
 */
class Wishlist extends \Magento\Catalog\Block\Product\View
{
    protected $registry;

    public function __construct(  
        \Magento\Framework\Registry $registry

    )
    {      
        $this->registry = $registry;

    }

 .
 .
 .
 .
 .


    public function getProductIdWishlist(){

         $currentProduct=$this->registry->registry('current_product');
       return $currentProduct->getCurrentProduct(); 
    }
}

my .phtml file

app/design/frontend/Smartwave/porto/Magento_Wishlist/templates/catalog/product/view/addto/wishlist.


<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var \Magento\Wishlist\Block\Catalog\Product\View\AddTo\Wishlist $block */
?>
<?php if ($block->isWishListAllowed()) : ?>
    <a href="javascript:void(0)"
       class="action towishlist"
       data-pdtId="<?php echo $block->getProductIdWishlist();?>"
       data-action="add-to-wishlist"><span><?= $block->escapeHtml(__('Add to Wish List')) ?></span></a>
<?php endif; ?>
<script type="text/x-magento-init">
    {
        "body": {
            "addToWishlist": <?= /* @noEscape */ $block->getWishlistOptionsJson() ?>
        }
    }
</script>

i need to print current product id inside 'data-pdtId' tag there is any way to print id without using object manager ,

1

You return wrong value in your block function. Use this below code in your function :

public function getProductIdWishlist(){
    $currentProduct = $this->registry->registry('current_product');
    return $currentProduct->getId(); 
}

Or try this below code also :

public function getProductIdWishlist(){
    $currentProduct = $this->registry->registry('product');
    return $currentProduct->getId(); 
}

If you extends \Magento\Catalog\Block\Product\View class in your block. Then, you can also use this below code :

$this->getProduct()->getId();

Override block file :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Wishlist\Block\Catalog\Product\View\AddTo\Wishlist" type="VendorName\ModuleName\Block\Catalog\Product\View\AddTo\Wishlist"/>
</config>
  • already try this way , but it returns empty – Shafeel Sha Jul 25 '19 at 10:43
  • Try to pass $currentProduct->getName(); You get value or not? – Rohan Hapani Jul 25 '19 at 10:44
  • @ShafeelSha Check my updated answer. – Rohan Hapani Jul 25 '19 at 10:47
  • nothing happen ! – Shafeel Sha Jul 25 '19 at 10:49
  • Did you used my updated answer? – Rohan Hapani Jul 25 '19 at 10:51
0

In your block file use get Id ,

namespace Smartwave\Porto\Block\Catalog\Product\View\AddTo;

class Wishlist extends \Magento\Catalog\Block\Product\View
{
    protected $registry;

    public function __construct(  
        \Magento\Framework\Registry $registry
    ) {      
        $this->registry = $registry;
    }

    public function getProductIdWishlist(){

       $currentProduct=$this->registry->registry('current_product');
       return $currentProduct->getId(); 
    }
}
0

You can get product id like this

$product = $this->registry->registry('current_product');//get current product                    
$productId = $product->getId();

You're extending \Magento\Catalog\Block\Product\View you can get product_id like this

$product = $this->getProduct();
return $product->getId();

or

$this->getProductId();

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.