Support » Plugin: WooCommerce » Is it bad practice to mess with a protected function get_paypal_args?

  • Hi Folks,

    I’m starting to get the hang of working with child themes, but have yet to deal with ‘protected’ functions. The function I’m referring to in this case is in the class-wc-gateway-paypal-request.php file in woocommerce.

    I was going to try and do something fairly simple, but wanted to makes ure it wasn’t going to screw up something. I wanted to make a slight change to the custom fields that were being transmitted. Here’s the function as it stands now:

    protected function get_paypal_args( $order ) {...
    'custom'        => json_encode( array( 'order_id' => $order->id, 'order_key' => $order->order_key ) ),
    ...}

    My plan is to copy the entire protected function over to my functions.php file, and make a small change to the ‘custom’ field listed above. Here’s what I’m planning on doing:

    protected function get_paypal_args( $order ) {...
    'custom'        => json_encode( array( 'order_id' => $order->id, 'order_key' => $order->order_key, 'rider_id' => $billing->fundraiser ) ),
    ...}

    For whatever reason, I’ve never been able to wrap my mind around the arrows…it’s like they’re there just to confuse me. I mean, isn’t $order->id the same as $order[‘id’]? It makes me question whether the $billing->fundraiser is calling the correct field.

    For reference, here is the custom field I created specifically to track the fundraiser on all purchases through woocommerce:

    $fields['billing']['fundraiser'] = array(
            'label'     => __('Fundraiser', 'woocommerce'),
            'placeholder'   => _x('fundraiser', 'placeholder', 'woocommerce'),
            'required'  => true,
            'class'     => array('form-row-wide'),
            'type' => 'select',
            'options' => $participants
        );
        return $fields;

    The fundraiser field displays correctly. I just want the fundraiser custom field information included in the paypal IPN’s so I can update my own databases and track rider fundraising activities.

    If I’m doing this wrong, I really appreciate any advice 🙂

    https://wordpress.org/plugins/woocommerce/

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Support Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Hi jdlev,

    You can’t override a protected function like that. Protected methods can only be accessed in the class itself and by inherited classes. http://php.net/manual/en/language.oop5.visibility.php

    You can use this filter though: https://github.com/woothemes/woocommerce/blob/master/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php#L65. Here is a helpful article on how to use filters: https://pippinsplugins.com/a-quick-introduction-to-using-filters/

    I’m not completely sure if $billing->fundraiser is going to work for you though. If it doesn’t, you should be able to get your custom field value using get_post_meta: https://developer.wordpress.org/reference/functions/get_post_meta/. Where the $post_id is the order ID, and the $key is fundraiser.

    Perfect. I’ll check it out. Thanks for the quick response Caleb! 🙂

    So read the tutorial (great article btw), and it really helped me understand filters better. Here’s the steps I’ve taken so far in my functions file:

    1) I added a filter:
    add_filter( 'get_paypal_args', 'add_custom_field_element_to_paypal');

    2) This is a bit trickier. I added the custom function to my functions.php file, but am not sure how to reference the ‘custom’ field since it’s an array within an array and has json_encoding. So how should I add another key value pair to the array:

    'custom' => json_encode( array( 'order_id' => $order->id, 'order_key' => $order->order_key, 'new_key' => $order->new_value ) )

    While we’re discussing things…do you have an elementary way to remember the function of the -> and => operators? My mind just goes blank whenever I see one of those dumb things lol.

    Plugin Author Mike Jolley

    (@mikejolley)

    'custom' => json_encode( array( 'order_id' => $order->id, 'order_key' => $order->order_key, 'new_key' => $order->new_value, 'someething-else' => 'something-else-value' ) )

    I’ll give it a shot. Thanks again Mike and Caleb 🙂

    Hey mike, one other quick question. What do you call the things like $order? Is that a class that is produced by woocommerce, and all a class is, is a shell of sorts for holding variables and functions?

    So I tried the following, and haven’t been able to add the variable to the custom field. Not sure where I’m going wrong as I tried replacing the $order->fundraiser with just a standard string to see if I could get it to at least pass some value, and still nothing. Here’s the full code:

    add_filter( 'get_paypal_args', 'add_custom_field_element_to_paypal');

    function add_custom_field_element_to_paypal()
    {
        return apply_filters('woocommerce_paypal_args', array_merge(
            array(
                'custom' => json_encode(
                    array(
                        'order_id' => $order->id, 'order_key' => $order->order_key, 'fundraiser' => $order->fundraiser)))));
    
    }
    Plugin Author Mike Jolley

    (@mikejolley)

    You’re not using the filter right. I suggest you re-read up on how filters work. You don’t need to re-run the filter on the values you modify for a start.

    I’ve been working to understand the example Caleb put forth…for whatever reason, it’s like reading greek to me, but I’ll keep trying.

    Let me ask you this. I noticed in the example, the add_filter hook is called after the function. Does the order of when the hook & when the function are called matter? Also, would I just use array_merge to add the last key-value pair to the array?

    I’m a bit confused because in the other example, you bring in another array of fruit as a parameter of the function. Should I be including the object/class/or whatever you call it that represents the paypal args which looks to be….$order? Ha! Maybe we’ve come full circle and are back to $order now 🙂

    Plugin Author Mike Jolley

    (@mikejolley)

    Let me just give you a very basic example and you can work out the rest.

    add_filter( 'woocommerce_paypal_args', 'jdlev_woocommerce_paypal_args', 10, 2 );
    
    function jdlev_woocommerce_paypal_args( $args, $order ) {
    // do something with $args
    return $args;
    }

    Thanks for stickin with me on this Mike, but it’s just not clicking for some reason…probably because I’ve been up all night. Anyways, I appreciate your patience because this seems like some pretty basic stuff I should get no problem. I’ll give in another go after a little siesta, and let you know how it turned out. Thanks again 🙂

    …and the trek continues 🙂 So Mike, I think I have a handle on what the filter does, but the function’s variables are throwing me for a loop (Ha! ‘loop’…that’s a wp joke! :D)

    If I understand add_filter correctly, add_filter acts on a preexisting hook titled ‘woocommerce_paypal_args’. The jdlev_woocommerce_paypal_args is just the name of the custom function to use to act on the information in the w_p_a hook. 10 is the priority and 2 is the number of arguments to pass into the j_w_p_a function. Both the filter and function belong in my functions.php file located in my child theme folder. The order that the filter and function are in, in the functions.php file doesn’t matter (the code doesn’t execute sequentially is basically what I’m asking on the functions.php file)

    I’ve been trying to read up on the 2 variables you are passing the function, but haven’t been able to find much info. What are the $args and $order variables, because I’m not sure if they’re arrays, classes, or objects? Can you send me a link to read up on those items? $order is just such a common phrase I’m having trouble finding more information on it.

    Another thing that I might be wrong about, but don’t you have to return $order when you’re done? The only thing I see returned in the example is the $args?

    Plugin Author Mike Jolley

    (@mikejolley)

    So $args is the array of variables you’re filtering which we pass to the filter in core.

    $order is an order object which we also pass to the filter.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Is it bad practice to mess with a protected function get_paypal_args?’ is closed to new replies.