结帐 woocommerce wordpress 中的简短说明 [英] Short Description in checkout woocommerce wordpress

查看:22
本文介绍了结帐 woocommerce wordpress 中的简短说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Woocommerce 的结帐页面中为每个产品添加简短说明?我做了大量的研究,这是我想出的最好的.

How do you add a short description for each product to the checkout page in Woocommerce? I have done tons of research and this is the best I have come up with.

<?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 中转换为字符串在这一行

Catchable fatal error: Object of class WP_Post could not be converted to string in /wp-content/plugins/woocommerce/templates/checkout/form-checkout.php at this line

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

推荐答案

过滤器 woocommerce_get_item_data 可以用于此目的.

The filter woocommerce_get_item_data can be used for that.

像这样:

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;
}

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

Note that maybe some kind of checking will be needed, e.g., make sure this filter is only called when actually viewing the Checkout page, as I don't know if it'll be called in other instances.

这篇关于结帐 woocommerce wordpress 中的简短说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