douma5954 2015-12-01 17:09
浏览 46
已采纳

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

I have a custom post type called 'bookings' that saves several fields of meta data to wp_postmeta.

I have a page that displays all the entries from this custom post type, i.e. all the bookings.

Here is the code to display my bookings:

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

?>

under each booking is a paypal payment button - it uses paypal IPN, so nothing too complex (you will see the required file in the "pay_deposit" div).

Here is my paypal payment form:

<?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>

When a payment is made, it uses the following file to communicated with paypal and update my database - 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); 

?>

My issue is in this file. The update_post_meta function is used to add a '1' to the deposit_paid column in my wp_postmeta db table. The issue I have is, how do I define the $booking_id - making sure that it is for the booking that has been paid for.

For example, on my page that lists all my bookings, if the first has post_id 10 and I pay for that, how do I make sure that the 'deposit_paid' entry for post_id 10 is updated?

  • 写回答

1条回答 默认 最新

  • drnx3715 2015-12-01 17:20
    关注

    Figured it out, I need to use:

    $booking_id = $_POST['item_number'];
    

    To get the item number sent to paypal (which is the same as the post id)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog