Product Attribute is used to display in Promo Rules for Shopping Cart Discount. It’s based on you, whether attribute wants to display on Promo rule or not.
You can manage/check it from the Manage Attribute section of the Backend of Magento manually but from programmatic way, you need to create additional logic for it.
is_used_for_promo_rules identifies to specific attribute will be used for the Promo discount on the Cart or Checkout page.
<?php
namespace Jesadiya\UsedPromo\Model;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
class UsedPromoRules
{
/**
* @var ProductAttributeRepositoryInterface
*/
private $productAttributeRepository;
public function __construct(
ProductAttributeRepositoryInterface $productAttributeRepository
) {
$this->productAttributeRepository = $productAttributeRepository;
}
/**
* Check Attribute used for promo rules
*
* @param int $id
* @return bool
* @throws NoSuchEntityException
*/
public function isUsedForPromo(int $id)
{
$attribute = $this->attributeRepository->get($id);
$isUsedForPromo = false;
if ($attribute) {
$isUsedForPromo = $attribute->getIsUsedForPromoRules();
}
return $isUsedForPromo;
}
}
Call method from the PHP Class,
$attributeId = 144; //size attribute echo $checkUsedForPromo = $this->isUsedForPromo($attributeId);
Based on the above code, You can verify the given attribute is used for the discount of the cart or not.
Output:
true/false
