以下代码有效,并将购买备注添加到购物车项目中。
add_filter( 'woocommerce_get_item_data', 'wc_test', 10, 2 );
function wc_test ( $other_data, $cart_item ){
$post_data = get_post( $cart_item['product_id'] );
$other_data[] = array( 'name' => $post_data->_purchase_note );
return $other_data;
}
但是,对于没有注释的产品,我总是得到“:”结果。
我还需要在注释下添加特定属性。
有什么建议吗?
所以你得到:
// Display on cart & checkout pages
function filter_woocommerce_get_item_data( $item_data, $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Get purchase_note
$purchase_note = $product->get_purchase_note();
// Get the product attribute value (adjust if desired)
$attribute = $product->get_attribute( 'pa_color' );
// When empty
if ( empty ( $attribute ) ) {
$attribute = '';
}
// NOT empty
if ( ! empty( $purchase_note ) ) {
$item_data[] = array(
'key' => __( 'Note', 'woocommerce' ),
'value' => $purchase_note . $attribute,
'display' => $purchase_note . $attribute,
);
}
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 10, 2 );