首页 文章

如何在WooCommerce结帐页面添加简短描述

提问于
浏览
-1

感谢阅读,刚刚有关于WooCommerce的问题,我想在下面的结算字段中添加一个简短的说明结帐页面 .

如何在以下结算字段的WooCommerce结帐页面添加简短说明?

我尝试添加功能,自定义代码但失败并出现错误 .

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

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

我使用了这段代码,但它显示的是内部产品信息表 .

enter image description here

1 回答

  • 2

    没有理由打电话给 get_post() . $product 对象存储在 $cart_item 数组中, $post 对象存储在 $product 内 . 由于 woocommerce_get_item_data 过滤器出现的唯一位置在购物车类中,因此可能会在订单接收页面或我的帐户区域或电子邮件等中显示产品's excerpt (aka the short description) to show up in the cart and in the checkout. Now, it isn' t .

    需要注意的一点是,WooCommerce 2.7是对WooCommerce的重大改写, $_product->post->post_excerpt 将导致PHP通知直接访问产品属性 . 所以我建议使用2.6和2.7兼容的方法 .

    add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2 );
    
    function wc_checkout_description_so_27900033( $other_data, $cart_item )
    {
        $_product = $cart_item['data'];
        // Use this for WC2.7
        //$other_data[] = array( 'name' =>  'description', 'value' => $_product->get_short_description() );
    
        // Use this for WC2.6
        $other_data[] = array( 'name' =>  'description', 'value' => $_product->post->post_excerpt );
    
        return $other_data;
    }
    

相关问题