[英]WooCommerce: Display also product variation description on cart items
[英]Make a working shortcode in product pages work also for WooCommerce cart items
我正在嘗試顯示通過自定義元字段輸入的價格的后綴文本,並通過簡碼顯示 output。
這是我的代碼:
function prefix_suffix_price_html($price){
$shortcode = do_shortcode('[shortcode]') ;
$psPrice = '';
$psPrice .= $price;
$psPrice .= '<span class="suffix">'. $shortcode . '</span>';
return $psPrice;
}
add_filter('woocommerce_get_price_html', 'prefix_suffix_price_html');
add_filter( 'woocommerce_cart_item_price', 'prefix_suffix_price_html' );
這在產品和存檔頁面上運行良好。
但是,它不適用於購物車項目。 返回一個空的跨度標簽,沒有短代碼的內容。
如果在您的簡碼 function 代碼中包含global $product;
,那么以下重新訪問的代碼應該可以工作:
add_filter( 'woocommerce_get_price_html', 'add_suffix_to_product_price_html', 10, 2 );
function add_suffix_to_product_price_html( $price, $product ){
return $price . '<span class="suffix">'. do_shortcode('[shortcode]') . '</span>';
}
add_filter( 'woocommerce_cart_item_price', 'add_suffix_to_cart_item_price_html' );
function add_suffix_to_cart_item_price_html( $price, $cart_item, $cart_item_key ){
$product = $cart_item['data'];
return $price . '<span class="suffix">'. do_shortcode('[shortcode]') . '</span>';
}
否則,您需要在您的問題中為您的簡碼提供 function 代碼,以便能夠為您提供正確的工作答案……
我現在省略了短代碼並為購物車項目解決了這個問題,如下所示:
add_filter( 'woocommerce_cart_item_price', 'add_suffix_to_cart_item_price_html' );
function add_suffix_to_cart_item_price_html( $price ){
global $product;
foreach( WC()->cart->get_cart() as $cart_item ){
$product = $cart_item['data'];
$product_id = $product->get_id();
$suffix = get_post_meta( $product->get_id(), 'CUSTOMFIELDNAME', true );
return $price . '<span class="suffix">'. $suffix . '</span>';
}
}
這篇文章進一步幫助了我: Get product custom field values as variables in WooCommerce
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.