Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/1a1b1e90337928732fb3e191d62d0aef to your computer and use it in GitHub Desktop.
Save wpmudev-sls/1a1b1e90337928732fb3e191d62d0aef to your computer and use it in GitHub Desktop.
[Forminator Pro] - Calculations with WooCommerce product
<?php
/**
* Plugin Name: [Forminator Pro] - Calculations with WooCommerce product
* Plugin URI: https://premium.wpmudev.org/
* Description: Make calculations with a product's data on front-end (as of 1.12.1.1)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: 0/11289012348292/1170321580851097
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// No need to do anything if the request is via WP-CLI.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
if ( ! class_exists( 'WPMUDEV_Forminator_Woo_Price_Calculations' ) ) {
class WPMUDEV_Forminator_Woo_Price_Calculations {
// User defined variables
private $form_id = '528';
private $field_number = 'number-1';
private $field_select = 'select-1';
private $price_cap = '200';
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Forminator_Woo_Price_Calculations();
}
return self::$_instance;
}
private function __construct() {
if ( ! class_exists( 'WooCommerce' ) || ! defined( 'FORMINATOR_VERSION' ) || FORMINATOR_VERSION < '1.12' ) {
return;
}
$this->init();
}
private function init(){
// Show form as 'Installments' tabs on product
add_filter( 'woocommerce_product_tabs', array( $this, 'wpmudev_woocommerce_product_tabs' ), 20 );
// Show form after woocommerce_template_single_excerpt() (Priority: 20)
add_action( 'woocommerce_single_product_summary', array( $this, 'woocommerce_product_installments_tab' ), 30 );
// Forminator render hooks
add_action( 'forminator_before_form_render', array( $this, 'wpmudev_forminator_before_form_render' ) );
// Forminator prevent submit hook
add_filter( 'forminator_custom_form_submit_errors', array( $this, 'wpmudev_forminator_custom_form_submit_errors' ), 10, 3 );
}
public function wpmudev_forminator_custom_form_submit_errors( $submit_errors, $form_id, $field_data_array ){
if ( $this->form_id != $form_id ){
return $submit_errors;
}
// In case of submission return an error message
if( wp_doing_ajax() ){
$response = array(
'success' => true,
'behav' => null,
'message' => __( "Error: This form can not be submitted!", 'wpmudev' ),
);
wp_send_json_error( $response );
}else{
add_filter( 'forminator_custom_form_invalid_form_message', function( $message, $id ){
return __( "Error: This form can not be submitted!", 'wpmudev' );
}, 10, 2);
}
return $submit_errors;
}
public function wpmudev_forminator_before_form_render( $form_id ){
if ( $this->form_id != $form_id ){
return;
}
// Fill hidden input with product price
add_filter( 'forminator_field_markup', array( $this, 'wpmudev_forminator_field_markup' ), 10, 2 );
// Remove submit button from form
add_filter( 'forminator_render_form_submit_markup', '__return_empty_string' );
}
public function wpmudev_forminator_field_markup( $html, $field ){
if( $field['element_id'] === $this->field_number ){
global $product;
if( $product->get_price() ){
$html = str_replace( 'value="' . $field['default_value'] . '"', 'value="' . $product->get_price() .'" disabled="disabled"', $html );
}
$html = str_replace( 'class="forminator-field', ' style="display: none;" class="forminator-field', $html );
}
if( $field['element_id'] === $this->field_select ){
global $product;
if( $product->get_price() > $this->price_cap ){
$html = str_replace( '</select>', '<option value="24" data-calculation="24">24</option></select>', $html );
}
}
return $html;
}
public function wpmudev_woocommerce_product_tabs( $tabs ){
$tabs['installments'] = array(
'title' => __( 'Installments', 'woocommerce' ),
'priority' => 10,
'callback' => array( $this, 'woocommerce_product_installments_tab' ),
);
return $tabs;
}
public function woocommerce_product_installments_tab(){ ?>
<h2><?php _e( 'Installments', 'woocommerce' ); ?></h2>
<?php echo do_shortcode( '[forminator_form id="' . $this->form_id . '"]' ); ?>
<?php }
}
add_action( 'plugins_loaded', function(){
return WPMUDEV_Forminator_Woo_Price_Calculations::get_instance();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment