Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更改WooCommerce中长度的产品尺寸标签_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 更改WooCommerce中长度的产品尺寸标签

Php 更改WooCommerce中长度的产品尺寸标签,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我有一些自定义代码,可以在Wordpress/Woocommerce中创建一个快捷码,用于显示产品尺寸(长度,宽度,高度)-输出正常,但我想将“长度”的标签改为“深度”(仅前端正常)并将输出重新排序,以显示为高度、宽度、深度 下面是我创建短代码的代码-但是,我不确定我需要做什么来更改输出标签&重新排序它们 add_shortcode( 'product_taxonomies', 'product_taxonomies_shortcode' ); function product_taxonomi

我有一些自定义代码,可以在Wordpress/Woocommerce中创建一个快捷码,用于显示产品尺寸(长度宽度高度)-输出正常,但我想将“长度”的标签改为“深度”(仅前端正常)并将输出重新排序,以显示为高度、宽度、深度

下面是我创建短代码的代码-但是,我不确定我需要做什么来更改输出标签&重新排序它们

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();
    }
}
add_-shortcode('product_-taxonomics'、'product_-taxonomics_-shortcode');
功能产品\u分类法\u短代码($atts){
//短代码属性
$atts=短码_atts(数组)(
“id'=>”,
),$atts,“产品分类法”);
$product_id=!空($atts['id'])?$atts['id']:0;
如果($product\U id>0){
$product=wc\U get\U product($product\U id);
}否则{
全球$产品;
}
如果(!is_a($product,'WC_product')){
$product=wc_get_product(get_the_id());
}
如果(是($product,'WC\U product')){
ob_start();
//重量
如果($product->has_weight()){
$weight_unit=get_选项('woocommerce_weight_unit');
$weight\u html=''.\u(“weight”)。:'.$value.$weight\u单位;
回显“.$weight_html.”;
}
//尺寸
如果($product->has_dimensions()){
$dimension_unit=get_选项('woocommerce_dimension_unit');
$dimensions=array();
foreach($product->get_维度(false)为$key=>$value){
如果(!空($value))
$dimensions[]=''.ucfirst($key)。:'.$value.$dimensions\u单位;
}
回显“”。内爆(“
”,$dimensions)。“”; } 返回ob_get_clean(); } }
问题空间 首先,让我们确定需要执行更改的代码范围:

        $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>';
$dimensions=array();
foreach($product->get_维度(false)为$key=>$value){
如果(!空($value))
$dimensions[]=''.ucfirst($key)。:'.$value.$dimensions\u单位;
}
回显“”。内爆(“
”,$dimensions)。”;
执行更改
$keys=[
“高度”=>0,
“宽度”=>0,
“深度”=>0
];
$dimensions=array();
foreach($product->get_维度(false)为$key=>$value){
如果(!空($value)){
$currentKey=($key=='Length')?'Depth':$key);
$dimensions[$keys[$currentKey]]=''.ucfirst($currentKey)。':'.$value.$dimension\u单位;
}
}
回显“”。内爆(“
”,$dimensions)。”;
解释
  • 我们将metrix映射到它们所需的数组索引,因此数组将按照正确的顺序构建,而不是重新排序
  • 如果是
    $key
    ,我们将“Length”替换为“Depth”,否则使用
    $key
    ,任何替换都在表示级别上进行
潜在问题
我假设了一些具体的关键值。如果我对它们的理解是错误的,那么一些不正确的值可能会引起麻烦。如果是这样的话,让我们来讨论一下您可能遇到的情况。

我知道这不是您直接想要的,甚至可能在开发人员论坛上偏离主题。但我想提供第二个选择:一个插件,它允许你在不编辑WordPress核心的情况下更改网站上的字符串

我想有很多这样的插件,但我喜欢的是一个叫做


祝你好运

我想我已经用另一种方式解决了

function display_product_dimensions( $atts ){
    // Extract shortcode attributes
    extract( shortcode_atts( array(
        'id' => '',
    ), $atts, 'product_dimensions' ) );

    if( $id == '' ) {
        global $product;

        if( ! is_a($product, 'WC_Product') ) {
            $product = wc_get_product( get_the_id() );
        }
    }

    return method_exists( $product, 'get_dimensions' ) ? wc_format_dimensions($product->get_dimensions(false)) : '';
}


