Php 在woocommerce\u get\u price\u html钩子中的price之后添加多语言文本

Php 在woocommerce\u get\u price\u html钩子中的price之后添加多语言文本,php,wordpress,woocommerce,product,price,Php,Wordpress,Woocommerce,Product,Price,我正在建立一个WooCommerce网站并对其进行定制,从internet库复制和粘贴代码 我已经设法在woocommerce产品中添加了“自定义价格和自定义文本”,以便将它们翻译成不同的语言。以下是产品页面的外观: 这是我在functions.php中的代码: //New Price add_action('woocommerce_product_options_pricing','custom_unit_price'); function custom_unit_price() { wo

我正在建立一个WooCommerce网站并对其进行定制,从internet库复制和粘贴代码

我已经设法在woocommerce产品中添加了“自定义价格和自定义文本”,以便将它们翻译成不同的语言。以下是产品页面的外观:

这是我在
functions.php中的代码:

//New Price
add_action('woocommerce_product_options_pricing','custom_unit_price');
function custom_unit_price() {
  woocommerce_wp_text_input( array( 'id' => '_unit_price', 'class' => 'wc_input_price short', 'label' => __( 'Unit Price', 'woocommerce' ) .    ' ('.get_woocommerce_currency_symbol().')', 'type' => 'number', 'desc_tip' => 'true','description' => __( 'Enter the unit price if you want to show this price type.', 'woocommerce' ), 'custom_attributes' => array(
                    'step'  => 'any',
                    'min'   => '0'
                ) ) );
}
add_action('woocommerce_process_product_meta_simple', 'save_custom_unit_price');
function save_custom_unit_price($post_id) {
    global $wpdb, $woocommerce, $woocommerce_errors;
    update_post_meta( $post_id, '_unit_price', stripslashes( $_POST['_unit_price'] ) );
}
// Text Field for unit measurement
add_action('woocommerce_product_options_pricing','custom_unit_measurement');
function custom_unit_measurement() {
woocommerce_wp_text_input ( array('id' => '_unit_measurement', 'label' => __( 'Unit Measurement', 'woocommerce' ), 'placeholder' => 'i.e: pro Stück','desc_tip' => 'true','description' => __( 'Enter the unit measurement in your language. If you want to show price per unit, this field must be filled', 'woocommerce' ) 
    )
);
}
add_action('woocommerce_process_product_meta_simple', 'save_custom_unit_measurement');
function save_custom_unit_measurement($post_id) {
    global $wpdb, $woocommerce, $woocommerce_errors;
    update_post_meta( $post_id, '_unit_measurement', stripslashes( $_POST['_unit_measurement'] ) );
}

// only copy the opening php tag if needed
// Change the shop / product prices if a _unit_price is set
function sv_change_product_html( $price_html, $product ) {

    $_unit_price = get_post_meta( $product->id, '_unit_price', true );
    if ( ! empty( $_unit_price ) ) {
        $price_html = '<span class="amount">' . wc_price( $_unit_price ).  '  </span>';
                      echo $_unit_measurement = get_post_meta( $product->id, '_unit_measurement', true );echo'<br />';      

    }

    return $price_html;
}

