admin

将WooCommerce订单详细信息保存在自定义表中

sql

我已经在表order-master中创建了一个自定义数据库,其中将保存WooCommerce订单total amountshipping charges并且token。为此,我已经编辑了paypemts.php文件,代码如下:

<?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')"  );
?>

此代码可以正常工作,并将数据存储到数据库中。但是它会在checkout.php页面加载后立即存储数据。因此,我进行了一些更改,并在页面onclick上的Place Order按钮上创建了一个事件checkout.php

OnCLick事件:

<?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事件执行得很好,但是不知道为什么数据没有进入数据库。以下是我的savedata.php文件。

<?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);
}
?>

请帮助我解决此问题。


阅读 169

收藏
2021-07-01

共1个答案

admin

尝试通过将ajax网址更改url: "savedata.php",为,url: "<?php echo admin_url('admin- ajax.php?action=save_order_data');?>"并在主题的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);
     }
}

已更新:中 存在错误var myKeyVals = { amount : <?php echo $total_amt; ?>., ship : <?php echo $ship; ?>., token : <?php echo $token; ?> }var myKeyVals如下更改您的:

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

您需要使用""javascript编写php代码。

希望这可以帮助。

2021-07-01