0

I make my own plugin but I have a problem when I try to pass my function with the button. When I reload the page, my function passes but when I press the button, the function doesn't work. error Generate_coupon_woo undefined. I tried to do this for when a user tried to generate code with a click.

I make my own plugin but I have a problem when I try to pass my function with the button. When I reload the page, my function passes but when I press the button, the function doesn't work. error Generate_coupon_woo undefined. I tried to do this for when a user tried to generate code with a click.

<?php
/**
 * Plugin Name: Demo remote post
 * Description: connectd
 * Version:     1.0.0
 * Author:      Digital Studio
 */
defined('ABSPATH') or die('Unauthorized Access !');



add_shortcode('nouveauShortcode', 'gererShortcode');




function gererShortcode(){
  ?>
 
  <div class="wrap">
    <h2>Digital Studio Plugin</h2>
    <button class="btn btn-primary" type="but" id="submit">code Promo</button>
    </div>
    <script>
      jQuery(document).ready(function($) {
        $("#submit").on('click', Generate_coupon_woo);
      });
    </script>
  <?php
  } 
  






    function Generate_coupon_woo(){
        $url='localhost:3306';
     
        $coupon_code = substr( "abcdefghijklmnopqrstuvwxyz123456789", mt_rand(0, 50) , 1) .substr( md5( time() ), 1); // Code
        $coupon_code = substr( $coupon_code, 0,10); // create 10 letters coupon
        $amount = '15'; // Amount
        $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product // ici le type soit fixé sur le panier à la fin, soit en pourcentage sur le panier , soit fixé sur le produit, soit en pourcentage sur le produit
        
        $coupon = array(
            'post_title'   => $coupon_code,
            'post_content' => '€15 off coupon',
            'post_excerpt' => '€15 off coupon',
            'post_status'  => 'publish',
            'post_author'  => 1,
            'post_type'    => 'shop_coupon'
        );
     
     
        $username = "*******";
        $password = "********";
        $database = "******";
        
        $mydb = new wpdb($username, $password, $database, $url);
        $rows = $mydb->get_results("SELECT comment_author FROM VWfBb8z7_comments");
        //$new_coupon_id = $mydb->wp_insert_post( $coupon );
        $insert_test = $mydb->insert( 'VWfBb8z7_posts', $coupon );
          
        update_post_meta( $insert_test, 'discount_type', $discount_type );
        update_post_meta( $insert_test, 'coupon_amount', $amount );
        update_post_meta( $insert_test, 'individual_use', 'yes' );
        update_post_meta( $insert_test, 'product_ids', '' );
        update_post_meta( $insert_test, 'exclude_product_ids', '' );
        update_post_meta( $insert_test, 'usage_limit', '1' );
        update_post_meta( $insert_test, 'expiry_date', '' );
        update_post_meta( $insert_test, 'apply_before_tax', 'no' );
        update_post_meta( $insert_test, 'free_shipping', 'no' );      
        update_post_meta( $insert_test, 'exclude_sale_items', 'no' );     
        update_post_meta( $insert_test, 'free_shipping', 'no' );      
        update_post_meta( $insert_test, 'product_categories', '' );       
        update_post_meta( $insert_test, 'exclude_product_categories', '' );       
        update_post_meta( $insert_test, 'minimum_amount', '' );       
        update_post_meta( $insert_test, 'customer_email', '' ); 
      }
      

  ?>

1 Answer 1

1

According to the documentation for add_shortcode: https://developer.wordpress.org/reference/functions/add_shortcode/

"Note that the function called by the shortcode should never produce an output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call since you cannot control when and where they are called from."

You may want to change your strategy on how you want to implement this code, and where you want it to be implemented.

If you do want to go this route. Try adding this. Adding to init gives it time for WordPress to initialise it.

add_action( 'init', 'wpdocs_add_custom_shortcode' );
 
function gerer_add_custom_shortcode() {
    add_shortcode('nouveauShortcode', 'gererShortcode');
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.