【问题标题】:Add a multilingual text after price in woocommerce_get_price_html hook在 woocommerce_get_price_html 挂钩中的价格后添加多语言文本
【发布时间】:2016-12-22 18:55:31
【问题描述】:

我正在构建一个 WooCommerce 网站并对其进行自定义,从 Internet 库中复制和粘贴代码。

我已设法在 woocommerce 产品中添加“自定义价格和自定义文本”,以便将它们翻译成不同的语言。这是产品页面的外观:https://www.primacent.de/de/product/woo-album-3-2

这是我在 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 );

问题是我希望在单价 (_unit_price) 之后用 单位度量 (_unit_measurement) 替换连字符“-”。

由于某些原因,我无法将第二个元值放入 &lt;span&gt; 包装中。

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

谢谢!

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    您只需使用 .=unit measure 值连接到一个字符串中(例如在 &lt;span&gt; 标记中) $price_html 变量的 /strong> 运算符,然后再返回。

    所以你的代码将是:

    //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;
    }
    

    代码进入您的活动子主题(活动主题或任何插件文件)的 function.php 文件中。

    【讨论】:

      猜你喜欢
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 2018-10-29
      • 1970-01-01
      • 2018-04-26
      相关资源
      最近更新 更多