1

I'm using CartThrob to sell tickets. I create a channel entry for each day and then use options for Adult, Child and Student tickets. I'm trying to write a function for an extension that would give the purchaser 2 free Child tickets whenever they purchase an Adult ticket.

The function I tried to write is as follows;

function cartthrob_reduce_tour_inventory()
{
    foreach ($this->EE->cartthrob->cart->items() as $item)
    {
        if($item->item_options('ticket_type') == "Adult")
        {
            //add two child tickets to cart
            $item->item_options('ticket_type') == "Child")->set_quantity() * 2);
            //set the price to free
            $item->set_price(0);
        }
    }

    return; 
}

This is the first time I have tried to write a function here, so please excuse what is probably nowhere near what it needs to be. I would be grateful if someone might be able to help? :)

Thank you.

Here's the code in full for the extension:

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cartthrob_tour_group_pricing_ext
{
    public $settings = array();
    public $name = 'CartThrob Tour Group Pricing';
    public $version = '1.0.1';
    public $description = 'Reduces child inventory by 2 when an adult ticket is selected. Handles flat rate for 10 or more tickets.';
    public $settings_exist = 'y';
    public $docs_url = 'http://barrettnewton.com';

    protected $EE;

    public function __construct($settings = '')
    {
    $this->EE =& get_instance();
    $this->settings = $settings;
    }


    public function activate_extension()
    {

        $this->EE->db->insert(
            'extensions',
            array(
            'class' => __CLASS__,
            'method' => 'cartthrob_reduce_inventory',
            'hook'  => 'cartthrob_on_authorize',
            'settings' => serialize($this->settings),
            'priority' => 10,
            'version' => $this->version,
            'enabled' => 'y'
            )
        );          
        $this->EE->db->insert(
            'extensions',
            array(
            'class' => __CLASS__,
            'method' => 'cartthrob_set_group_prices',
            'hook'  => 'cartthrob_add_to_cart_end',
            'settings' => serialize($this->settings),
            'priority' => 10,
            'version' => $this->version,
            'enabled' => 'y'
            )
        );
        $this->EE->db->insert(
            'extensions',
            array(
            'class' => __CLASS__,
            'method' => 'cartthrob_set_group_prices',
            'hook'  => 'cartthrob_multi_add_to_cart_end',
            'settings' => serialize($this->settings),
            'priority' => 10,
            'version' => $this->version,
            'enabled' => 'y'
            )
        );
        $this->EE->db->insert(
            'extensions',
            array(
            'class' => __CLASS__,
            'method' => 'cartthrob_set_group_prices',
            'hook'  => 'cartthrob_update_cart_end',
            'settings' => serialize($this->settings),
            'priority' => 10,
            'version' => $this->version,
            'enabled' => 'y'
            )
        );
    }
    public function update_extension($current='')
    {
        if ($current == '' OR $current == $this->version)
        {
            return FALSE;
        }

        $this->EE->db->update(
            'extensions',
            array('version' => $this->version),
            array('class' => __CLASS__)
        );
    }
    public function disable_extension()
    {
    $this->EE->db->delete('extensions', array('class' => __CLASS__));
    }


    function settings()
    {
        $settings = array();

        $settings['quota']                  = array('i', '', "10");
        $settings['adult_group_rate']       = array('i', '', "9.00");
        $settings['child_group_rate']       = array('i', '', "3.60");
        $settings['student_group_rate']     = array('i', '', "7.20");

        return $settings;
    }

    function cartthrob_reduce_tour_inventory()
    {
        foreach ($this->EE->cartthrob->cart->items() as $item)
        {
            if($item->item_options('tour_pass_type') == "Adult")
            {
                //add two child tickets to cart
                $item->item_options('tour_pass_type') == "Child")->set_quantity() * 2);
                //set the price to free
                $item->item_options('tour_pass_type') == "Child")->set_price(0);
            }
        }

        return; 
    }
    function cartthrob_set_tour_group_prices()
    {
        $ticket_count = array();
        foreach($this->EE->cartthrob->cart->items() as $item)
        {
            if($item->item_options('tour_pass_time'))
            {
                $ticket_date = $item->title();
                $ticket_time = $item->item_options('tour_pass_time');
                if(!isset($ticket_count[$ticket_date.$ticket_time]))
                {
                    $ticket_count[$ticket_date.$ticket_time]['count'] = 0;
                    $ticket_count[$ticket_date.$ticket_time]['items'] = array();
                }
                // need to keep the original price stored somewhere in case we need to revert back to the non-group rate
                if(!$item->meta('original_price'))
                {
                    $item->set_meta('original_price', $item->price());
                }
                $ticket_count[$ticket_date.$ticket_time]['count'] += $item->quantity();
                $ticket_count[$ticket_date.$ticket_time]['items'][] = $item;
            }
        }
        // check the number of tickets being purchased
        foreach($ticket_count as $ticket)
        {
            // is the number of tickets being purchased equal to or greater than the quota?
            if($ticket['count'] >= $this->settings['quota'])
            {
                // evaluate each ticket item
                foreach($ticket['items'] as $ticket_item)
                {
                    //if statement here to set price per ticket type


                    // set the price of each item to appropriate rate
                    $ticket_item->set_price($this->settings[$ticket_item->meta('ticket_peak_rate')]);
                }
            }
            else
            {
                foreach($ticket['items'] as $ticket_item)
                {
                    // set the price of each item to the original price
                    $ticket_item->set_price($ticket_item->meta('original_price'));
                }
            }
        }
        return;
    }
    // END
}
//END CLASS
7
  • Can you explain a bit more about what's not working with your function? We can't help if we don't know what the problem is :) Feb 23, 2013 at 16:30
  • Hey Adrian. It's not working at all. :) This is my first attempt at writing a function to do the task I wish to accomplish, so i'm really looking for someone to tell me if i'm on the right path with it. Feb 23, 2013 at 17:28
  • Can you post a link to your full extension code? Are you sure the extension is registered correctly? (e.g. does your code run at all?) Feb 24, 2013 at 5:53
  • Hey Adrian. I've added the full code above. The extension won't install properly and I think it's to do with the cartthrob_reduce_tour_inventory function. The extensions page just goes blank. :/ Feb 24, 2013 at 13:35
  • If you're getting a white screen, it's usually a PHP error which isn't being displayed. Try setting $debug = 1; in your main EE index.php and admin.php files. Feb 25, 2013 at 1:30

0

Your Answer

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

Browse other questions tagged or ask your own question.