Last active
February 11, 2019 08:48
-
-
Save davidtowoju/11802e81e793a0832eb4d0eda0882c99 to your computer and use it in GitHub Desktop.
Enable margin on GP ecommerce discount field
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 | |
/* | |
Plugin Name: GP Ecommerce Margin | |
Plugin URI: https://figarts.co | |
*/ | |
/** You may noe start coding! **/ | |
class GPECF_Margins { | |
private static $instance = null; | |
public static function get_instance() { | |
if( null == self::$instance ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
private function __construct() { | |
add_action( 'init', array( $this, 'hooks' ) ); | |
} | |
public function hooks() { | |
add_filter( 'gform_register_init_scripts', array( $this, 'register_init_script' ) ); | |
add_filter( 'gform_field_content', array( $this, 'add_margin_attribute' ), 10, 5 ); | |
add_action( 'gpecf_discount_total', array( $this, 'filter_discount_as_margin' ), 10, 2 ); | |
add_action( 'gform_field_standard_settings', array( $this, 'gf_add_custom_field' ), 10, 2); | |
add_action('gform_editor_js', array($this, 'editor_script'), 11, 2); | |
} | |
public function register_init_script( $form ) { | |
if( ! gp_ecommerce_fields()->has_ecommerce_field( $form ) ) { | |
return; | |
} | |
$script = '(function($){' . | |
'gform.addFilter( "gpecf_discount_total", function( discount, $field ) { | |
if( $field.data( "enablemargin" ) == 1 ) { | |
discount *= -1; | |
} | |
// alert(discount); | |
return discount; | |
} );' . | |
'})(jQuery);'; | |
// $script = ; | |
GFFormDisplay::add_init_script( $form['id'], 'gpecf_margins', GFFormDisplay::ON_PAGE_RENDER, $script ); | |
} | |
public function add_margin_attribute( $content, $field, $value, $entry_id, $form_id ) { | |
if( $field->is_form_editor() || $field->is_entry_detail() || $field->type !== 'discount' ) { | |
return $content; | |
} | |
$margins_enabled = rgobj( $field, 'marginField' ); | |
if ($margins_enabled == true) $margins_enabled = 1; | |
$search = "name='input_{$field->id}'"; | |
$replace = sprintf( "%s data-enableMargin='%d'", $search, $margins_enabled ); | |
$content = str_replace( $search, $replace, $content ); | |
return $content; | |
} | |
public function filter_discount_as_margin( $discount, $field ) { | |
if( $field->type == 'discount' && $field->marginField ) { | |
$discount *= -1; | |
} | |
return $discount; | |
} | |
public function gf_add_custom_field( $position, $form_id ) | |
{ | |
if ($position == 20) { | |
// retrieve the data earlier stored in the database or create it | |
?> | |
<li class="margin_setting field_setting"> | |
<input type="checkbox" id="margin_field" value="1" onclick="SetFieldProperty( 'marginField', this.checked);"> | |
<label class="inline" for="margin_field"> | |
<?php esc_html_e( 'Enable Margin' ); | |
//form_tooltip( 'my-custom-setting-toolip' ); ?> | |
</label> | |
</li> | |
<?php | |
} | |
} | |
public function editor_script() { | |
?> | |
<script type="text/javascript"> | |
(function($) { | |
$(document).bind( 'gform_load_field_settings', function( event, field, form ) { | |
// populates the stored value from the field back into the setting when the field settings are loaded | |
$( '#margin_field' ).attr( 'checked', field['marginField'] == true ); | |
// if our desired condition is met, we show the field setting; otherwise, hide it | |
if( GetInputType( field ) == 'discount' ) { | |
$( '.margin_setting' ).show(); | |
} else { | |
$( '.margin_setting' ).hide(); | |
} | |
} ); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
} | |
function gpecf_margins() { | |
return GPECF_Margins::get_instance(); | |
} | |
gpecf_margins(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment