Created
August 6, 2013 10:52
-
-
Save ijy/6163534 to your computer and use it in GitHub Desktop.
For Expresso Store v1. Add a $10 discount for every item in the cart which for three or more items. In Store v2 this is now included natively.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
| /** | |
| * Store Discount Extension | |
| * | |
| * @package ExpressionEngine | |
| * @subpackage Addons | |
| * @category Extension | |
| * @author Ian Young | |
| * @link http:// | |
| */ | |
| class Store_discount_ext { | |
| public $settings = array(); | |
| public $description = 'Apply bath quantity-based discounts to Exp:resso Store'; | |
| public $docs_url = 'http://'; | |
| public $name = 'Store Batch Discount'; | |
| public $settings_exist = 'n'; | |
| public $version = '1.0'; | |
| private $EE; | |
| /** | |
| * Constructor | |
| * | |
| * @param mixed Settings array or empty string if none exist. | |
| */ | |
| public function __construct($settings = '') | |
| { | |
| $this->EE =& get_instance(); | |
| $this->settings = $settings; | |
| } | |
| // ---------------------------------------------------------------------- | |
| /** | |
| * Activate Extension | |
| * | |
| * This function enters the extension into the exp_extensions table | |
| * | |
| * @see http://codeigniter.com/user_guide/database/index.html for | |
| * more information on the db class. | |
| * | |
| * @return void | |
| */ | |
| public function activate_extension() | |
| { | |
| // Setup custom settings in this array. | |
| $this->settings = array(); | |
| // Add hooks | |
| $data = array( | |
| 'class' => __CLASS__, | |
| 'method' => 'update_cart', | |
| 'hook' => 'store_cart_update_end', | |
| 'settings' => serialize($this->settings), | |
| 'priority' => 10, | |
| 'version' => $this->version, | |
| 'enabled' => 'y' | |
| ); | |
| $this->EE->db->insert('extensions', $data); | |
| } | |
| // ---------------------------------------------------------------------- | |
| /** | |
| * Disable Extension | |
| * | |
| * This method removes information from the exp_extensions table | |
| * | |
| * @return void | |
| */ | |
| function disable_extension() | |
| { | |
| $this->EE->db->where('class', __CLASS__); | |
| $this->EE->db->delete('extensions'); | |
| } | |
| // ---------------------------------------------------------------------- | |
| /** | |
| * Update Extension | |
| * | |
| * This function performs any necessary db updates when the extension | |
| * page is visited | |
| * | |
| * @return mixed void on update / false if none | |
| */ | |
| function update_extension($current = '') | |
| { | |
| if ($current == '' OR $current == $this->version) | |
| { | |
| return FALSE; | |
| } | |
| $this->EE->db->where('class', __CLASS__); | |
| $this->EE->db->update( | |
| 'extensions', | |
| array('version' => $this->version) | |
| ); | |
| } | |
| // ---------------------------------------------------------------------- | |
| /** | |
| * Responds to the store_cart_update_end hook in Exp:resso Store | |
| * to apply a discount to each product, as needed | |
| * | |
| * @return array of cart items | |
| */ | |
| function update_cart($cart_contents) | |
| { | |
| $this->EE->load->helper(array('store')); | |
| // Get the number of products currently in the user's cart | |
| $count = count($cart_contents['items']); | |
| // Add a $10 discount for 3 or more of the same product | |
| foreach ($cart_contents['items'] as $item) | |
| { | |
| if ($item['item_qty'] >= 3) | |
| { | |
| $cart_contents['order_discount_val'] += 10; | |
| } | |
| } | |
| // Add the quantity discount to any existing discount (promo code) | |
| $cart_contents['order_discount_val'] += $discount; | |
| // Make sure we don't end up with a negative total | |
| if ($cart_contents['order_discount_val'] > $cart_contents['order_subtotal_val']) | |
| { | |
| $cart_contents['order_discount_val'] = $cart_contents['order_subtotal_val']; | |
| } | |
| $cart_contents['order_discount'] = store_format_currency($cart_contents['order_discount_val']); | |
| // Update the cart total to reflect the new discount | |
| $cart_contents['order_total_val'] = $cart_contents['order_subtotal_val'] - $cart_contents['order_discount_val']; | |
| $cart_contents['order_total'] = store_format_currency($cart_contents['order_total_val']); | |
| return $cart_contents; | |
| } | |
| } | |
| /* End of file ext.store_discount.php */ | |
| /* Location: /system/expressionengine/third_party/store_discount/ext.store_discount_batch.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment