1

A solution I think it is to create a new customers group from

Stores -> Customer Groups

and create a cart price rule to this group only.

When you create a new Group you have to select a tax class. But there is only one in list: "retail customer".

How can I add a new tax class, that I have to select and that puts the customers automatically in this group based if the number of orders placed is 0 or not?

Or any other solution for creating coupons for those who didn't place any orders

1
  • you mean new customer only able to get discount with coupon code ?
    – Yogesh
    Oct 31, 2017 at 13:43

1 Answer 1

0

I think the best solution is to check if a customer has made any order.

So in your case try to load all the customer that you have like this ;

You need to inject \Magento\Customer\Model\Customer class to get customer collection

<?php

    protected $_customers;

    public function __construct(
        ...
        \Magento\Customer\Model\Customer $customers
        ...
    ) {
        ...
        $this->_customers = $customers;
        ...
    }

    public function getCollection()
    {
        //Get customer collection
        return $this->_customers->getCollection();
    }

    public function getCustomer($customerId)
    {
        //Get customer by customerID
        return $this->_customers->load($customerId);
    }

}

Now you can get customer collection or customer by customerID in phtml file

<?php

//Get customer collection
$collection = $this->getCollection();
foreach ($collection as $customer) {
    echo $customer->getEmail(); // Get Customer Email
}


//Get customer by ID
$customerId = 1;
$customer = $this->getCustomer($customerId);
echo $customer->getEmail(); // Get customer Email

?>

and then check if they have any order

This will check the first order of the customer. If the customer dosen't have a "first order' this means that he/she didn't order anything yet.

<?php

namespace Namespace\Module\Block;

class Test extends \Magento\Framework\View\Element\Template
{
    protected $orderFactory;

    public function __construct(
        \Magento\Sales\Model\OrderFactory $orderFactory
    ) {
        $this->orderFactory = $orderFactory;
    }

    public function getFirstOrder($emailId){
        $emailId = '[email protected]';

        $order = $this->orderFactory->create()->getCollection()->addAttributeToSelect('customer_email')
                ->addFieldToFilter('customer_email',array('eq'=>$emailId))->getFirstItem();

        if($order->getCustomerEmail()){
            echo 'second time order';
        }
        else{
            echo 'first time order';
        }

    }

So you need to load all the customer and then make a foreach and check for each if they have first order or not. I hope this will help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.