使用以下功能,我将自定义字段应用于Woocommerce购物车中添加的产品,并在结帐页面中将其应用于订单和通知电子邮件。
我的问题是如何在购物车,结帐等产品标题上方显示品牌。任何帮助将不胜感激。
// Display in cart and checkout pages
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // Get the WC_Product Object
if ( $value = $product->get_meta('my_custom_field') ) {
$product_name .= '<span class="custom_field_class">'.$value.'</span>';
}
return $product_name;
}
// Display in orders and email notifications
add_filter( 'woocommerce_order_item_name', 'customizing_order_item_name', 10, 2 );
function customizing_order_item_name( $product_name, $item ) {
$product = $item->get_product(); // Get the WC_Product Object
if ( $value = $product->get_meta('my_custom_field') ) {
$product_name .= '<span class="custom_field_class">'.$value.'</span>';
}
return $product_name;
}
答案 0 :(得分:0)
我已尝试针对您的查询使用此解决方案
我已经使用ACF在后端的产品页面上输入品牌名称,并在函数中使用该字段来获取品牌名称的值并在前端显示。
//Displaying custom field value in single product page
add_action( 'woocommerce_single_product_summary', 'add_custom_field', 0 );
function add_custom_field() {
global $product; // Changed this
// Added this too (compatibility with WC +3)
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
echo "<div class='produto-informacoes-complementares'>";
echo get_field( 'brand_name', $product_id );
echo "</div>";
return true;
}
// Storing this custom field into cart and session:
add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_product_field', 10, 2 );
function save_my_custom_product_field( $cart_item_data, $product_id ) {
$custom_field_value = get_field( 'brand_name', $product_id, true );
if( !empty( $custom_field_value ) )
{
$cart_item_data['brand_name'] = $custom_field_value;
// below statement make sure every add to cart action as unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
//Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
$custom_items = array();
// Woo 2.4.2 updates
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['brand_name'] ) ) {
$custom_items[] = array( "name" => "Brand Name", "value" => $cart_item['brand_name'] );
}
return $custom_items;
}
//Display Filed Value on Mail tamplate
function add_order_item_meta_acf( $item_id, $values ) {
wc_add_order_item_meta( $item_id, 'Brand Name', $values [ 'brand_name' ] );
}
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta_acf' , 10, 2);