【问题标题】:Short Description in checkout woocommerce wordpress结帐 woocommerce wordpress 中的简短说明
【发布时间】:2013-02-14 04:42:53
【问题描述】:

如何在 Woocommerce 的结帐页面中为每个产品添加简短描述?我做了很多研究,这是我想出的最好的。

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

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    过滤器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;
    }
    

    请注意,可能需要进行某种检查,例如,确保仅在实际查看结帐页面时调用此过滤器,因为我不知道在其他情况下是否会调用它。

    【讨论】:

    • 谢谢。我该如何让这个功能发挥作用。我把它放在 class-wc-cart.php 并把 在我希望它出现的页面上。那是错的吗?我得到 1 的输出。
    • 抱歉,只需将其放入主题的 functions.php 文件即可。事实上,没有echo
    • 查看 this answer 了解 WooCommerce 2.2+ 更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2019-12-06
    • 2023-04-07
    • 2015-05-12
    • 1970-01-01
    • 2021-09-23
    • 2015-09-12
    相关资源
    最近更新 更多