У меня есть короткий код, который отображает эту цену. Это также работает, например, в описании продукта.
Шорткод: [deposit_amount]
Однако, если я хочу отобразить это под фактической ценой продукта, это вызовет «На этом сайте произошла критическая ошибка».
Что я делаю не так? - Мой код в файле function.php
Код: Выделить всё
function cw_change_product_price_display( $price ) {
global $post, $product;
if(get_post_meta( $post->ID, '_custom_star_after_price', true )){
$cstar = get_post_meta( $post->ID, '_custom_star_after_price', true );
}
if(get_post_meta( $post->ID, '_custom_depositprice', true )){
$txt1deposit = get_post_meta( $post->ID, '_txt_before_deposit', true );
$depositprice = get_post_meta( $post->ID, '_custom_depositprice', true );
$txt2deposit = get_post_meta( $post->ID, '_txt_after_deposit', true );
}
$price .= '' . $cstar . ' + [url=/shipping]Shipping[/url]
' . $txt1deposit . do_shortcode("[deposit_amount]") . $txt2deposit . '';
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
Код: Выделить всё
if( ! function_exists('get_custom_deposit_product_price') ) {
function get_custom_deposit_product_price( $atts ) {
// Attributes
$atts = shortcode_atts( array(
'id' => '0',
), $atts, 'deposit_amount' );
global $product, $post;
if( ! is_object( $product ) || $atts['id'] != 0 ){
if( is_object( $post ) && $atts['id'] == 0 )
$product_id = $post->ID;
else
$product_id = $atts['id'];
$product = wc_get_product( $product_id );
}
if( is_object( $product ) ){
$price_excl_tax = wc_get_price_excluding_tax($product);
$price_deposit = get_post_meta( $post->ID, '_custom_depositprice', true );
$deposit_amount = $price_excl_tax - $price_deposit;
return wc_price($deposit_amount);
}
}
add_shortcode( 'deposit_amount', 'get_custom_deposit_product_price' );
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... t-page-cau