John Davidson

php - Change PayPal email based on WooCommerce order items product tags

0 comments
Message:


I am trying to find a solution to my problem. I would like to change the PayPal email based on the WooCommerce product tag.
Example: for clothing, footwear and bags tags put the email [email protected] .....
If the tags are snow, sun, garden, then put the email [email protected] etc ...


I found this code after several searches but i can't get it to work with tags. I tried them all but failed :(. Could anyone give me the modified code for the tags? Thank you very much !!
 


// 1. Switch PayPal email for Product ID
add_filter( 'woocommerce_paypal_args' , 'switch_paypal_email_based_product', 9999, 2 );
function switch_paypal_email_based_product( $paypal_args, $order ) {
 
    foreach ( $order->get_items() as $item_id => $item ) {

        // ENTER PRODUCT ID HERE
        if ( 123456 == $item->get_product_id() ) {
            // ENTER OTHER PAYPAL EMAIL HERE
            $paypal_args['business'] = '[email protected]';
            break;
        }
    }
    return $paypal_args;
}
 
// 2. Avoid IPN Failure after switching PayPal email for Product ID
require_once WC()->plugin_path() . '/includes/gateways/paypal/includes/class-wc-gateway-paypal-ipn-handler.php';
 
class WC_Gateway_Paypal_IPN_Handler_Switch extends WC_Gateway_Paypal_IPN_Handler {
    
    protected function validate_receiver_email( $order, $receiver_email ) {
 
        if ( strcasecmp( trim( $receiver_email ), trim( $this->receiver_email ) ) !== 0 ) {
 
            // ENTER HERE SAME PAYPAL EMAIL USED ABOVE
            if ( $receiver_email != '[email protected]' ) {
 
                WC_Gateway_Paypal::log( "IPN Response is for another account: {$receiver_email}. Your email is {$this->receiver_email}" );
                $order->update_status( 'on-hold', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'woocommerce' ), $receiver_email ) );
                exit;
            }
        }
    }
}
 
new WC_Gateway_Paypal_IPN_Handler_Switch();

Update


I put it in the function.php file of the child theme, but it gives me an error in the line that I am attaching to you. Did I copy badly? :(. Did I forget something? Thank you very much !!


Error: syntax error, unexpected 'array' (T_ARRAY), expecting ')' 



if ( ! in_array( $receiver_email array('[email protected]', '[email protected]') ) {

the error he gives me in this line ... but honestly I don't know if the program that is wordpress understood if it is in this line or if you are wrong :(. It does not make me save the code for this problem ...


Error: syntax error, unexpected 'array' (T_ARRAY), expecting ')'


My Answer:
@example.com](mailto:example.com), for electronics and gadgets tags put the email [[email protected]](mailto:[email protected]).

Here is a code snippet that you can add to your theme's functions.php file to achieve this:

php
add_filter( 'woocommerce_paypal_args', 'change_paypal_email_based_on_product_tags', 10, 2 );

function change_paypal_email_based_on_product_tags( $paypal_args, $order ) {
$items = $order->get_items();

$paypal_email = '[email protected]'; // Default email

foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$product_tags = wp_get_post_terms( $product_id, 'product_tag', array( 'fields' => 'names' ) );

if ( in_array( 'clothing', $product_tags ) || in_array( 'footwear', $product_tags ) || in_array( 'bags', $product_tags ) ) {
$paypal_email = '[email protected]';
break;
} elseif ( in_array( 'electronics', $product_tags ) || in_array( 'gadgets', $product_tags ) ) {
$paypal_email = '[email protected]';
break;
}
}

$paypal_args['business'] = $paypal_email;

return $paypal_args;
}


This code snippet will check the product tags of each item in the order and change the PayPal email accordingly. You can customize the product tags and corresponding email addresses as needed.

Rate this post

3 of 5 based on 8592 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us