Free Product with Purchase

Discussion in 'Customization & add-ons' started by cpaprotna, Oct 11, 2017.

  1. cpaprotna

    cpaprotna Member

    Joined:
    Dec 23, 2013
    Messages:
    44
    I have products of specific categories set up. One is a main Membership category and the others have a category of Designation. When someone is on a membership form and they are purchasing a Membership product, they need to be able to purchase 1 or more Designation products. The First designation product they pick should be free, the others are all billed regular price. Is there a way to set this up without the user having to enter a coupon code?
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Hello,

    I am afraid it is not possible to do by default but we can implement custom plugin for you that add such feature.
    Please contact us in helpdesk for details:
    https://www.amember.com/support

    Best Regards.
  3. cpaprotna

    cpaprotna Member

    Joined:
    Dec 23, 2013
    Messages:
    44
    Thank you, I was able to create a calculator that did what I need. But my site also uses a coupon code. Is there a way to ensure that my calculator runs AFTER the calculator for the coupon code?
  4. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    How did you add your calculator? What is code you use?
  5. cpaprotna

    cpaprotna Member

    Joined:
    Dec 23, 2013
    Messages:
    44
    I added the code to the site.php file and found my mistake, I was eliminating the coupon discount before looking to see if it exists.

    This calculator checks the current date and if it's October - December, discounts the main membership item by 50%. It checks if there is already a coupon discount (which would be $25% off the membership) and then calculates 50% off that total before applying the end of year discount to the first charge.

    It's important to know that for this website, all memberships go from 4/1 - 4/1. No matter what day they purchase their membership, it has to expire on 4/1 of the next year. This is why they get a 50% discount after October - The date is from the current date to 4/1 of the next year. If they purchase between January 1 and April 1, it goes from 01/01 of the current year until 4/1 of the next year (so they pay full price but get some extra months).

    I have several other hooks in site.php that will update the access record dates and the invoice rebill date.

    It then discounts the first Designation membership item 100%.


    PHP:
    Am_Di::getInstance()->hook->add('invoiceGetCalculators''FSGAinvoiceGetCalculators');

    function 
    FSGAinvoiceGetCalculators(Am_Event $e) {
        
    $vars $e->getVars();
        
    $calculators $e->getReturn();
        
    $calculators[] = new Am_Invoice_Calc_MemberDisc($this->vars);   
        
    $e->setReturn($calculators);

    }

    class 
    Am_Invoice_Calc_MemberDisc extends Am_Invoice_Calc
    {
        
    /** @var Coupon */
        
    protected $coupon;
        protected 
    $user;
        protected 
    $membership_count;
        protected 
    $designation_count;   
        protected 
    $designation_prods;
        
    //Product Category
        //  Designation = 3
        //  Membership = 1
        //  Membership Individual = 6
        //  Membership Organizational = 7
       
        
    public function productInCategory($category_id$product_id){
            
    $rows AM_di::getInstance()->db->query("SELECT product_id FROM ?_product_product_category
                WHERE product_category_id = ? and product_id = ?"
    $category_id$product_id);
            return !empty(
    $rows);
        }
        public function 
    calculate(Invoice $invoiceBill)
        {
            
    $this->membership_count $this->designation_count 0;
            
    $this->user $invoiceBill->getUser();
            
    $isFirstPayment $invoiceBill->isFirstPayment();
            
    $first_discountable $second_discountable = array();
            foreach (
    $invoiceBill->getItems() as $item) {
                
    $item->_calculateTotal();
                
    $month date("n");
                
    $eoy_discount false;
                if (
    $month >= 10) {$eoy_discount true;}
                if (
    $eoy_discount) {
                    if (
    $this->productInCategory(1$item->item_id)) {               
                        
    $itemprice $item->first_price;
                        
    $currentdiscount $item->first_discount;
                        
    $currentprice $itemprice $currentdiscount;
                        
    $endprice $currentprice .5;
                        
    $memberdiscount $itemprice $endprice;
                        
    $item->first_discount $memberdiscount;
                        
    $this->membership_count += 1;
        
    Am_Di::getInstance()->errorLogTable->log("price: ".$itemprice."Current Discount: ".$currentdiscount." Current Price: ".$currentprice." End Price: ".$endprice." Discount: ".$item->first_discount);               
                    }
                }
                    
                if (
    $this->productInCategory(3$item->item_id)) {
                    
    $this->designation_count += 1;
                    
    $first_discountable[] = $item;
                    
    $second_discountable[] = $item;
                }
            }               
            
    $discount_avail $this->membership_count;
            if (!empty(
    $first_discountable[1])) {
                if (
    $discount_avail >= 1) {
                    
    $item $first_discountable[1];
                    
    $discount $item->first_price;
                    if (
    $discount_avail == 1) {
                        
    $discount_avail 0;                       
                    }
                    
    $item->first_discount $discount;
                    
    $item->second_discount $discount;
                }
            }
           
            foreach (
    $invoiceBill->getItems() as $item) {
                
    $item->_calculateTotal();
            }
        }
    }
  6. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    According your code snippet your calculator always run after calculator for coupon code.

    Best Regards.
  7. cpaprotna

    cpaprotna Member

    Joined:
    Dec 23, 2013
    Messages:
    44
    Yes, I found the error in my code. This line:
    Code:
    $this->membership_count = $this->designation_count = 0;
    was zeroing out the coupon discount. Once I removed that line of code it did what I needed it to do.

    Thanks for your help.

Share This Page