【发布时间】:2021-04-07 09:09:13
【问题描述】:
我有一些自定义代码在 Wordpress/Woocommerce 中创建一个简码,用于显示产品尺寸(Length、Width、Height >) - 输出工作正常,但是我想将“Length”的标签更改为“Depth”(仅前端即可)并重新排序输出显示为 Height、Width、Depth(按此顺序)
这是我创建简码的代码 - 但是,我不确定我需要做什么来更改输出标签并重新排序。
add_shortcode( 'product_taxonomies', 'product_taxonomies_shortcode' );
function product_taxonomies_shortcode( $atts ){
// Shortcode Attributes
$atts = shortcode_atts( array(
'id' => '',
), $atts, 'product_taxonomies' );
$product_id = ! empty($atts['id']) ? $atts['id'] : 0;
if( $product_id > 0 ) {
$product = wc_get_product( $product_id );
} else {
global $product;
}
if( ! is_a($product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_id() );
}
if ( is_a($product, 'WC_Product' ) ) {
ob_start();
// Weight
if ( $product->has_weight() ) {
$weight_unit = get_option('woocommerce_weight_unit');
$weight_html = '<strong>'.__("Weight").':</strong> ' . $value . $weight_unit;
echo '<div class="dimensions">' . $weight_html . '</div>';
}
// Dimensions
if ( $product->has_dimensions() ) {
$dimension_unit = get_option('woocommerce_dimension_unit');
$dimensions = array();
foreach( $product->get_dimensions( false ) as $key => $value ) {
if( ! empty($value) )
$dimensions[] = '<strong>'.ucfirst($key).':</strong> ' . $value . $dimension_unit;
}
echo '<div class="dimensions">' . implode('<br>', $dimensions) . '</div>';
}
return ob_get_clean();
}
}
【问题讨论】:
标签: php wordpress woocommerce