在商品购物车数据之后显示 Woocommerce 购物车中的产品 ACF 自定义字段

时间:2021-06-24 06:14:34

标签: wordpress woocommerce advanced-custom-fields

我在每个产品中都有一个名为“product_acf_description”的 ACF 元自定义字段

我希望在商品名称和数据之后将其显示在购物车中

我应该使用什么代码?

我发现以下代码来自:Add product description to cart items in Woocommerce

它以正确的位置和格式显示描述而不是自定义字段

add_filter( 'woocommerce_get_item_data', 'customizing_cart_item_data', 10, 2 );
function customizing_cart_item_data( $cart_data, $cart_item ) {
    $description = $cart_item['data']->get_description(); // Get the product description

    // For product variations when description is empty
    if( $cart_item['variation_id'] > 0 && empty( $description ) ){
        // Get the parent variable product object
        $parent_product = wc_get_product( $cart_item['product_id'] );
        // Get the variable product description
        $description = $parent_product->get_description();
    }

    // If product or variation description exists we display it
    if( ! empty( $description ) ){
        $cart_data[] = array(
            'key'      => __( 'Description', 'woocommerce' ),
            'value'    => $description,
            'display'  => $description,
        );
    }
    return $cart_data;
}

0 个答案:

没有答案