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

时间:2018-10-12 05:36:24

标签: php sql ajax wordpress woocommerce

我在表order-master中创建了一个自定义数据库,其中将保存WooCommerce订单total amountshipping chargestoken。 为此,我编辑了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);
}
?>

请帮助我解决此问题。

1 个答案:

答案 0 :(得分:1)

尝试通过将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代码。

希望这会有所帮助。