首页 > 解决方案 > How do at make my standalone file woocommerce ready so I can use the functions

问题描述

Hi I am having a problem getting my standalone file to use woocommerce functions. may make it work when I hook, but will need to retrieve it for themselves. So I can use the file to jQuery.load (URL) So I come up with that function when the page finishes loading.

Is it possible? Or is that not possible?

My jQuery call for file:

jQuery( document ).ready(function() {
    jQuery('div#result').load('http://localhost/(hidden)/wp-content/themes/storefront-child/variation_product_site.php');
});

My standalone file


<?php
    global $product, $post;

    $variations = $product->get_available_variations();
?>

<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

    $output = '
    <select name="variation_id" id="variation_id">
    <option value="">Vælg...</option>';

    foreach( $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>


function add_woocommerce_file() {
    echo '<div id="result"></div>';
}
add_filter('woocommerce_after_shop_loop_item', 'add_woocommerce_file', 60);

Fatal error: Uncaught Error: Call to a member function get_available_variations() on null in C:\www(Hidden)\wp-content\plugins\variation-product-site\variation_product_site.php on line 9

标签: phpwordpressfilewoocommerce

解决方案


推荐阅读