php自定义商品属性,php – 可变产品属性:自定义每个显示的单选按钮文本值
以下是您重新訪問的代碼,該代碼僅顯示“Tab”屬性單選按鈕自定義顯示的文本a< span>基于屬性slug和$term_slug的組合,使用不同的類值標記.
因此,您可以將一些CSS樣式顏色應用于每個單選按鈕顯示的’pa_tab’屬性的自定義文本,將這些CSS規則添加到您的活動主題style.css …
這是重新訪問的代碼:
// Display the product variation price inside the variations dropdown.
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name', 10, 1 );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
// Define HERE the targetted attribute taxonomy slug
$tax_slug = 'pa_tab';
// compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if ( empty( $term ) || empty( $product_id ) )
return $term;
$result = $wpdb->get_var( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( ! empty( $result ) ) ? $result : $term;
$variation_id = $wpdb->get_var( "
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
");
$parent = wp_get_post_parent_id( $variation_id );
if ( $parent > 0 ) {
$_product = wc_get_product( $variation_id );
if ( has_term( $term, $tax_slug, $_product->id) )
$output = ' ' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ' - ('.$term.')';
else
$output = ' ' . $term;
return $output;
}
return $term;
}
代碼放在活動子主題(或主題)的function.php文件中,或者放在任何插件文件中.
此代碼經過測試和運行.
生成的html代碼是這樣的:
$99.00
- (Option Blue)
$299.00
- (Option Green)
So you target this specific radio buttons displayed custom text with CSS rules something like:
span.pa_tab-price {
font-family: lato, sans-serif;
font-size: medium;
}
span.pa_tab-option-blue, span.pa_tab-option-green,
span.pa_tab-option-purple, span.pa_tab-option-orange {
font-family: lato, sans-serif;
font-size: medium;
font-style: normal;
font-weight: 300;
}
span.pa_tab-option-blue {
color: blue;
}
span.pa_tab-option-green {
color: green;
}
span.pa_tab-option-purple {
color: purple;
}
span.pa_tab-option-orange {
color: orange;
}
這只是一個例子
總結
以上是生活随笔為你收集整理的php自定义商品属性,php – 可变产品属性:自定义每个显示的单选按钮文本值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java实现qq抢红包_Java实现抢红
- 下一篇: evalin matlab,求解MATL