1

How to get product value ( product price) if attribute "logitech" is set to yes [attribute is Boolean].

I'm new here, still learning magneto. below code, calculate all items in the cart but i attribute logitech product to be excluded from getting discount

class Discount extends Calculation
{
    /**
     * @param Quote $quote
     * @param ShippingAssignmentInterface $shippingAssignment
     * @param Total $total
     *
     * @return $this|Calculation
     * @throws LocalizedException
     * @throws Zend_Serializer_Exception
     */
    public function collect(
        Quote $quote,
        ShippingAssignmentInterface $shippingAssignment,
        Total $total
    ) {
        parent::collect($quote, $shippingAssignment, $total);
        $items = $shippingAssignment->getItems();

        if (!$items || !$this->canCalculate($quote->getStoreId(), true) || $quote->getIsMultiShipping()) {
            $this->resetAffiliate($quote);

            return $this;
        }

        $itemFields = ['affiliate_discount_amount', 'base_affiliate_discount_amount'];
        $this->resetAffiliateData(
            $items,
            $quote,
            array_merge($itemFields, ['affiliate_discount_shipping_amount', 'affiliate_base_discount_shipping_amount']),
            $itemFields
        );
        $account            = $this->registry->registry('mp_affiliate_account');
        $isBreakCampaign    = false;
        $affiliateKey       = $this->getAffiliateKey(); // if no cookie then first order key
        $affSource          = $this->getAffiliateSourceFromCookie(self::AFFILIATE_COOKIE_SOURCE_NAME);
        $campaignCouponCode = null;

        if ($affSource === 'coupon') {
            $affCodeWithPrefix = explode('-', $affiliateKey);
            if (count($affCodeWithPrefix) === 2) {
                $campaignCouponCode = $affCodeWithPrefix[1];
            } else {
                return $this;
            }
        }

        foreach ($this->getAvailableCampaign($account, $campaignCouponCode) as $campaign) {
            $isCalculateTax      = $campaign->getApplyDiscountOnTax();
            $isCalculateShipping = $campaign->getApplyToShipping();
            $totalCart           = $this->getTotalOnCart($items, $quote, $isCalculateShipping, $isCalculateTax, false);

            if ($totalCart getDiscountOnCampaign($campaign, $totalCart);
            if ($quote->getBaseAffiliateDiscountAmount() + $totalDiscount > $totalCart) {
                $totalDiscount   = $totalCart - $quote->getBaseAffiliateDiscountAmount();
                $isBreakCampaign = true;
            }
            $baseDiscount = $discount = 0;

            if ($totalDiscount) {
                $lastItem = null;
                foreach ($items as $item) {
                    if ($item->getParentItem()) {
                        continue;
                    }

                    if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                        /** @var Item $child */
                        foreach ($item->getChildren() as $child) {
                            $this->calculateItemDiscount(
                                $child,
                                $totalCart,
                                $totalDiscount,
                                $baseDiscount,
                                $discount,
                                $isCalculateTax,
                                $lastItem
                            );
                        }
                    } else {
                        $this->calculateItemDiscount(
                            $item,
                            $totalCart,
                            $totalDiscount,
                            $baseDiscount,
                            $discount,
                            $isCalculateTax,
                            $lastItem
                        );
                    }
                }

                if ($campaign->getApplyToShipping()) {
                    $shippingBaseDiscount = $totalDiscount - $baseDiscount;
                    $shippingDiscount     = $this->priceCurrency->convert($shippingBaseDiscount);
                    $quote->setAffiliateDiscountShippingAmount(
                        $quote->getAffiliateDiscountShippingAmount() + $shippingDiscount
                    );
                    $quote->setBaseAffiliateDiscountShippingAmount(
                        $shippingBaseDiscount
                    );
                    $baseDiscount += $shippingBaseDiscount;
                    $discount     += $shippingDiscount;
                } elseif ($lastItem && $totalDiscount > $baseDiscount) {
                    $lastItemBaseDiscount = $totalDiscount - $baseDiscount;
                    $lastItemDiscount     = $this->priceCurrency->convert($lastItemBaseDiscount);
                    $lastItem->setBaseAffiliateDiscountAmount(
                        $item->getBaseAffiliateDiscountAmount() + $lastItemBaseDiscount
                    )->setAffiliateDiscountAmount($item->getAffiliateDiscountAmount() + $lastItemDiscount);
                    $baseDiscount += $lastItemBaseDiscount;
                    $discount     += $lastItemDiscount;
                }

                $quote->setAffiliateDiscountAmount($quote->getAffiliateDiscountAmount() + $discount);
                $quote->setBaseAffiliateDiscountAmount($quote->getBaseAffiliateDiscountAmount() + $baseDiscount);
                $baseGrandTotal = $total->getBaseGrandTotal() - $baseDiscount;
                $grandTotal     = $total->getGrandTotal() - $discount;
                if ($grandTotal setAffiliateKey($this->getAffiliateKeyFromCookie());
                $total->setBaseGrandTotal($baseGrandTotal);
                $total->setGrandTotal($grandTotal);
                $quote->setBaseGrandTotal($baseGrandTotal);
                $quote->setGrandTotal($grandTotal);
                $quote->save();

                if ($isBreakCampaign) {
                    break;
                }
            }
        }

        return $this;
    }

    /**
     * @param $item
     * @param $total
     * @param $totalDiscount
     * @param $baseDiscount
     * @param $discount
     * @param $isCalculateTax
     * @param $lastItem
     */
    public function calculateItemDiscount(
        $item,
        $total,
        $totalDiscount,
        &$baseDiscount,
        &$discount,
        $isCalculateTax,
        &$lastItem
    ) {
        $itemBaseDiscount = ($this->getItemTotalForDiscount($item, $isCalculateTax, false) / $total) * $totalDiscount;
        $itemBaseDiscount = $this->round($itemBaseDiscount, 'base');
        $itemDiscount     = $this->convertPrice($itemBaseDiscount, false, false, $item->getStoreId());
        $itemDiscount     = $this->round($itemDiscount);
        $item->setBaseAffiliateDiscountAmount($item->getBaseAffiliateDiscountAmount() + $itemBaseDiscount)
            ->setAffiliateDiscountAmount($item->getAffiliateDiscountAmount() + $itemDiscount);
        $baseDiscount += $itemBaseDiscount;
        $discount     += $itemDiscount;
        $lastItem     = $item;

    }
}
2

2 Answers 2

0
<?php

