PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to Define Product in WooCommerce Shortcode to use on Custom Order Received Page

 May 09, 2022     php, product, shortcode, woocommerce, wordpress     No comments   

Issue

I am trying to understand how to define product in this code after having made some changes to it based on the answer in my previous question Why does WooCommerce Order Shortcode Generate Plugin Notice on Custom Thank You Page?

The notice message have changed and now it asks me to define product. This is the notice:

Notice: Undefined variable: product in /wp-content/themes/storefront/functions.php on line 842

The notice refers to this line:

'purchase_note' => $product ? $product->get_purchase_note() : '',

This is the full code:

function order_cost_breakdown(){

    if ( isset( $_GET['order_id']) && $_GET['order_id'] > 0 ) {

        $order_id = (int) esc_attr( $_GET['order_id'] );

        $order = wc_get_order( $order_id );

        $order_data = $order->get_data();

        $show_purchase_note    = $order->has_status( apply_filters( 'woocommerce_purchase_note_order_statuses', array( 'completed', 'processing' ) ) );

    ob_start();
    
    ?>

        <div class="woocommerce-account woocommerce-page"><div class="woocommerce">

            <table class="shop_table order_details">

                <thead>

                    <tr>
                        <th class="product-name"><?php _e( 'Product', 'woocommerce' ); ?></th>
                        <th class="product-total"><?php _e( 'Total', 'woocommerce' ); ?></th>

                    </tr>

                </thead>

            <tbody>

        <?php foreach ( $order->get_items() as $item_id => $item ) {

        wc_get_template( 'order/order-details-item.php', array (
        'order' => $order,
        'item_id' => $item_id,
        'item' => $item,
        'product' => apply_filters( 'woocommerce_order_item_product', $item->get_product( $item ), $item ),
        'show_purchase_note' => $show_purchase_note,
        'purchase_note' => $product ? $product->get_purchase_note() : '',
        ) );
        }
    ?>

    <?php do_action( 'woocommerce_order_items_table', $order ); ?>

            </tbody>
        
        <tfoot>

    <?php

        foreach ( $order->get_order_item_totals() as $key => $total ){
    ?>

    <tr>
    
        <th scope="row"><?php echo $total['label']; ?></th>
        <td><?php echo $total['value']; ?></td>
    </tr>

        <?php
    }

    ?>
            </tfoot>
    
        </table>
    
    </div>

</div>

<?php

    return ob_get_clean();
    
    }

}

Any advice is helpful as I am clearly not an expert.


Solution

To fix the notice

Replace

<?php foreach ( $order->get_items() as $item_id => $item ) {

    wc_get_template( 'order/order-details-item.php', array (
    'order' => $order,
    'item_id' => $item_id,
    'item' => $item,
    'product' => apply_filters( 'woocommerce_order_item_product', $item->get_product( $item ), $item ),
    'show_purchase_note' => $show_purchase_note,
    'purchase_note' => $product ? $product->get_purchase_note() : '',
    ) );
    }
?>

With

<?php foreach ( $order->get_items() as $item_id => $item ) {
    $product = $item->get_product();

    wc_get_template( 'order/order-details-item.php', array (
        'order'              => $order,
        'item_id'            => $item_id,
        'item'               => $item,
        'product'            => apply_filters( 'woocommerce_order_item_product', $item->get_product( $item ), $item ),
        'show_purchase_note' => $show_purchase_note,
        'purchase_note'      => $product ? $product->get_purchase_note() : '',
    ));
}       
?>

That should suffice



Answered By - 7uc1f3r
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing