0
votes

So I created a jquery script to fetch a data upon selecting from the drop down box, and show the amount of stocks but the thing is, it says: Fatal error: Class 'WC_Product_Variable' not found.

But when testing things out to directly run the file, it works, but when being fetched by jquery and show it so the page wont refresh it's not showing anything but instead throwing an error.

change.js


    $(document).ready(function(){
     $( ".selectProductClass" )
        .on('change', function () {
            var str = "";
            $( "select.selectProductClass option:selected" ).each(function() {
              str += $( this ).val() + " ";
            });
            $.post('../wp-content/plugins/pos-extension-supplier/admin/pos-extension-product-stock.php"); ?>', {str:str}, function(data) {
                $('div.stocksLeftInHere').text(data);
            });
        });
    });

product-stock.php


    $product_id=$_POST['str'];
    echo $product_id;

    $handle=new WC_Product_Variable($product_id);
    $variations1=$handle->get_children();

    foreach ($variations1 as $value) {
        $single_variation=new WC_Product_Variation($value);
        $branch_name = implode(" / ", $single_variation->get_variation_attributes());

            if($branch_name == "Warehouse") {
                echo "".$single_variation->stock."";
                echo "";
                echo "stock."'>";
            }
    }

The problem is, it doesn't found the WC_Product_variable, Fatal error: Class 'WC_Product_Variable' not found.

1
What is your woocommerce version? - Ruhul Amin
@RuhulAmin its 2.6.7 - Hades Hades
@RuhulAmin also to add, i think it somehow runs alone, what i mean is even using wp basic functions are not working also. I do call the file for running a few query but how will it be able to execute if the the classes aren't recognizable by it. - Hades Hades

1 Answers

0
votes

In your case, the Woocommerce classes are not already load when your script want to use WC_Product_Variable.

You need to include the file manually before calling the class

if(!class_exists('WC_Product_Variable')){
    include(plugin_url.'/woocommerce/includes/class-wc-product-variable.php');// adjust the link
}

I hope it helps ;-)