Save WooCommerce Order details in custom table

眉间皱痕 提交于 2021-02-10 19:41:32

问题


I have created a custom database in table order-master in which I will save WooCommerce order total amount, shipping charges and token. For this, I have edited paypemts.php file, code is below:

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/canadiapharma.com/wp-config.php' );
global $woocommerce;
global $wpdb;
echo '<h3>Custom Calculation :</h3>';
$amount_2 = $woocommerce->cart->get_cart_total();
$ship_raw = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_shipping_total() ) );
$ship = $ship_raw - 3600;
echo '<strong>Shipping :</strong>';
echo $ship; 
$nano=WC()->cart->cart_contents_total;
echo '<br>';
$total_amt = $nano+$ship;
echo '<strong>Total :</strong>';
echo $total_amt;
echo '<br>';
$salt = 'dAta_EnC!=';
$token_raw = base64_encode($total_amt. $salt);
$token = preg_replace(sprintf('/=/', $salt), '', $token_raw);
echo '<strong>Token :</strong>';
echo $token;
$wpdb->query("INSERT INTO order_master (payment_amt, ship, token) VALUES ('$total_amt', '$ship', '$token')"  );
?>

This code works fine and stores the data to the database. But it stores the data as soon as the checkout.php page loads. So, I made some changes and created an onclick event on Place Order button in checkout.php page.

OnCLick Event:

<?php echo apply_filters( 'woocommerce_order_button_html', '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '" onclick="dbinsert()">' . esc_html( $order_button_text ) . '</button>' ); 
        ?>

dbinsert():

<script>
        function dbinsert() {
            var myKeyVals = { amount : <?php echo $total_amt; ?>., ship : <?php echo $ship; ?>., token : <?php echo $token; ?> }

            var saveData = $.ajax({
                        type: 'POST',
                        url: "savedata.php",
                        data: myKeyVals,
                        dataType: "text",
                        success: function(dbinsert) { alert("Save Complete") }
            });
            saveData.error(function() { alert("Something went wrong"); });
                    }
        </script>

onclick event executes perfectly but doesn't know why data is not going into the database. Below is my savedata.php file.

<?php
global $wpdb;
$total_amt = $_POST['amount'];
$ship = $_POST['ship'];
$token = $_POST['token'];
$stmt = $wpdb->query("INSERT INTO order_master (payment_amt, ship, token) VALUES ('$total_amt', '$ship', '$token')"  );
$stmt->bindparam('payment_amt', $total_amt);
$stmt->bindparam('ship', $ship);
$stmt->bindparam('token', $token);
if($stmt->execute())
{
  $res="Data Inserted Successfully:";
  echo json_encode($res);
}
else {
  $error="Not Inserted,Some Probelm occur.";
  echo json_encode($error);
}
?>

Please help me to fix this issue.


回答1:


Try by changing your ajax url url: "savedata.php", to url: "<?php echo admin_url('admin-ajax.php?action=save_order_data');?>" and add below code in your theme's function.php

add_action('wp_ajax_nopriv_save_order_data', 'save_order_data');
add_action('wp_ajax_save_order_data', 'save_order_data');
function save_order_data() {
     global $wpdb;
     $total_amt = $_POST['amount'];
     $ship = $_POST['ship'];
     $token = $_POST['token'];
     $stmt = $wpdb->query("INSERT INTO order_master (payment_amt, ship, token) VALUES ('$total_amt', '$ship', '$token')"  );
     $result = $wpdb->query($wpdb->prepare($stmt));

     if($result)
     {
         $res="Data Inserted Successfully:";
         echo json_encode($res);
     }
     else {
         $error="Not Inserted,Some Probelm occur.";
         echo json_encode($error);
     }
}

Updated: There is an error in var myKeyVals = { amount : <?php echo $total_amt; ?>., ship : <?php echo $ship; ?>., token : <?php echo $token; ?> }. Change your var myKeyVals as below:

var myKeyVals = { amount : "<?php echo $total_amt; ?>", 
                  ship : "<?php echo $ship; ?>", 
                  token : "<?php echo $token; ?>" }

You need to write php code in "" in javascript.

Hope this helps.



来源:https://stackoverflow.com/questions/52772903/save-woocommerce-order-details-in-custom-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!