Skip to content

Instantly share code, notes, and snippets.

@nathanpitman
Created March 28, 2014 16:23
Show Gist options
  • Save nathanpitman/9836805 to your computer and use it in GitHub Desktop.
Save nathanpitman/9836805 to your computer and use it in GitHub Desktop.
Automatically populates the Expresso Store SKU field with the corresponding entry_id value on save if no custom value is specified.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ExpressionEngine - by EllisLab
*
* @package ExpressionEngine
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2003 - 2011, EllisLab, Inc.
* @license http://expressionengine.com/user_guide/license.html
* @link http://expressionengine.com
* @since Version 2.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Auto Store SKU Extension
*
* @package ExpressionEngine
* @subpackage Addons
* @category Extension
* @author Nathan Pitman
* @link http://ninefour.co.uk/labs
*/
class Auto_store_sku_ext {
public $settings = array();
public $description = 'Automatically populates the Expresso Store SKU field with the corresponding entry_id value on save if no custom value is specified.';
public $docs_url = 'http://ninefour.co.uk/labs';
public $name = 'Auto Store SKU';
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();
$data = array(
'class' => __CLASS__,
'method' => 'populate_sku',
'hook' => 'entry_submission_end',
'settings' => serialize($this->settings),
'version' => $this->version,
'enabled' => 'y'
);
$this->EE->db->insert('extensions', $data);
}
// ----------------------------------------------------------------------
/**
* populate_sku
*
* @param
* @return
*/
public function populate_sku($entry_id, $meta, $data)
{
if (isset($data['store_product_field'])) {
// Auto-populate SKU with entry_id if no value is specified
$data = array('sku' => $entry_id);
$sql = $this->EE->db->update_string('exp_store_stock', $data, "entry_id = " . $entry_id . " AND sku = ''");
$this->EE->db->query($sql);
}
}
// ----------------------------------------------------------------------
/**
* 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;
}
}
// ----------------------------------------------------------------------
}
/* End of file ext.auto_store_sku.php */
/* Location: /system/expressionengine/third_party/auto_store_sku/ext.auto_store_sku.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment