How to get error message if ask QTY higher than the QTY in stock. like IsSalableWithReservationsCondition.php file that echo the "The requested qty is not available" how to use the same logic but use the value in the attribute I created as a quantity of that product selected and if ask QTY higher than the value in that attribute echo this message "The requested qty is not available"
2 Answers
Your issue is related to incorrect logic of extend original classes.
Your are trying to copy paste target (\Magento\Catalog\Block\Product\View\Attributes
) class and extend from the same target class (\Magento\Catalog\Block\Product\View\Attributes
) instead of it parent.
I don't know and see what is reason to overwrite this class and fully extend it.
To solve your issue you can change extends
to \Magento\Framework\View\Element\Template
or remove __construct
method.
-
please if you can explain the logic further on how to extend class if you can. or send thread links where i can learn more about extend classes, thank you– BayzelCommented Aug 4, 2022 at 13:57
-
If you can specify your goal I can help you with correct solution Commented Aug 4, 2022 at 15:11
-
Alright victor i want to overwrite that file of attributes and extend it class and add some few lines of code into it. hope that clear. those lines of code i want to add are for error message which show if qty requested exceed stock but i want to use attribute file to get value from attribute i created that have values into them and echo that value in screen and say " the requested QTY of (product name) exceed (the value in that attribute) in stock". that what i want to do. i can change my question about so you better under clearly– BayzelCommented Aug 5, 2022 at 5:49
-
Feel free to update your question with more detail. It still not clear your goal. I guess you chose bad place to modify your logic. The file which you are try to override related to render attributes on frontend PDP page and not related to stock messages. For access to any product attribute you don't need to override anything. Asking the right question is recipe for success answer. Commented Aug 5, 2022 at 8:23
-
Alright victor let me ask right question , will edit my question above– BayzelCommented Aug 5, 2022 at 11:46
Update your Attributes.php
with
<?php
namespace extendAttribute\showAttribute\Block\Product\View;
use Magento\Catalog\Block\Product\View\Attributes as ExtendAttribute;
use Magento\Catalog\Model\Product;
use Magento\Framework\Phrase;
use Magento\Framework\Pricing\PriceCurrencyInterface;
/**
* Attributes attributes block
*
* @api
* @since 100.0.2
**/
class Attributes extends ExtendAttribute
{
/**
* @var Product
*/
protected $_product = null;
/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry = null;
/**
* @var PriceCurrencyInterface
*/
protected $priceCurrency;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param PriceCurrencyInterface $priceCurrency
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Registry $registry,
PriceCurrencyInterface $priceCurrency,
array $data = []
) {
$this->priceCurrency = $priceCurrency;
$this->_coreRegistry = $registry;
parent::__construct($context, $registry, $priceCurrency, $data);
}
/**
* Returns a Product
*
* @return Product
*/
public function getProduct()
{
if (!$this->_product) {
$this->_product = $this->_coreRegistry->registry('product');
}
return $this->_product;
}
/**
* $excludeAttr is optional array of attribute codes to exclude them from additional data array
*
* @param array $excludeAttr
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAdditionalData(array $excludeAttr = [])
{
$data = [];
$product = $this->getProduct();
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
if ($this->isVisibleOnFrontend($attribute, $excludeAttr)) {
$value = $attribute->getFrontend()->getValue($product);
if ($value instanceof Phrase) {
$value = (string)$value;
} elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
$value = $this->priceCurrency->convertAndFormat($value);
}
if (is_string($value) && strlen(trim($value))) {
$data[$attribute->getAttributeCode()] = [
'label' => $attribute->getStoreLabel(),
'value' => $value,
'code' => $attribute->getAttributeCode(),
];
};
}
}
return $data;
}
/**
* Determine if we should display the attribute on the front-end
*
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* @param array $excludeAttr
* @return bool
* @since 103.0.0
*/
protected function isVisibleOnFrontend(
\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute,
array $excludeAttr
) {
return ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr));
}
}
Hope this works for you!