• Resolved mktecknok

    (@mktecknok)


    I have created a simple popup on a test page where I load the product summary of a variable product. When I paste the following code in the php file directly, it works perfectly.

    global $product;
    $product = wc_get_product(14);

    do_action( ‘woocommerce_single_product_summary’ );

    But if I insert it via ajax through functions.php, the variable prices are missing and when the product is added to cart, I get an error message saying that “Please choose product options by visiting BBQ Chicken Pizza”.

    Here’s the jquery function.

    $('.product_parent_tabs .variable_a2c_btn').click(function(){
    
        var prod_id = $(this).attr("data-prod") ; 
    
        $.ajax({
            url: ajaxurl,
            data: {
                'action':'cus_product_popup_ajax', // This is our PHP function below
                'prod_id' : prod_id // This is the variable we are sending via AJAX
            },
            success:function(data) {
        // This outputs the result of the ajax request (The Callback)
                $('#cus_popup .cus_popup_details').html(data);
            },
            error: function(errorThrown){
                window.alert(errorThrown);
            }
        });

    functions.php code below

    function cus_product_popup_ajax() {
    
        if ( isset($_REQUEST) ) {
            $prod_id = $_REQUEST['prod_id'];
    
            global $product;
            $product = wc_get_product(14);
    
            do_action( 'woocommerce_single_product_summary' );
    
        }
    
        wp_die();
    }
    
    add_action( 'wp_ajax_cus_product_popup_ajax', 'cus_product_popup_ajax' );
    add_action( 'wp_ajax_nopriv_cus_product_popup_ajax', 'cus_product_popup_ajax' );
    • This topic was modified 2 years, 3 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce do action does not work in Ajax functions, but works when hardcoded’ is closed to new replies.