Wordpress如何在循环之外获取帖子ID

时间:2015-12-01 17:09:34

标签: php wordpress paypal

我有一个名为'预订的自定义帖子类型'这会将几个元数据字段保存到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,所以没有太复杂(你会看到&#34; pay_deposit&#34; div中所需的文件)。

这是我的PayPal付款表格:

<?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函数用于添加&#39; 1&#39;到我的wp_postmeta db表中的deposit_paid列。我遇到的问题是,如何定义$ booking_id - 确保它是为已付款的预订。

例如,在我列出我所有预订的页面上,如果第一个有post_id 10并且我为此付费,我该如何确保&#39; deposit_paid&#39; post_id 10的条目是否已更新?

1 个答案:

答案 0 :(得分:0)

想出来,我需要使用:

$booking_id = $_POST['item_number'];

将项目编号发送到paypal(与帖子ID相同)