如何将产品重量显示到wc_add_to_cart_message_html? [重复]

问题描述 投票:0回答:1

我正在寻找一种在成功消息中显示产品重量的解决方案。我使用以下几行,但遇到一个错误,指出您的网站存在严重错误:

add_filter( 'wc_add_to_cart_message', 'my_add_to_cart_function', 10, 2 ); 

function my_add_to_cart_function( $message, $product_id ) { 

    $message = sprintf('<span style="font-size: 12px;"> %s  has been added by to your basket.</span><br><span style="font-size: 12px;">Weight: %s </span><a href="https://example.com/cart/" class="w-100 add-to-card-pills btn mt-3">View Basket</a>', 
    get_the_title( $product_id ) ,
    get_the_weight( $product_id ) 
    
    ); 
    return $message; 
    
}

有没有办法获取$message中的权重、图片缩略图和meta_key?

wordpress woocommerce
1个回答
1
投票

使用此代码

add_filter( 'wc_add_to_cart_message', 'my_add_to_cart_function', 10, 2 ); 

function my_add_to_cart_function( $message, $product_id ) {

    $product = wc_get_product($product_id);
    

    $message = sprintf('<span style="font-size: 12px;"> %s  has been added by to your basket.</span><br><span style="font-size: 12px;">Weight: %s </span><a href="https://example.com/cart/" class="w-100 add-to-card-pills btn mt-3">View Basket</a>', 
    get_the_title( $product_id ) ,
    $product->get_weight()
    
    ); 
    return $message; 
    
}

没有 get_the_weight 这样的函数,因此您需要从产品对象中获取_weight。已测试且有效

© www.soinside.com 2019 - 2024. All rights reserved.