2013-02-28 83 views
3

如何在Woocommerce的結賬頁面添加每個產品的簡短描述?我已經完成了大量的研究,這是我所想到的最好的。結帳時的簡短描述woocommerce wordpress

<?php 
if (sizeof($woocommerce->cart->get_cart())>0) : 
    foreach ($woocommerce->cart->get_cart() as $item_id => $values) : 
     $_product = $values['data']; 
    if ($_product->exists() && $values['quantity']>0) : 
     echo ' 
    <tr class = "' . esc_attr(apply_filters('woocommerce_checkout_table_item_class', 'checkout_table_item', $values, $item_id)) . '"> 
    <td class="product-name">'.$_product->get_title().$woocommerce->cart->get_item_data($values).'</td> 
    <td class="short_description">'.$_product->get_post_data().$woocommerce->post->get_post_excerpt($values).'</td> 

    <td class="product-quantity">'.$values['quantity'].'</td> 

    <td class="basispreis">'.$_product->get_price().$woocommerce->post->get_post_excerpt($values).'</td> 

    <td class="product-total">' . apply_filters('woocommerce_checkout_item_subtotal', $woocommerce->cart->get_product_subtotal($_product, $values['quantity']), $values, $item_id) . '</td> 
    </tr>'; 
    endif; 
    endforeach; 
    endif; 
    do_action('woocommerce_cart_contents_review_order'); 
?> 

我得到的錯誤

開捕致命錯誤:類對象WP_Post不能在被轉換爲字符串/wp-content/plugins/woocommerce/templates/checkout/form-checkout.php 這條線

<td class="short_description">'.$_product->get_post_data().$woocommerce->post->get_post_excerpt($values).'</td> 

回答

7

該過濾器woocommerce_get_item_data可用於此。

像這樣:

add_filter('woocommerce_get_item_data', 'wc_checkout_description_so_15127954', 10, 2); 

function wc_checkout_description_so_15127954($other_data, $cart_item) 
{ 
    $post_data = get_post($cart_item['product_id']); 
    $other_data[] = array('name' => $post_data->post_excerpt); 
    return $other_data; 
} 

注意,也許會需要某種形式的檢查,例如,確保實際觀看的結帳頁面時,此過濾器纔會被調用,因爲我不知道這是否是在其他情況下會被調用。

+0

謝謝。我如何着手讓這個功能起作用。我把它放在class-wc-cart.php中,並把<?php echo add_filter('woocommerce_get_item_data','wc_checkout_description_so_15127954',10,2); ?>我想讓它出現在頁面上。那是錯的嗎?我得到的輸出爲1. – SilverNightaFall 2013-02-28 20:33:46

+0

對不起,只需將它放在主題的'functions.php'文件中即可。事實上,沒有'回聲'。 – brasofilo 2013-02-28 22:40:35

+2

請參閱[此答案](http://stackoverflow.com/a/27918859/383847)WooCommerce 2.2+更新。 – helgatheviking 2015-01-13 09:34:04

相關問題