Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method to check if there are ONLY Gift Vouchers in the cart #75

Open
xenowebdev opened this issue Oct 17, 2021 · 0 comments
Open

Method to check if there are ONLY Gift Vouchers in the cart #75

xenowebdev opened this issue Oct 17, 2021 · 0 comments

Comments

@xenowebdev
Copy link

What are you trying to do?
I have a scenario where there could be just gift vouchers in a cart. At present the user still has to enter a shipping address, which is not needed as the gift voucher is a download. I want to be able to bypass the user entering a shipping address so I need a way to check if the cart has ONLY gift vouchers.

What's your proposed solution?

As per your suggestion on Discord:

public function hasGiftVouchers($hasOnly = false, $cart = null)
{
    $giftVouchers = [];

    if (!$cart) {
        $cart = Commerce::getInstance()->getCarts()->getCart();
    }

    if (!$cart->lineItems) {
        return false;
    }

    foreach ($cart->lineItems as $lineItem) {
        $isGiftVoucher = (bool)(get_class($lineItem->purchasable) === Voucher::class);

        if ($isGiftVoucher) {
            $giftVouchers[] = $lineItem;
        }
    }

    if ($hasOnly) {
        return (count($cart->lineItems) === count($giftVouchers)) ? true : false;
    } else {
        return count($giftVouchers) ? true : false;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant