2

In my WooCommerce project, in the functions.php file I have written the following function which runs when the WooCommerce shop page loads all products. I have found the source of the problem but can not figure out why it happens.

Somehow only one can exist between: $prodVars, $prodVar or $prodPrices. If all three are active (uncommented) then the shop page loop breaks and shows the first item only. If I comment two of those variables the loop runs perfectly and displays all information.

Is there something that I misunderstood and used incorrectly?

function get_desc(){
    $prodID = get_the_ID();
    $product = wc_get_product($prodID);
    $product2 = wc_get_product($prodID);
    $prodDesc = $product->get_short_description();
    echo "<div class='prodDetWrap'>";
    echo "<div class='prodVariations'>";

    $prodVars = $product->get_attributes();

    $prodVar = $product->get_variation_attributes();

    $prodPrices = $product2->get_variation_prices();

        $prodPrice = $prodPrices['price'];
    $prod1Price = current($prodPrice);
    $prodCnt = 0;

    echo "</div>";
    if($prodDesc !== "NULL"){
        echo "<p>";
        echo $prodDesc;
        echo "</p>";
    }else{
        ;
    }

    echo "<p class='disclaimer'>Er varen ikke på lager, bestiller vi dem hjem til jer.</p>";
    echo "</div>";
}

remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
add_action( 'woocommerce_after_shop_loop_item_title', 'get_desc', 12);

1 Answer 1

2

There are multiple mistakes in your code, like:

  • You are getting 2 times the WC_Product object using wc_get_product(). Instead you should call the available global WC_Product object.
  • Segmentation between variable products and other product types is mandatory, as WC_Variable_Product methods will throw errors on other product types, like for:
    • get_variation_attributes() method,
    • get_variation_prices() method,
  • $prodDesc variable should be defined and $prodCnt is not used.
  • else{ ; } will throw an error.

You should enabled the debug when testing code, using define( 'WP_DEBUG', true ); in the root wp-config.php file of your installation.

Try the following instead:

remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );

add_action( 'woocommerce_after_shop_loop_item_title', 'get_desc', 12 );

function get_desc(){
    global $product;

    if( ! is_a( $product, 'WC_Product') ) {
        $product = wc_get_product(get_the_ID());
    }

    $short_description = $product->get_short_description();

    echo "<div class='prodDetWrap'>
    <div class='prodVariations'>";

    $product_attributes = $product->get_attributes();

    // Only WC_Variable_Product methods (for variable products only)
    if( $product->is_type('variable') ) {
        $variation_attr   = $product->get_variation_attributes();
        $variation_prices = $product->get_variation_prices();

        $variation_price  = reset($variation_prices['price']);
    }

    $prodCnt = 0;

    echo "</div>";

    if ( isset($prodDesc) && $prodDesc !== "NULL") {
        echo "<p>";
        echo $prodDesc;
        echo "</p>";
    } else {
        echo '';
    }

    echo "<p class='disclaimer'>Er varen ikke på lager, bestiller vi dem hjem til jer.</p>
    </div>";
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Sign up to request clarification or add additional context in comments.

1 Comment

How about explaining what the mistakes are so users don't have to dissect your code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.