• I have a problem getting my function running through ajax. I need it to use my function when the page has finished loading. This is not a get function ore Post, it’s pure code. There should be a select opportunity.

    I’ve really searched a lot on google but just can’t find the solution.

    I would then like to be able to use my php function and display it on the page after the page has finished loading

    add_action('wp_ajax_add_woocommerce_file', 'add_woocommerce_file');
    add_action('wp_ajax_nopriv_add_woocommerce_file', 'add_woocommerce_file');
    function add_woocommerce_file() { ?>
    
     <form class="cart variation" action="" method="post" enctype='multipart/form-data'>
         <div class="popup">
                 <div class="popup-content">
                     <div class="close-content-container">X</div>
                 <?php the_content(); ?>
                     </div>
                </div>
         <?php
        global $product, $post;
    
        $output = '
        <select name="variation_id" id="variation_id">
        <option value="">V?lg...</option>';
    
        foreach( $product->get_available_variations() as $variation ){
            if($variation['max_qty'] > 0) {//Finder ud af om der er vare p? lager det den kalde variation.
            $option_value = array(); 
    
            foreach( $variation['attributes'] as $attribute => $term_slug ){
                $taxonomy = str_replace( 'attribute_', '', $attribute );
                $attribute_name = get_taxonomy( $taxonomy )->labels->singular_name; // Attribute name
                $term_name = get_term_by( 'slug', $term_slug, $taxonomy )->name; // Attribute value term name
    
                $option_value[] =  ' ' .$term_name. ' ';
    
            }
    
            $option_value = implode( '   ::   ', $option_value );
    
            $output .= '
            <option class="option_value" value="'.$variation['variation_id'].'">'.$option_value.'</option>';
    
            }
        }
        $output .= '
            </select>';
    ?><a type="button" id="open" class="open-popup">Kort varebeskrivelse</a><?php
       echo $output;
    
     ?>
         <input type="hidden" name="variation_id" id="variation_id" value="" />
         <input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
         <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $post->ID ); ?>" />
    
         <div class="tilfoej"><div class="cart_flex">L?g i kurv</div></div>
    
    </form> <?php 
    }
    
    add_filter('woocommerce_after_shop_loop_item', 'add_woocommerce_file', 60);
    
        // My ajax call
    
    jQuery(document).ready( function () {
    
        // Ajax 
        jQuery.ajax({
                url: the_ajax_script.ajaxurl,
                type: "POST",
                dataType: 'html',
                data: {
                        action: 'add_woocommerce_file'
                                },
                success: function( response ) {
                    jQuery(".variation").show();
                                },
                error : function() {
                    alert("virker ikke");
                }
        });
    });
    
    // output on category
        <div class="variation"></div>
Viewing 5 replies - 1 through 5 (of 5 total)
  • What is the point?
    The page is generated in PHP. there is no user input for the ajax function, so why use JS to go back to the server and generate the HTML?
    Just generate the HTML in PHP and use CSS to hide it, and have your JS trigger the CSS to show it when the page is loaded. No need for ajax.

    Thread Starter mortenamdk

    (@mortenamdk)

    I think that solution not will help the problem. Det variations will be at the start not Come when document is ready.
    There is meny product there has to be loaded about 60. And there meny database calls. And that slows the next page load.
    Hope that clearing why i need at solution to do that.

    Best regards
    Morten

    Moderator bcworkz

    (@bcworkz)

    You might be getting a better page speed score by loading most of the page with Ajax, but you are actually slowing down how long it takes for the user to see the data on the page.

    jQuery(document).ready() does what it is supposed to do. If you are having trouble with your code, it’s not the document ready aspect.

    Thread Starter mortenamdk

    (@mortenamdk)

    I dont have problem with my function, it is the Ajax i dont can get to work.
    But if it not possible to get it to work please say it.

    Moderator bcworkz

    (@bcworkz)

    Try url: the_ajax_script.ajaxurl+'?action=add_woocommerce_file',

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Need my function to run after document ready’ is closed to new replies.