id = "agechecked_woocommerce_addon"; // The Title shown on the top of the settings page $this->method_title = __("AgeChecked", 'agechecked-wc-addon'); // The description, shown on the actual Payment options page on the backend $this->method_description = __("AgeChecked Plug-in for WooCommerce", 'agechecked-wc-addon'); // The title to be used for the vertical tabs that can be ordered top to bottom $this->title = __("AgeChecked", 'agechecked-wc-addon'); // If you want to show an image next to the plugin name on the frontend, enter a URL to an image. $this->icon = null; // This basically defines your settings which are then loaded with init_settings() $this->init_form_fields(); $this->init_settings(); // Turn these settings into variables we can use foreach ($this->settings as $setting_key => $value) { $this->$setting_key = is_string($value) ? trim($value) : $value; } // Save settings if (is_admin()) { // Versions over 2.0 // Save our administration options. Since we are not going to be doing anything special // we have not defined 'process_admin_options' in this class so the method in the parent // class will be used instead add_action('woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options', )); } add_action('wp_enqueue_scripts', array($this, 'agechecked_woocommerce_addon_enqueue_scripts')); add_action('woocommerce_checkout_before_customer_details', array($this, 'agechecked_woocommerce_addon_ui')); //validating add_action('woocommerce_checkout_process', array($this, 'agechecked_woocommerce_addon_verify')); // Saving data add_action('woocommerce_checkout_update_order_meta', array($this, 'agechecked_woocommerce_addon_checkout_update_order_meta')); //showing data in admin add_action('woocommerce_admin_order_data_after_billing_address', array($this, 'agechecked_woocommerce_addon_admin_order_data_after_billing_address'), 10, 1); load_plugin_textdomain('agechecked-wc-addon', false, dirname(plugin_basename(__FILE__)) . '/lang/'); } function WC_compat($order, $old, $new = null, $old_is_property = true, $new_is_property = false) { $method_property_name = !$new ? 'get_'.$old : $new; if (!$new_is_property) { return method_exists($order, $method_property_name) ? $order->$method_property_name() : ($old_is_property ? $order->$old : $order->$old()); } else { return property_exists($order, $method_property_name) ? $order->$method_property_name : ($old_is_property ? $order->$old : $order->$old()); } } /** * Output plugin settings to WC Checkout tab. */ public function admin_options() { echo '

' . esc_html( $this->method_title ) . '

'; echo wp_kses_post( wpautop( $this->method_description ) ); parent::admin_options(); $this->checks(); } //Plugin settings function init_form_fields() { $this->form_fields = array( 'enabled' => array( 'id' => 'agechecked_wc_settings_enabled', 'title' => __('Enable/Disable', 'agechecked-wc-addon'), 'label' => __('Enable age checks on checkout', 'agechecked-wc-addon'), 'desc_tip' => __('Select to enable age checks.', 'agechecked-wc-addon'), 'type' => 'checkbox', 'default' => 'no', ), 'url' => array( 'title' => __('API URL', 'agechecked-wc-addon'), 'desc_tip' => __('Please insert the API URL provided by your agechecked.com account manager.', 'agechecked-wc-addon'), 'id' => 'agechecked_wc_settings_url', 'type' => 'text', 'css' => 'min-width:300px;', 'default' => 'https://staging.agechecked.com/api', // WC >= 2.0 'placeholder' => 'e.g. https://staging.agechecked.com/api', 'description' => __('', 'agechecked-wc-addon') ), 'public_key' => array( 'title' => __('Public Key', 'agechecked-wc-addon'), 'desc_tip' => __('Please insert the public key provided by your agechecked.com account manager.', 'agechecked-wc-addon'), 'id' => 'agechecked_wc_settings_public_key', 'type' => 'text', 'css' => 'min-width:600px;', 'default' => '', // WC >= 2.0 'description' => __('', 'agechecked-wc-addon') ), 'private_key' => array( 'title' => __('Private Key', 'agechecked-wc-addon'), 'desc_tip' => __('Please insert the private key provided by your agechecked.com account manager.', 'agechecked-wc-addon'), 'id' => 'agechecked_wc_settings_private_key', 'type' => 'text', 'css' => 'min-width:600px;', 'default' => '', // WC >= 2.0 'description' => __('', 'agechecked-wc-addon') ), "product_category" => array( 'title' => __('Product Category', 'agechecked-wc-addon'), 'desc_tip' => __('Please enter the product category that should be age checked. Ensure category exists.', 'agechecked-wc-addon'), 'id' => 'agechecked_wc_settings_product_category', 'type' => 'text', 'default' => 'AgeChecked', 'placeholder' => 'Limit by category (e.g. AgeChecked)', 'css' => 'min-width:300px;', 'description' => __('Allocates age checks to the specified category of products. Leave empty to age check all products.', 'agechecked-wc-addon') ) ); } // Check if SSL is enabled and notify the user. public function checks() { if ( 'no' == $this->enabled ) { return false; } // PHP Version if ( version_compare( phpversion(), '5.3', '<' ) ) { echo '

