WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
[resolved] multiple paypal payment address based on page id (11 posts)

  1. vasilescu_anton
    Member
    Posted 1 year ago #

    I am looking for a way to change the paypal payment email address based on the page you are on. There would be a few pages each connected to their own email address. Are there any filters I could use to do this?

    Thank you,
    Anton

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

  2. Native Imaging
    Member
    Posted 1 year ago #

    I too am curious about this. I have a community site setup where multiple vendors need to sell products on the same site. Is there the possibility of using different paypal accounts to direct payments to the correct vendor based on Product Categories?
    Or is there possibly a feature in PayPal for setting up sub-merchant accounts or something of that sort?

  3. vasilescu_anton
    Member
    Posted 1 year ago #

    Luckily woocommerce has hooks in place that allow you to change the paypal arguments array and all you have to do is add the email address you want. The example below has the email address hard coded but you can feed it any email address you need:

    $paypal_args = apply_filters( 'woocommerce_paypal_args', $paypal_args );
    // Hook in
    add_filter( 'woocommerce_paypal_args' , 'custom_override_paypal_email' );
    
    // Our hooked in function is passed via the filter!
    function custom_override_paypal_email( $paypal_args ) {
    	$paypal_args['business'] = 'info@ocweb.pro';
    	print_r( $paypal_args['business'] );
    	return $paypal_args;
    }
  4. Native Imaging
    Member
    Posted 1 year ago #

    Ok. Interesting. Would this be used for page ID's, or product categories?

    Thank you for the code. Will try to implement this as soon as the Products are catalogued.

  5. vasilescu_anton
    Member
    Posted 1 year ago #

    It's all up to you at this point. The code above only replaces the email address right before the connection to paypal is made so it overrides the one set in the WooCommerce settings.

    You can definitely set it up for page ID's, categories, authors, etc.

  6. brianfeister
    Member
    Posted 1 year ago #

    @vasilescu_anton - that is wonderful - just what I was looking for :)

    I've got this feature 99% of the way there, it loops over all cart items to check if any of them have a paypal_email_override meta value, if it finds one, it will override the default email with the found meta value at the woocommerce_paypal_args. It also checks to verify that there is not more than one such override (since users might get a bit crazy with this and we don't want to screw that up.

    The only small thing that's getting me is that everything is working as expected except you'll notice at the bottom that I've done a bunch of add_action's in attempt to find the right action hook for the error reporting - basically just want to push out an error message when the cart page is first viewed if there is a conflict. The plugin tells the user "Hey, the following items send payment to different PayPal users (item A -> email A, item B -> email B), please place separate orders."

    Problem is that doing $woocommerce->add_error() is not giving me the error I'm wanting when hooked to woocommerce_after_cart_contents. I think I noticed that the woocommerce_before_cart_contents hook does not have the $woocommerce->cart->cart_contents ready for me to loop over them at that time...

    I think I'm missing something small here, possible you know the correct place to hook for such function?

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]

    /* ===================================================================
     *
     * Function to enable item-specific paypal email addresses
     *
     * ================================================================ */
    
    function find_alt_paypal_emails() {
        global $woocommerce;
        // loop over the items in the cart
        foreach ( $woocommerce->cart->cart_contents as $item ) {
            // check to see if any of them has the <code>paypal_email_override</code> meta value
            $override_email = get_post_meta($item['product_id'], 'paypal_email_override', true);
            // if there is an override val, build an array of each item name and the
            // paypal email associated with that item
            if ( ! empty( $override_email ) ) {
                $paypal_override = array(
                    'item_title' => $item['data']->post->post_title,
                    'email' => $override_email
                    );
                $paypal_overrides[] = $paypal_override;
                // error_log(print_r($paypal_overrides,true));
            }
        }
        // if there are override emails indicated
        if ( ! empty( $paypal_overrides ) ) {
            // loop through and build an error message to warn the user of multiple payees
            if ( count( $paypal_overrides ) > 1 ) {
                $error_list = '';
                foreach ( $paypal_overrides as $override ) {
                    $error_list .= $override['item_title'] . ' → ' . $override['email'] . ', ';
                }
                $error_list = rtrim($error_list, ', ');
                $woocommerce->add_error( __( 'The following items send payment to different '
                    . 'PayPal Users, please place separate orders: (' . $error_list . ')', 'woocommerce' ) );
            } else {
                return $paypal_overrides[0]['email'];
            }
        }
    }
    add_action( 'woocommerce_after_cart_contents', 'find_alt_paypal_emails' );
    add_action( 'woocommerce_after_checkout_form', 'find_alt_paypal_emails' );
    add_action( 'woocommerce_after_order_total', 'find_alt_paypal_emails' );
    
    $paypal_args = apply_filters( 'woocommerce_paypal_args', $paypal_args );
    // Hook in
    add_filter( 'woocommerce_paypal_args' , 'custom_override_paypal_email' );
    
    // Our hooked in function is passed via the filter!
    function custom_override_paypal_email( $paypal_args ) {
        $alt_email = find_alt_paypal_emails();
        error_log(print_r($alt_email,true));
        if ( empty( $alt_email ) ) {
            return $paypal_args;
        }
        $paypal_args['business'] = find_alt_paypal_emails();
        print_r( $paypal_args['business'],true );
        return $paypal_args;
    }
  7. Sebastien D
    Member
    Posted 1 year ago #

    This is what I have been looking for. Thank you for sharing.

  8. helencham
    Member
    Posted 1 year ago #

    Im a newbie to these coding, but can anyone tell me where do I make these amendments? Like which file? I just need to be able to specify which account payment should go to depending on the product/category selected. Need to be able to send payment to two different paypal account.

  9. Joyce_321
    Member
    Posted 12 months ago #

    Hello @vaselescu_anton & brian feister.

    I have an online music selling site, which would have different artists sell their songs online, by registering to us. However, i want that they would get the return in their personal paypal. Have you got any idea, how do i get that paypal done?

  10. blg425
    Member
    Posted 11 months ago #

    Can someone please help with a small tutorial on how to implement the code? Thanks

  11. Svetoslav Marinov
    Member
    Posted 9 months ago #

    Hi,

    That an interesting idea.

    Actually, I recently released a premium plugin that does a similar thing. It switches to PayPal Micropayment email if the cart total is $11 or less. I might be able to modify it so it can make the switch based on the category.

    My question is what if the user wants to order products from another category? What if Cat1 and Cat2 have different paypal addresses? Do you stop the order?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic