2016-07-07 30 views
0

基本上,我想顯示Woocommerce thankyou.php上使用的優惠券。這裏是我添加的代碼在thankyou.php上顯示優惠券說明和ID

$coupons = $order->get_items('coupon'); 
foreach ($coupons as $item_id => $item) { 
echo "<span class='coupon-name'><b>".$item['name']."</b></span>"; 
$post = get_post($item_id); 
echo "<p class='coupon-description'>".$post->post_excerpt."</p>"; 
} 

但是,只有優惠券代碼顯示,而描述不顯示。

這裏就是我把我的代碼:

if (! defined('ABSPATH')) { 
exit; 
} 


if ($order) : ?> 



<?php if ($order->has_status('failed')) : ?> 

    <p class="woocommerce-thankyou-order-failed"><?php _e('Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.', 'woocommerce'); ?></p> 

    <p class="woocommerce-thankyou-order-failed-actions"> 
     <a href="<?php echo esc_url($order->get_checkout_payment_url()); ?>" class="button pay"><?php _e('Pay', 'woocommerce') ?></a> 
     <?php if (is_user_logged_in()) : ?> 
      <a href="<?php echo esc_url(wc_get_page_permalink('myaccount')); ?>" class="button pay"><?php _e('My Account', 'woocommerce'); ?></a> 
     <?php endif; ?> 
    </p> 

<?php else : ?> 

    <?php 
     $coupons = $order->get_items('coupon'); 
     foreach ($coupons as $item_id => $item) { 
      echo "<span class='coupon-name'><b>".$item['name']."</b></span>"; 
      $post = get_post($item_id); 
      echo "<p class='coupon-description'>".$post->post_excerpt."</p>"; 
     } 
    ?> 

    <p class="woocommerce-thankyou-order-received"><?php echo apply_filters('woocommerce_thankyou_order_received_text', __('Thank you. Your order has been received.', 'woocommerce'), $order); ?></p> 

    <ul class="woocommerce-thankyou-order-details order_details"> 
     <li class="order"> 
      <?php _e('Order Number:', 'woocommerce'); ?> 
      <strong><?php echo $order->get_order_number(); ?></strong> 
     </li> 
     <li class="date"> 
      <?php _e('Date:', 'woocommerce'); ?> 
      <strong><?php echo date_i18n(get_option('date_format'), strtotime($order->order_date)); ?></strong> 
     </li> 
     <li class="total"> 
      <?php _e('Total:', 'woocommerce'); ?> 
      <strong><?php echo $order->get_formatted_order_total(); ?></strong> 
     </li> 
     <?php if ($order->payment_method_title) : ?> 
     <li class="method"> 
      <?php _e('Payment Method:', 'woocommerce'); ?> 
      <strong><?php echo $order->payment_method_title; ?></strong> 
     </li> 
     <?php endif; ?> 
    </ul> 
    <div class="clear"></div> 

<?php endif; ?> 

<?php do_action('woocommerce_thankyou_' . $order->payment_method, $order->id); ?> 
<?php do_action('woocommerce_thankyou', $order->id); ?> 
<p class="woocommerce-thankyou-order-received"><?php echo apply_filters('woocommerce_thankyou_order_received_text', __('Thank you. Your order has been received.', 'woocommerce'), null); ?></p> 

任何人都可以請幫我這個?

+0

你能解釋一下什麼樣的描述是? –

+0

這是優惠券的描述。 – user6561526

回答

0
please use this code hope so it will work for you : 
<?php 
add_action('woocommerce_thankyou', 'apply_product_on_coupon'); 
function apply_product_on_coupon() { 
     global $woocommerce; 

     if (! empty($woocommerce->cart->applied_coupons)) { 
      $my_coupon = $woocommerce->cart->get_coupons() ; 
      foreach($my_coupon as $coupon){ 

       if ($post = get_post($coupon->id)) { 
         if (!empty($post->post_excerpt)) { 
          echo "<span class='coupon-name'><b>".$coupon->code."</b></span>"; 
          echo "<p class='coupon-description'>".$post->post_excerpt."</p>"; 
         } 
       } 
      } 
     } 
    } ?> 

如果這是不行的,然後使用優惠券簡碼插件,並調用三江源頁面上簡碼 https://wordpress.org/plugins/woocommerce-coupon-shortcodes/

<?php echo do_shortcode('[coupon_shortcode]'); ?> 
+0

對不起Raj Kumar Bhardwaj,您的代碼只有在優惠券應用於購物車時纔有效。當你進行結賬時,它不會顯示任何內容。你有任何解決方案? – user6561526

+0

你有沒有嘗試使用插件的簡碼方法? –

+0

是的,但它沒有顯示任何內容。我也需要在訂單頁面上顯示優惠券。 (我的帳戶/訂單)。你能幫我麼?我的代碼僅顯示優惠券名稱,但不顯示說明。但是,您的代碼在購物車頁面上工作(結帳前) – user6561526

0

,你可以得到這樣的

if($order->get_used_coupons()) { 

     $coupons_count = count($order->get_used_coupons()); 

     echo '<h4>' . __('Coupons used') . ' (' . $coupons_count . ')</h4>'; 

     echo '<p><strong>' . __('Coupons used') . ':</strong> '; 

     $i = 1; 
     $coupons_list = ''; 

     foreach($order->get_used_coupons() as $coupon) { 
      $coupons_list .= $coupon; 
      if($i < $coupons_count) 
       $coupons_list .= ', '; 
      $i++; 
     } 

     echo '<p><strong>Coupons used (' . $coupons_count . ') :</strong> ' . $coupons_list . '</p>'; }