WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
[resolved] Custom function in child theme (3 posts)

  1. kyunomi
    Member
    Posted 3 months ago #

    Hi,

    I was trying to create a custom function in a child theme that i created.

    <?php
    
    add_action('woocommerce_order_details_after_order_table', 'My_additional_shipping_info');
    
    function My_additional_shipping_info() {
    	if ( $totals = $order->get_order_item_totals() ) {
    		if ($totals['shipping']['value']=="Self-Pickup @ Raffles Place MRT") {
    			echo 'Self-Pickup: ';
    		} else {
    			echo 'Delivery: ';
    		}
    	}
    	if ( $delivery_time = get_post_meta( $order->id, '_delivery_time', true ) ) {
    		echo "{$delivery_time}"; }
    }
    ?>

    However, after i wrote it, the following error will occur
    Fatal error: Call to a member function get_order_item_totals() on a non-object in /home/.../wp-content/themes/theme-child/functions.php

    I guess it has something to do with me trying to call upon a function from wordpress without linking to it. but i dont know how to do it as i am still learning.

    Is the $totals and $order defined within woocommerce?

    Appreciate for any assistance! Thanks!

    http://wordpress.org/plugins/woocommerce/

  2. varktech.com
    Member
    Posted 3 months ago #

    Hi,
    Lots of problems with the code... Let's start with fixing the following:

    add_action('woocommerce_order_details_after_order_table', 'My_additional_shipping_info', 10, 1);
    
    function My_additional_shipping_info($order) {

    Take a look at where in woo the action is executed, you'll see that the hook sends down $order_id as the passed argument. You can use that to some benefit in your code. If you want to time your msg to occur before woo's thankyou, you can change "10, 1" to be "-1, 1"

  3. kyunomi
    Member
    Posted 3 months ago #

    it worked! Thanks alot!

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.