• Resolved pl.xrisa

    (@xrisa30)


    Hi,

    I have a code over 2 years now to restrict COD payments for orders that the cart total is above 200€. The code working very good but the last days I see a bug in my debug page that says “Notice: Trying to get property ‘total’ of non-object”

    Is there any change to woocommerce code files and now I must edit my code too?
    I’m searching but I can’t find something.

    I’m appriciate it if your can help to find what’s going wrong all of a sudden.

    My code is:

    function xr_limit_cod_gateway( $available_gateways ) {
    	global $woocommerce;
    	$cart_total = $woocommerce->cart->total;
    	if ( isset( $available_gateways['cod'] ) && $cart_total > 200) {
    		unset(  $available_gateways['cod'] );
    	}
    	return $available_gateways;
    }
    add_filter( 'woocommerce_available_payment_gateways', 'xr_limit_cod_gateway' );

    Thank you
    Xrisa

    • This topic was modified 2 years, 9 months ago by pl.xrisa.
    • This topic was modified 2 years, 9 months ago by pl.xrisa.
    • This topic was modified 2 years, 9 months ago by pl.xrisa.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Daniyal Ahmed (a11n)

    (@daniyalahmedk)

    Hi there,

    Thanks for reaching out.

    We usually don’t provide support for custom snippets, but I’ll make an exception here as it’s relatively a quick one to add. Can you please try to replace the previous code with the following one:

    add_filter( 'woocommerce_available_payment_gateways', 'show_hide_payment_methods' );
    function show_hide_payment_methods( $available_gateways ) {
    
        if ( WC()->cart->total > 200 ) {
            if ( isset($available_gateways['cod']) ) {
                unset($available_gateways['cod']);
            }
        }
    
        return $available_gateways;
    }

    Let me know how it goes!

    Best,

    Thread Starter pl.xrisa

    (@xrisa30)

    Hi,

    Thank you very much for your help and for how fast you answered to my issue.

    Unfortunately, the error remains. The line with the error in your code is this:

    if ( WC()->cart->total > 200 ) {

    And in my code is this:

    $cart_total = $woocommerce->cart->total;

    That’s why I thought that maybe something changed in your files for the “total” property.

    I’m searching all day in woocommerce folders but I can’t find something.

    Hello @xrisa30!

    I tested the code that Daniyal shared before and found it to be working properly.

    I suggest you do a conflict test to find whether the issue is coming from your theme/other plugin/other custom codes.

    You can find a more detailed explanation of how to do a conflict test here. → https://docs.woocommerce.com/document/how-to-test-for-conflicts/

    Before you start
    Please make sure you have a good backup in place of your full site and database. Most hosting companies have this included in the subscription, but you could also consider using a service like Jetpack. If something goes wrong, it’s nice to know that you can restore your site.

    Let us know how it goes.

    Thread Starter pl.xrisa

    (@xrisa30)

    Hi guys,

    Both codes (mine and Daniyal’s) working fine but the error in the logs remains. The thing is that this error probably cause another error.

    The last times I tried to update WC it doesn’t let the database to be updated and it caused the site to get down.
    Every time I have to remove this code in note.php

    if ( empty( $label ) ) {
    $this->error( 'admin_note_invalid_data', __( 'The admin note action label prop cannot be empty.', 'woocommerce' ) );
    }

    After this I update again the database and put again the code back.

    I tried in my localhost to do some debug and it seems that this error is from wpocean theme.

    So, I will let them know.

    Thanks both of you for your help.
    Xrisa

    Hi @xrisa30!

    I am happy to hear that you figured it out, let us know how it goes!

    All the best,

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Error in property’ is closed to new replies.