add_filter( 'woocommerce_format_dimensions', 'Custom_formated_product_dimentions_with_labels', 10, 2 );
function Custom_formated_product_dimentions_with_labels( $dimension_string, $dimensions ){
    if ( empty( $dimension_string ) )
        return __( 'N/A', 'woocommerce' );

    // Set here your new array of dimensions based on existing keys/values
    $new_dimentions = array(
        '<b>Height:</b>' => $dimensions['height'],
        '<b>Width:</b>'  => $dimensions['width'],
        '<b>Depth:</b>' => $dimensions['length']
    );

    $dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) );

    $label_with_dimensions = array();

    foreach( $dimensions as $key => $dimention ){
        $dimensions[$key] = ucfirst($key) . ' ' . $dimention . ' ' . get_option( 'woocommerce_dimension_unit' );
    }

    return implode( ' <br> ',  $dimensions) . '';
}
功能显示\产品\尺寸($atts){
//提取短代码属性
提取(短码)附件(数组)(
“id'=>”,
),$atts,'产品尺寸');
如果($id=''){
全球$产品;
如果(!is_a($product,'WC_product')){
$product=wc_get_product(get_the_id());
}
}
返回方法_存在($product,'get_dimensions')?wc_格式_dimensions($product->get_dimensions(false)):“”;
}
添加过滤器('woocommerce\u format\u dimensions'、'Custom\u Formatted\u product\u dimensions\u with\u labels',10,2);
函数自定义\格式化\产品\尺寸\带有\标签($dimension\字符串,$dimensions){
if(空($dimension_string))
返回(“不适用”、“商业”);
//在此根据现有键/值设置新的维度数组
$new\u维度=数组(
'高度:'=>$dimensions['Height'],
'宽度:'=>$dimensions['Width'],
'深度:'=>$dimensions['length']
);
$dimensions=数组\过滤器(数组\映射('wc\ u格式\本地化\十进制',$new\维度));
$label_,带_维度=数组();
foreach($key=>$dimension的维度){
$dimensions[$key]=ucfirst($key)。'.$dimensions.''.get_选项('woocommerce_dimensions_unit');
}
返回内爆(“
”,$dimensions)。”; }

这似乎工作正常,显示正确…

非常好,谢谢-这是一种工作:)高度现在先显示,但宽度和深度(长度)现在缺失/未显示displayed@Combined还有一些文本上的差异。你能找出$key的值是什么,包括空格吗?它可能在长度的开始/结束处有一个空格,或者长度可能是长度,所以是一。我需要知道长度键的确切字符串。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能无效。-很好的建议,谢谢。我更新了我的ans
function display_product_dimensions( $atts ){
    // Extract shortcode attributes
    extract( shortcode_atts( array(
        'id' => '',
    ), $atts, 'product_dimensions' ) );

    if( $id == '' ) {
        global $product;

        if( ! is_a($product, 'WC_Product') ) {
            $product = wc_get_product( get_the_id() );
        }
    }

    return method_exists( $product, 'get_dimensions' ) ? wc_format_dimensions($product->get_dimensions(false)) : '';
}


add_filter( 'woocommerce_format_dimensions', 'Custom_formated_product_dimentions_with_labels', 10, 2 );
function Custom_formated_product_dimentions_with_labels( $dimension_string, $dimensions ){
    if ( empty( $dimension_string ) )
        return __( 'N/A', 'woocommerce' );

    // Set here your new array of dimensions based on existing keys/values
    $new_dimentions = array(
        '<b>Height:</b>' => $dimensions['height'],
        '<b>Width:</b>'  => $dimensions['width'],
        '<b>Depth:</b>' => $dimensions['length']
    );

    $dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) );

    $label_with_dimensions = array();

    foreach( $dimensions as $key => $dimention ){
        $dimensions[$key] = ucfirst($key) . ' ' . $dimention . ' ' . get_option( 'woocommerce_dimension_unit' );
    }

    return implode( ' <br> ',  $dimensions) . '';
}