add_filter( 'woocommerce_get_price_html', 'sv_change_product_html', 10, 2 );
//新价格
添加行动(“商业、产品、选项、定价”、“自定义单价”);
功能自定义单位价格(){
woocommerce\u wp\u text\u input(数组('id'=>''单位价格','class'=>'wc\u input\u价格缩写','label'=>('单价','woocommerce')。('get\u woocommerce\u currency\u symbol()'),'type'=>'编号','desc tip'=>'真','description'=>。(如果要显示此价格类型,请输入单位价格,'woocommerce','woocmerce')),“自定义_属性”=>数组(
'步骤'=>'任何',
“min”=>“0”
) ) );
}
添加动作(“商业、流程、产品、元、简单”、“保存、定制、单价”);
功能保存\自定义\单价($post\ id){
全球$wpdb、$woocommerce、$woocommerce\u错误;
更新发布元($发布id,''单价',带斜杠($发布[''单价']);
}
//用于单位测量的文本字段
添加行动(“商业、产品、选项、定价”、“定制、单位、计量”);
功能自定义单位测量(){
woocommerce\u wp\u text\u输入(数组('id'=>')\u单位测量','label'=>('unit measurement','woocommerce'),'placeholder'=>'即:pro Stück','desc_tip'=>'true','description'=>\uuuuu('用您的语言输入单位测量值。如果要显示每单位的价格,必须填写此字段,'woocommerce'))
)
);
}
添加动作(“商业、过程、产品、元、简单”、“保存、自定义、单位、度量”);
功能保存\自定义\单位\测量($post\ id){
全球$wpdb、$woocommerce、$woocommerce\u错误;
更新发布元($发布id,''单位测量'',带斜杠($发布[''单位测量']);
}
//仅在需要时复制开始的php标记
//如果设置了单价,则更改商店/产品价格
函数sv_change_product_html($price_html,$product){
$\u unit\u price=get\u post\u meta($product->id,“\u unit\u price”,true);
如果(!空($\单价)){
$price\u html=''.wc\u价格($\单价)。'';
echo$_unit_measurement=get_post_meta($product->id,'u unit_measurement',true);echo'
; } 返回$price_html; } 添加过滤器('woocommerce\u get\u price\u html'、'sv\u change\u product\u html',10,2);
问题是,我希望单价(
\u unit\u price
)后面的单位度量(
\u unit\u measurement
替换连字符标记“-”

由于某些原因,我无法将第二个元值放入
包装中

请帮帮我。我怎样才能做到这一点


谢谢

在返回
=
变量之前,您只需在价格之后使用
=
运算符将该单位度量值连接到字符串中(例如在
标记中)

因此,您的代码将是:

//New Price
add_action( 'woocommerce_product_options_pricing' ,'custom_unit_price' );
function custom_unit_price() {
    woocommerce_wp_text_input( array( 
        'id'                => '_unit_price', 
        'class'             => 'wc_input_price short', 
        'label'             => __( 'Unit Price', 'woocommerce' ) .    ' ('.get_woocommerce_currency_symbol().')', 
        'type'              => 'number', 
        'desc_tip'          => 'true',
        'description'       => __( 'Enter the unit price if you want to show this price type.', 'woocommerce' ), 
        'custom_attributes' => array(
            'step'          => 'any',
            'min'           => '0'
        ) 
    ));
}

add_action('woocommerce_process_product_meta_simple', 'save_custom_unit_price');
function save_custom_unit_price($post_id) {
    global $wpdb, $woocommerce, $woocommerce_errors;
    update_post_meta( $post_id, '_unit_price', stripslashes( $_POST['_unit_price'] ) );
}

// Text Field for unit measurement
add_action('woocommerce_product_options_pricing','custom_unit_measurement');
function custom_unit_measurement() {
    woocommerce_wp_text_input ( array(
        'id'            => '_unit_measurement', 
        'label'         => __( 'Unit Measurement', 'woocommerce' ), 
        'placeholder'   => 'i.e: pro Stück',
        'desc_tip'      => 'true',
        'description'   => __( 'Enter the unit measurement in your language. If you want to show price per unit, this field must be filled', 'woocommerce' ) 

    ));
}

add_action('woocommerce_process_product_meta_simple', 'save_custom_unit_measurement');
function save_custom_unit_measurement($post_id) {
    global $wpdb, $woocommerce, $woocommerce_errors;
    update_post_meta( $post_id, '_unit_measurement', stripslashes( $_POST['_unit_measurement'] ) );
}

// only copy the opening php tag if needed
// Change the shop / product prices if a _unit_price is set
add_filter( 'woocommerce_get_price_html', 'sv_change_product_html', 10, 2 );
function sv_change_product_html( $price_html, $product ) {

    $_unit_price = get_post_meta( $product->id, '_unit_price', true );

    if ( ! empty( $_unit_price ) ) {
        $_unit_measurement = get_post_meta( $product->id, '_unit_measurement', true );

        // Here you just concatenate the $_unit_measurement variable (in for example another span tag) after the price
        $price_html = '<span class="amount">' . wc_price( $_unit_price ).  '  </span>';
        $price_html .= '<span class="mesurement">' . $_unit_measurement . '  </span><br />';        
    }
    // return the formated price with the formated unit mesurement
    return $price_html;
}
//新价格
添加行动(“商业、产品、选项、定价”、“自定义单价”);
功能自定义单位价格(){
woocommerce_wp_text_输入(数组(
'id'=>''单价',
“类”=>“wc\u输入\u价格短”,
“label'=>”(“单价”,“woocommerce”)。(“.get\u woocommerce\u currency\u symbol()”,
'类型'=>'编号',
'desc_tip'=>'true',
“description'=>\(“如果要显示此价格类型,请输入单价。”,“woocommerce”),
“自定义_属性”=>数组(
'步骤'=>'任何',
“min”=>“0”
) 
));
}
添加动作(“商业、流程、产品、元、简单”、“保存、定制、单价”);
功能保存\自定义\单价($post\ id){
全球$wpdb、$woocommerce、$woocommerce\u错误;
更新发布元($发布id,''单价',带斜杠($发布[''单价']);
}
//用于单位测量的文本字段
添加行动(“商业、产品、选项、定价”、“定制、单位、计量”);
功能自定义单位测量(){
woocommerce_wp_text_输入(数组(
“id'=>“\u单位\u测量”,
“标签”=>uuuuu(‘单位测量’、‘商业’),
“占位符”=>“即:支持Stück”,
'desc_tip'=>'true',
'description'=>\uuuu('用您的语言输入单位度量值。如果要显示每单位的价格,此字段必须填写'woocommerce')
));
}
添加动作(“商业、过程、产品、元、简单”、“保存、自定义、单位、度量”);
功能保存\自定义\单位\测量($post\ id){
全球$wpdb、$woocommerce、$woocommerce\u错误;
更新发布元($发布id,''单位测量'',带斜杠($发布[''单位测量']);
}
//仅在需要时复制开始的php标记
//如果设置了单价,则更改商店/产品价格
添加过滤器('woocommerce\u get\u price\u html'、'sv\u change\u product\u html',10,2);
函数sv_change_product_html($price_html,$product){
$\u unit\u price=get\u post\u meta($product->id,“\u unit\u price”,true);
如果(!空($\单价)){
$\u unit\u measurement=get\u post\u meta($product->id,'u unit\u measurement',true);
//在这里,您只需在price后面连接$\u unit\u度量变量(例如,在另一个span标记中)
$price\u html=''.wc\u价格($\单价)。'';
$price_html.=''.$_unit_measurement.
; } //返回带格式单位测量的格式价格 返回$price_html; }
代码进入活动子主题(活动主题或任何插件文件)的function.php文件