• Hello WC Lovers

    I am working in a personal project, and your plugin is just my savior.

    Although your plugin is very good, not to say perfect, I would like to add a custom payment method.

    This, instead of adding to the cart, would place an order immediately, obtaining the information of the buyer, as well as the seller, sharing it with both and the administrator. This is to copy the ease of purchases from an online sales platform known as MercadoLibre.

    However, I manage to get the information from both, manage it through ajax, and send an email to both the seller and the administrator.
    However, the purchase order is only placed in WooCommerce, in the backend, and does not appear in the corresponding seller control panel.
    I have come to the conclusion that I need to manage certain variable data, however, after 3 months of days of research in the plugin files, trial and error, I cannot get it to work.

    I did not want to resort to you as I am sure you have more important things to attend to, but my project cannot wait any longer.

    I provide you with the code I am currently working with. I would greatly appreciate a suggestion.

    function wcfmmp_display_seller_info_shortcode() {
    global $WCFM, $WCFMmp, $post;
    $product = wc_get_product();

    if(!$post ) return;
    if(!wcfm_is_vendor( $post->post_author ) ) return;

    $store_user = wcfmmp_get_store( $post->post_author );
    $store_info = $store_user->get_shop_info();

    $seller_username = $store_user->get_name();
    $seller_email = $store_user->get_email();
    $seller_phone = $store_user->get_phone();
    // $seller_phone = get_user_meta( $store_user->get_id(), '_wcfm_store_phone', true

    // buyer info
    $buyer_name = wp_get_current_user()->display_name;
    $buyer_email = wp_get_current_user()->user_email;

    $current_user = wp_get_current_user();
    $buyer_phone = '';
    $seller_buy_phone = '';
    if( wcfm_is_vendor( $current_user->ID ) ) {
    $store_buyer_user = wcfmmp_get_store( $current_user->ID );
    $store_buyer_info = $store_buyer_user->get_shop_info();

    $seller_buy_phone = $store_buyer_user->get_phone();
    }
    $buyer_phone = $seller_buy_phone;

    $order_current_user_id = $current_user->ID;
    $actual_product = wc_get_product();
    $actual_product_id = $actual_product->get_id();
    $actual_product_title = $actual_product->get_title();
    $actual_product_price = $actual_product->get_price();
    $actual_product_comission = $actual_product_price * 0.05;
    $formatted_actual_product_commission = number_format($actual_product_comission, 2, ',', '.');

    ob_start(); // Start output buffering
    ?>
    <style>
    .wcfmmp-seller-info {
    display: none;
    background-color: #c4ffcb;
    margin-top: 10px;
    padding: 10px;
    box-shadow: 0px 0px 2px #000000;
    margin-bottom: 10px;
    border-bottom-right-radius: 7px;
    border-top-right-radius: 7px;
    border-bottom-left-radius: 7px;
    }
    .wcfmmp-confirm {
    display: none;
    }
    </style>
    <button class="wcfmmp-buy-btn" style="margin: 10px; background-color: #0484FB;">Buy Now</button>
    <div class="wcfmmp-confirm" style="margin: 10px;">
    <p>Seguro que deseas comprar este producto?</p>
    <button class="wcfmmp-buy-confirm" style="background-color: #0484FB;">Buy</button>
    <button class="wcfmmp-cancel" style="background-color: #000000; margin-left: 10px;">Cancel</button>
    </div>
    <div class="wcfmmp-seller-info">
    <p><strong>Operacion exitosa. Utiliza estos datos para contactar al vendedor y finalizar tu compra</strong></p>
    <p><strong><?php _e( 'Seller:', 'wc-multivendor-marketplace' );?></strong> <?php echo $seller_username;?></p>
    <p><strong><?php _e( 'Email:', 'wc-multivendor-marketplace' );?></strong> <?php echo $seller_email;?></p>
    <p><strong><?php _e( 'Phone:', 'wc-multivendor-marketplace' );?></strong> <?php echo $seller_phone;?></p>
    </div>
    <?php
    $output = ob_get_clean(); // Get the buffered output

    // Add a script to show/hide the seller info with confirmation
    ?>
    <script>
    var ajaxurl = '<?php echo admin_url('admin-ajax.php');?>';

    document.addEventListener("DOMContentLoaded", function() {
    document.querySelector('.wcfmmp-buy-btn').addEventListener('click', function() {
    this.style.display = 'none';
    var confirmElement = document.querySelector('.wcfmmp-confirm');
    confirmElement.style.display = 'block';
    });

    document.querySelector('.wcfmmp-buy-confirm').addEventListener('click', function() {
    var sellerInfo = document.querySelector('.wcfmmp-seller-info');
    sellerInfo.style.display = 'block';
    this.parentNode.style.display = 'none';
    sendEmail('<?php echo $seller_username;?>', '<?php echo $seller_email;?>', '<?php echo $seller_phone;?>', '<?php echo $buyer_name;?>', '<?php echo $buyer_email;?>', '<?php echo $buyer_phone;?>', '<?php echo $actual_product_id;?>', '<?php echo $actual_product_title;?>', '<?php echo $actual_product_price;?>', '<?php echo $formatted_actual_product_commission;?>');
    createOrder('<?php echo $actual_product_id;?>', '<?php echo $order_current_user_id;?>', '<?php echo $buyer_phone;?>');
    });

    document.querySelector('.wcfmmp-cancel').addEventListener('click', function() {
    var confirmElement = document.querySelector('.wcfmmp-confirm');
    confirmElement.style.display = 'none';
    var buyButton = document.querySelector('.wcfmmp-buy-btn');
    buyButton.style.display = 'block';
    });
    });

    function sendEmail(sellerUsername, sellerEmail, sellerPhone, buyerName, buyerEmail, buyerPhone, productId, productTitle, productPrice, productComission) {
    jQuery.post(ajaxurl, {
    'action': 'send_email_to_admin',
    'seller_username': sellerUsername,
    'seller_email': sellerEmail,
    'seller_phone': sellerPhone,
    'buyer_name': buyerName,
    'buyer_email': buyerEmail,
    'buyer_phone': buyerPhone,
    'actual_product_id': productId,
    'actual_product_title': productTitle,
    'actual_product_price': productPrice,
    'formatted_actual_product_commission': productComission
    });
    }

    function createOrder(productId, buyerId, buyerPhone){
    jQuery.post(ajaxurl, {
    'action': "create_order",
    'actual_product_id': productId,
    'buyerId': buyerId,
    'buyer_phone': buyerPhone
    });
    }
    </script>

    <?php
    return $output;
    }

    add_shortcode( 'wcfmmp_display_seller_info', 'wcfmmp_display_seller_info_shortcode' );

    // Add an AJAX handler to send the email
    add_action('wp_ajax_send_email_to_admin', 'send_email_to_admin_callback');
    add_action('wp_ajax_nopriv_send_email_to_admin', 'send_email_to_admin_callback');

    function send_email_to_admin_callback() {
    // seller information
    $seller_username = $_POST['seller_username'];
    $seller_email = $_POST['seller_email'];
    $seller_phone = $_POST['seller_phone'];

    // buyer information
    $buyer_name = $_POST['buyer_name'];
    $buyer_email = $_POST['buyer_email'];
    $buyer_phone = $_POST['buyer_phone'];

    // product information
    $actual_product_id = $_POST['actual_product_id'];
    $actual_product_title = $_POST['actual_product_title'];
    $actual_product_price = $_POST['actual_product_price'];
    $formatted_actual_product_commission = $_POST['formatted_actual_product_commission'];

    //date
    date_default_timezone_set("America/Caracas");
    setlocale(LC_ALL,"es_ES@euro","es_ES","esp");
    $current_date_time = date('d-m-Y g:i:s A');

    // send email to admin
    $admin_email = '[email protected]';
    $subject = 'Buy Confirmation from Seller: '. $seller_username;
    $message = 'A buyer has confirmed to buy the product by id' . $actual_product_id . ' with product name ' . $actual_product_title . ' por el total de ' . $actual_product_price . ' con una comision de ' . $formatted_actual_product_commission . ' from seller '. $seller_username. ' with email '. $seller_email. ' and phone '. $seller_phone. '. The buyer\'s information is: '. $buyer_name. ' with email '. $buyer_email. ' and phone '. $buyer_phone . 'on' . $current_date_time;
    wp_mail($admin_email, $subject, $message);

    //send email to seller
    $to_seller = $seller_email;
    $seller_subject = 'Buy confirmation! Client: ' . $buyer_name;
    $seller_message = 'The buyer ' . $buyer_name . ' con el correo electronico ' . $buyer_email . ' y el numero de telefono ' . $buyer_phone . ' ha comprado tu producto ' . $actual_product_title . 'por el total de ' . $actual_product_price . '. La comision actual es del 5%, acabando asi en ' . $formatted_actual_product_commission . '. Compra realizada el ' . $current_date_time;
    wp_mail( $to_seller, $seller_subject, $seller_message);
    wp_die();
    }

    add_action( 'wp_ajax_create_order', 'create_order' );
    function create_order() {
    $buyer_phone = $_POST['buyer_phone'];
    $product_id = $_POST['actual_product_id'];
    $user_id = $_POST['buyerId'];
    $product = wc_get_product( $product_id );
    $current_user = wp_get_current_user();
    // Set payment method

    $order_data = array(
    'status' => 'pending',
    'customer_id' => 1, // Replace with the customer ID
    'customer_note' => 'This is a new order created by the plugin',
    'total' => 101.00, // Replace with the order total
    'payment_method' => 'paypal', // Replace with the payment method
    'payment_method_title' => 'PayPal', // Replace with the payment method title
    'set_paid' => true, // Set the order as paid
    'created_via' => 'checkout', // Set the origin of the order to "plugin"
    );

    $order = wcfmmp_create_order();

    $shipping = new WC_Order_Item_Shipping();
    $shipping->set_method_title( 'Free shipping' );
    $shipping->set_method_id( 'free_shipping:1' ); //set an existing Shipping method ID
    $shipping->set_total( 0 ); // optional
    $order->add_item( $shipping );

    $address = array(
    'first_name' => 'Misha',
    'last_name' => 'Rudrastyh',
    'company' => 'rudrastyh.com',
    'email' => '[email protected]',
    'phone' => '+995-123-4567',
    'address_1' => '29 Kote Marjanishvili St',
    'address_2' => '',
    'city' => 'Tbilisi',
    'state' => '',
    'postcode' => '0108',
    'country' => 'GE'
    );
    $order->add_product( $product, 1 );
    $order->set_customer_id( $user_id );
    $order->set_customer_ip_address( $_SERVER['REMOTE_ADDR'] );
    $order->set_customer_user_agent( $_SERVER['HTTP_USER_AGENT'] );
    $order->set_billing_address( $address );
    $order->set_shipping_address( $address );
    $order->set_payment_method( 'stripe' );
    $order->set_payment_method_title( 'Credit/Debit card' );
    $order->set_status( 'waiting', 'Order is created programmatically' );
    $order->update_status( 'waiting' );
    $order->set_created_via();
    $order->calculate_totals();
    $note = "This order was created through the 'Comenzar compra' button.";
    $order->add_order_note( $note );
    $order->save();

    return $order->get_id();
    }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.