【问题标题】:Display Brand Name Above Product Title in Woocommerce Cart, Checkout Page, Orders and Email Notification在 Woocommerce 购物车、结帐页面、订单和电子邮件通知中的产品标题上方显示品牌名称
【发布时间】:2020-04-08 02:21:09
【问题描述】:

使用以下功能,我将自定义字段应用于添加到 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;
}

【问题讨论】:

  • 您想显示从数据库中获取的值以显示在购物车、结帐和电子邮件中吗?
  • 正确,我想显示从数据库中获取的字段值,以显示在购物车、结帐、电子邮件和订单中。

标签: php wordpress woocommerce advanced-custom-fields hook-woocommerce


【解决方案1】:

我已针对您的查询尝试了此解决方案

我在后端使用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);

【讨论】:

  • 谢谢@AddWeb 解决方案!我尝试了上面的代码,但不幸的是没有成功
  • 您能否详细说明您尝试解决方案的方式,以便可以建议您或帮助您
猜你喜欢
  • 2019-08-27
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
  • 2016-09-04
  • 2018-06-03
  • 1970-01-01
  • 2018-06-17
  • 2019-04-16
相关资源
最近更新 更多