    namespace Test\Module\Helper;

    use Magento\Framework\App\Helper\AbstractHelper;

    class Product extends AbstractHelper
     {
          protected $productRepository; 

          public function __construct(
            ProductRepositoryInterface $productRepository
          ) {
              $this->productRepository = $productRepository;
          }

          public function getProduct($productId=1)
          {
              $product = $this->productRepository->getById($productId);
              $logitech = $product->getResource()->getAttribute('logitech')->getFrontend()->getValue($product);
              if($logitech){
                    return $product;
              }
          }
      }
1
  • this code worked but please help with above problem
    – Bayzel
    Jun 14 at 8:53
0

I think the question is still not clear, but i want to suggest one thing about processing product attributes in cart. Boolean value should be stored as 1 or 0/null.

If you want to load your attribute for quote you may create following rule

app/code/Acme/StackExchange/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="logitech"/>
    </group>
</config>

After this you can use this attribute with Quote Item instance. Example:

    // example with iterate quote
    foreach ($quote->getAllVisibleItems() as $quoteItem) {
        $product = $quoteItem->getProduct();
        if ($product->getData('logitech') === '1') {
            // custom logic here
        }
    }
1
  • i have edited my question sorry for that and thank you for the suggestion
    – Bayzel
    Jun 14 at 9:38

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.