' . sprintf( __( 'AgeChecked Error: AgeChecked requires PHP 5.3 and above. You are using version %s.', 'agechecked-wc-addon' ), phpversion() ) . '

'; return false; } // Check required fields if (!$this->url) { echo '

' . __( 'AgeChecked Error: Please enter your API URL', 'transactium-wc-addon' ) . '

'; return false; } // Check required fields if ( ! $this->public_key || ! $this->private_key ) { echo '

' . __( 'AgeChecked Error: Please enter your public and private keys', 'transactium-wc-addon' ) . '

'; return false; } return true; } /** * Disabling payment gateway functionality as we only require this for settings in WC Checkout tab * * @return bool */ public function is_available() { return false; } //Checking if product category specified in plugin settings matches the given product category function check_agechecked_product_category($product_category) { $agechecked_product_category_name = $this->product_category; if ($product_category->name === $agechecked_product_category_name) return true; return false; } function agechecked_woocommerce_addon_check_if_required() { if($this->enabled === "no" || !$this->checks()) return false; global $woocommerce; $agechecked_product_category_name = $this->product_category; //If no category is set then all categories are age checked if (empty($agechecked_product_category_name)) return true; foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { $_product = $values['data']; $terms = get_the_terms($_product->id, 'product_cat'); if (!empty($terms)) { // second level loop search, in case some items have several categories foreach ($terms as $term) { if ($this->check_agechecked_product_category($term)) { return true; } } } } return false; } function agechecked_woocommerce_addon_enqueue_scripts() { if (!$this->agechecked_woocommerce_addon_check_if_required()) return false; $public_key = $this->public_key; $url = $this->url; // Enqueues required JS files wp_enqueue_script('jqhack', plugins_url('agechecked-woocommerce-addon/assets/js/jqhack.js'), array('jquery')); $url = add_query_arg(array('merchantkey' => $public_key, 'version' => '1.0'), $url . '/jsapi/getjavascript'); wp_enqueue_script('agechecked', $url, array('jquery', 'jqhack'), null); wp_enqueue_style('agechecked', plugins_url('agechecked-woocommerce-addon/assets/css/style.css'), array()); } //Loading AgeChecked UI on checkout function agechecked_woocommerce_addon_ui($checkout) { if (!$this->agechecked_woocommerce_addon_check_if_required()) return false; $checkout = WC()->checkout(); ?> agechecked_woocommerce_addon_check_if_required()) return false; $private_key = $this->private_key; $url = $this->url; $url = add_query_arg(array('merchantkey' => $private_key, 'agecheckid' => $_POST['agechecked_agecheckid']), $url . "/jsonapi/getagecheck"); $resp = wp_remote_get($url); $jd = (object) json_decode(wp_remote_retrieve_body($resp), true); if (($jd->status != 6 && $jd->status != 7) || $jd->agecheckid != $_POST['agechecked_agecheckid'] || $jd->ageverifiedid != $_POST['agechecked_ageverifiedid']) wc_add_notice(__('Could not age check you', 'agechecked-wc-addon'), 'error'); } //Adding result to order meta function agechecked_woocommerce_addon_checkout_update_order_meta($order_id) { if (!empty($_POST['agechecked_ageverifiedid'])) { update_post_meta($order_id, 'ageverifiedid', sanitize_text_field($_POST['agechecked_ageverifiedid'])); } } //Showing result out to user on order completion function agechecked_woocommerce_addon_admin_order_data_after_billing_address($order) { echo '

' . __('AgeVerifiedId', 'agechecked-wc-addon') . ': ' . get_post_meta($this->WC_compat($order, 'id'), 'ageverifiedid', true) . '

'; } } add_filter('woocommerce_payment_gateways', 'agechecked_woocommerce_addon_gateway'); function agechecked_woocommerce_addon_gateway($methods) { $methods[] = AgeChecked_WC_Addon::get_instance(); return $methods; } } // Add custom action links add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'agechecked_woocommerce_addon_action_links'); function agechecked_woocommerce_addon_action_links($links) { $plugin_links = array( '' . __('Settings', 'agechecked-wc-addon') . '' ); // Merge our new link with the default ones return array_merge($plugin_links, $links); } ?>