【发布时间】:2016-03-05 16:58:09
【问题描述】:
我有一个名为“bookings”的自定义帖子类型,它将元数据的几个字段保存到 wp_postmeta。
我有一个页面显示此自定义帖子类型的所有条目,即所有预订。
这是显示我的预订的代码:
<?php
$current_user = wp_get_current_user();
$args = array(
'post_type' => 'bookings',
'meta_query' => array(
array(
'key' => 'wedding_user',
'value' => $current_user->display_name,
'compare' => '=',
)
)
);
// Welcome
echo '<div class="user_avatar">'.get_avatar( $current_user->ID, 32 ).'</div>';
echo '<div class="user_welcome"><p>Hello '.$current_user->display_name.' <span class="user_id">(ID# '.$current_user->ID.')</span></p>';
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
if (get_post_meta( $the_query->post->ID, 'wedding_hidden_is_completed', true )==1) {
$is_complete = 'Form Complete <br /><span class="small">('.get_post_meta( $the_query->post->ID, 'wedding_hidden_date_completed', true ).')</span>';
} else {
$is_complete = 'Not Complete';
}
echo '<p class="booking_id">Booking ID: DISTR' . $the_query->post->ID . '</p>';
echo '<ul class="show_bookings">';
echo '<li>' . get_post_meta( $the_query->post->ID, 'wedding_name', true ) . '</li>';
echo '<li>' . get_post_meta( $the_query->post->ID, 'wedding_date', true ) . '</li>';
echo '<li>' . get_post_meta( $the_query->post->ID, 'wedding_package', true ) . '</li>';
echo '<li>£' . get_post_meta( $the_query->post->ID, 'wedding_price', true ). '</li>';
echo '<li>' . get_post_meta( $the_query->post->ID, 'wedding_payment_due_date', true ) . '</li>';
echo '<li>' . $is_complete . '</li>';
echo '<li>Is Viewed?</li>';
echo '<li>Actions';
echo '<ul class="actions-sub-menu">';
echo '<li><a href="'. esc_url(add_query_arg( 'booking-id', $the_query->post->ID, site_url( '/booking-form/' ) )).'">Fill out booking form</a></li>';
echo '<li><a href="'. esc_url(add_query_arg( 'booking-id', $the_query->post->ID, site_url( '/pay-deposit/' ) )) .'">Pay deposit</a></li>';
echo '<li>Pay remaining balance</li>';
echo '<li>Email a copy of your receipt</li>';
echo '<li>View booking form</li>';
echo '</ul>';
echo '</li> <!--end actions-sub-menu-->';
echo '</ul>';
?>
<div class="pay_deposit">
<?php
require DISTRACTIONS_LOGIN_PLUGIN_DIR . 'includes/payments/deposit.php';
?>
</div> <!-- end pay deposit -->
<?php
}
} else {
echo '<p>No bookings found</p>';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
每个预订下方都有一个 paypal 付款按钮 - 它使用 paypal IPN,所以没什么太复杂的(您将在“pay_deposit”div 中看到所需的文件)。
这是我的贝宝付款方式:
<?php
$paypal_url='https://www.sandbox.paypal.com/cgi-bin'; //
$paypal_id='malik@thedistractionsband.co.uk'; // Business email ID
$booking_form_id = $the_query->post->ID;
$total_amount = get_post_meta( $the_query->post->ID, 'wedding_price', true );
$deposit_amount = $total_amount*0.2;
?>
<h3>Pay Deposit</h3>
<p>Your deposit aount of £<?php echo $deposit_amount; ?> ...</p>
<form action="<?php echo $paypal_url; ?>" method="post" name="frmPayPal1">
<input type="hidden" name="business" value="<?php echo $paypal_id; ?>">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="item_name" value="<?php echo get_post_meta( $the_query->post->ID, 'wedding_name', true ); ?> - 20% Deposit">
<input type="hidden" name="item_number" value="DISTR<?php echo $booking_form_id; ?>">
<input type="hidden" name="credits" value="510">
<input type="hidden" name="userid" value="<?php echo $current_user->ID; ?>">
<input type="hidden" name="amount" value="<?php echo $deposit_amount; ?>">
<input type="hidden" name="cpp_header_image" value="http://www.thedistractionsband.co.uk/files/2015/08/LOGO-1.1-1024x304.png">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="handling" value="0">
<input type="hidden" name="cancel_return" value="<?php echo get_site_url()."/payment-cancel/"; ?>">
<input type="hidden" name="return" value="<?php echo get_site_url()."/my-bookings/"; ?>">
<input name="notify_url" value="<?php echo DISTRACTIONS_LOGIN_PLUGIN_URL ?>includes/payments/paypal-ipn.php" type="hidden">
<input type="submit" border="0" name="submit" value="Pay Now" alt="PayPal - The safer, easier way to pay online!">
<div class="cards"><i class="fa fa-cc-amex"></i> <i class="fa fa-cc-mastercard"></i> <i class="fa fa-cc-visa"></i> <i class="fa fa-credit-card"></i> <i class="fa fa-cc-paypal"></i></div>
</form>
付款后,它使用以下文件与 paypal 通信并更新我的数据库 - paypal-ipn.php
<?php
// Make wordpress functions available - so we can write to the db etc
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );
global $wpdb;
update_post_meta($booking_id, 'deposit_paid', 1);
?>
我的问题在这个文件中。 update_post_meta 函数用于将“1”添加到我的 wp_postmeta 数据库表中的 deposit_paid 列。我遇到的问题是,如何定义 $booking_id - 确保它是用于已付款的预订。
例如,在我列出所有预订的页面上,如果第一个预订的 post_id 为 10 并且我为此付费,我如何确保 post_id 10 的“deposit_paid”条目已更新?
【问题讨论】: