当变体使用报价时,在下拉列表中显示Woocommerce变体价格

时间:2018-10-10 05:38:54

标签: woocommerce variations

我已成功使用此代码在下拉列表中显示Woocommerce的变化价格,但问题是我的一些变化用引号表示长度。例如:6'2“手提袋。在引号的变化中,我没有出现错误,但是价格被完全省略了。有关如何解决此问题的任何想法?

这里是开发站点上示例产品的链接,其中显示了问题:https://windgenuity.tracikay.com.au/shop/nalu-2019/

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;

$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

$term_slug = ( !empty( $result ) ) ? $result[0] : $term;


$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";

$variation_id = $wpdb->get_col( $query );

$parent = wp_get_post_parent_id( $variation_id[0] );

if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );


$itemPrice = strip_tags (WC_price( $_product->get_price() ));
//display price
return $term . ' (' . $itemPrice . ')';
}
return $term;

}  

0 个答案:

没有答案