Avatar billede Morten Professor
27. august 2019 - 22:26 Der er 5 kommentarer

Woocommerce og ny ekstern fil

Hej

Jeg har nogle problemer med at få integreret funktionerne fra woocommerce i min fil som jeg skal bruge.
Her er min kode:
<?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 woocommerce_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>
<?php

Får fejlen:
Error: Call to a member function get_available_variations() on null in C:\wamp64\www\teenstyle_live\wp-content\themes\storefront-child\

Håber i kan hjælpe mig.

Med venlig hilsen
Morten
Avatar billede acore Ekspert
28. august 2019 - 07:53 #1
$product har formodentlig ikke nogen værdi. Prøv at indsætte linien

var_dump($product);

hvis du gerne vil se indholdet af $product
Avatar billede Morten Professor
28. august 2019 - 10:27 #2
Hej tak for svar

Jeg prøvede lige og der kommer dette frem:
object(WC_Product_Variable)[11145]
  protected 'children' =>
    array (size=2)
      0 => int 43
      1 => int 44
  protected 'visible_children' =>
    array (size=2)
      0 => int 43
      1 => int 44
  protected 'variation_attributes' => null
  protected 'object_type' => string 'product' (length=7)
  protected 'post_type' => string 'product' (length=7)
  protected 'cache_group' => string 'products' (length=8)
  protected 'data' =>
    array (size=50)
      'name' => string 'Test billede' (length=12)
      'slug' => string 'test-billede' (length=12)
      'date_created' =>
        object(WC_DateTime)[11147]
          protected 'utc_offset' => int 0
          public 'date' => string '2019-06-18 09:49:37.000000' (length=26)
          public 'timezone_type' => int 3
          public 'timezone' => string 'Europe/Copenhagen' (length=17)
      'date_modified' =>
        object(WC_DateTime)[11150]
          protected 'utc_offset' => int 0
          public 'date' => string '2019-07-16 08:10:20.000000' (length=26)
          public 'timezone_type' => int 3
          public 'timezone' => string 'Europe/Copenhagen' (length=17)
      'status' => string 'publish' (length=7)
      'featured' => boolean false
      'catalog_visibility' => string 'visible' (length=7)
      'description' => string 'Test test' (length=9)
      'short_description' => string '' (length=0)
      'sku' => string '' (length=0)
      'price' => string '200' (length=3)
      'regular_price' => string '' (length=0)
      'sale_price' => string '' (length=0)
      'date_on_sale_from' => null
      'date_on_sale_to' => null
      'total_sales' => int 1
      'tax_status' => string 'taxable' (length=7)
      'tax_class' => string '' (length=0)
      'manage_stock' => boolean false
      'stock_quantity' => null
      'stock_status' => string 'instock' (length=7)
      'backorders' => string 'no' (length=2)
      'low_stock_amount' => string '' (length=0)
      'sold_individually' => boolean false
      'weight' => string '' (length=0)
      'length' => string '' (length=0)
      'width' => string '' (length=0)
      'height' => string '' (length=0)
      'upsell_ids' =>
        array (size=0)
          empty
      'cross_sell_ids' =>
        array (size=0)
          empty
      'parent_id' => int 0
      'reviews_allowed' => boolean true
      'purchase_note' => string '' (length=0)
      'attributes' =>
        array (size=3)
          'pa_hoejde' =>
            object(WC_Product_Attribute)[11153]
              ...
          'pa_laengde' =>
            object(WC_Product_Attribute)[11154]
              ...
          'pa_str' =>
            object(WC_Product_Attribute)[11171]
              ...
      'default_attributes' =>
        array (size=0)
          empty
      'menu_order' => int 0
      'post_password' => string '' (length=0)
      'virtual' => boolean false
      'downloadable' => boolean false
      'category_ids' =>
        array (size=1)
          0 => int 15
      'tag_ids' =>
        array (size=0)
          empty
      'shipping_class_id' => int 0
      'downloads' =>
        array (size=0)
          empty
      'image_id' => string '34' (length=2)
      'gallery_image_ids' =>
        array (size=0)
          empty
      'download_limit' => int -1
      'download_expiry' => int -1
      'rating_counts' =>
        array (size=0)
          empty
      'average_rating' => string '0' (length=1)
      'review_count' => int 0
  protected 'supports' =>
    array (size=0)
      empty
  protected 'id' => int 33
  protected 'changes' =>
    array (size=0)
      empty
  protected 'object_read' => boolean true
  protected 'extra_data' =>
    array (size=0)
      empty
  protected 'default_data' =>
    array (size=50)
      'name' => string '' (length=0)
      'slug' => string '' (length=0)
      'date_created' => null
      'date_modified' => null
      'status' => boolean false
      'featured' => boolean false
      'catalog_visibility' => string 'visible' (length=7)
      'description' => string '' (length=0)
      'short_description' => string '' (length=0)
      'sku' => string '' (length=0)
      'price' => string '' (length=0)
      'regular_price' => string '' (length=0)
      'sale_price' => string '' (length=0)
      'date_on_sale_from' => null
      'date_on_sale_to' => null
      'total_sales' => string '0' (length=1)
      'tax_status' => string 'taxable' (length=7)
      'tax_class' => string '' (length=0)
      'manage_stock' => boolean false
      'stock_quantity' => null
      'stock_status' => string 'instock' (length=7)
      'backorders' => string 'no' (length=2)
      'low_stock_amount' => string '' (length=0)
      'sold_individually' => boolean false
      'weight' => string '' (length=0)
      'length' => string '' (length=0)
      'width' => string '' (length=0)
      'height' => string '' (length=0)
      'upsell_ids' =>
        array (size=0)
          empty
      'cross_sell_ids' =>
        array (size=0)
          empty
      'parent_id' => int 0
      'reviews_allowed' => boolean true
      'purchase_note' => string '' (length=0)
      'attributes' =>
        array (size=0)
          empty
      'default_attributes' =>
        array (size=0)
          empty
      'menu_order' => int 0
      'post_password' => string '' (length=0)
      'virtual' => boolean false
      'downloadable' => boolean false
      'category_ids' =>
        array (size=0)
          empty
      'tag_ids' =>
        array (size=0)
          empty
      'shipping_class_id' => int 0
      'downloads' =>
        array (size=0)
          empty
      'image_id' => string '' (length=0)
      'gallery_image_ids' =>
        array (size=0)
          empty
      'download_limit' => int -1
      'download_expiry' => int -1
      'rating_counts' =>
        array (size=0)
          empty
      'average_rating' => int 0
      'review_count' => int 0
  protected 'data_store' =>
    object(WC_Data_Store)[11146]
      private 'instance' =>
        object(WC_Product_Variable_Data_Store_CPT)[11149]
          protected 'prices_array' =>
            array (size=2)
              ...
          protected 'internal_meta_keys' =>
            array (size=42)
              ...
          protected 'must_exist_meta_keys' =>
            array (size=1)
              ...
          protected 'extra_data_saved' => boolean false
          protected 'updated_props' =>
            array (size=0)
              ...
          protected 'meta_type' => string 'post' (length=4)
          protected 'object_id_field_for_meta' => string '' (length=0)
      private 'stores' =>
        array (size=20)
          'coupon' => string 'WC_Coupon_Data_Store_CPT' (length=24)
          'customer' => string 'WC_Customer_Data_Store' (length=22)
          'customer-download' => string 'WC_Customer_Download_Data_Store' (length=31)
          'customer-download-log' => string 'WC_Customer_Download_Log_Data_Store' (length=35)
          'customer-session' => string 'WC_Customer_Data_Store_Session' (length=30)
          'order' => string 'WC_Order_Data_Store_CPT' (length=23)
          'order-refund' => string 'WC_Order_Refund_Data_Store_CPT' (length=30)
          'order-item' => string 'WC_Order_Item_Data_Store' (length=24)
          'order-item-coupon' => string 'WC_Order_Item_Coupon_Data_Store' (length=31)
          'order-item-fee' => string 'WC_Order_Item_Fee_Data_Store' (length=28)
          'order-item-product' => string 'WC_Order_Item_Product_Data_Store' (length=32)
          'order-item-shipping' => string 'WC_Order_Item_Shipping_Data_Store' (length=33)
          'order-item-tax' => string 'WC_Order_Item_Tax_Data_Store' (length=28)
          'payment-token' => string 'WC_Payment_Token_Data_Store' (length=27)
          'product' => string 'WC_Product_Data_Store_CPT' (length=25)
          'product-grouped' => string 'WC_Product_Grouped_Data_Store_CPT' (length=33)
          'product-variable' => string 'WC_Product_Variable_Data_Store_CPT' (length=34)
          'product-variation' => string 'WC_Product_Variation_Data_Store_CPT' (length=35)
          'shipping-zone' => string 'WC_Shipping_Zone_Data_Store' (length=27)
          'webhook' => string 'WC_Webhook_Data_Store' (length=21)
      private 'current_class_name' => string 'WC_Product_Variable_Data_Store_CPT' (length=34)
      private 'object_type' => string 'product-variable' (length=16)
  protected 'meta_data' => null
Avatar billede acore Ekspert
28. august 2019 - 10:53 #3
Ja, så er det vist ikke forklaringen.

Kan du ikke se af fejlmeldingen i hvilken fil og linie den kommer? Er det helt sikkert, at det er den linie, du viser?
Avatar billede Morten Professor
28. august 2019 - 11:26 #4
Fatal error: Uncaught Error: Call to a member function get_available_variations() on null in C:\wamp64\www\(ses ikke)\wp-content\themes\storefront-child\woocommerce\variation_product_site.php on line 4
Avatar billede acore Ekspert
28. august 2019 - 11:49 #5
Der virker underligt - har ingen god forklaring. Måske andre har en idé?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester