2017-07-14 9 views
2

既存のWooCommerce関数を適切にオーバーライドする最良の方法は何ですか?この場合、私はwc_price()関数を変更したいと思います。私はそれに狂った何かをする必要はありません、文字通り、HTMLの<span>属性を価格の周りに追加する必要があります。正常にWooCommerce関数WC_Price()をオーバーライドする

function wc_price($price, $args = array()) { 
    extract(apply_filters('wc_price_args', wp_parse_args($args, array(
     'ex_tax_label'  => false, 
     'currency'   => '', 
     'decimal_separator' => wc_get_price_decimal_separator(), 
     'thousand_separator' => wc_get_price_thousand_separator(), 
     'decimals'   => wc_get_price_decimals(), 
     'price_format'  => get_woocommerce_price_format(), 
    )))); 

    $negative  = $price < 0; 
    $price   = apply_filters('raw_woocommerce_price', floatval($negative ? $price * -1 : $price)); 
    $price   = apply_filters('formatted_woocommerce_price', number_format($price, $decimals, $decimal_separator, $thousand_separator), $price, $decimals, $decimal_separator, $thousand_separator); 

    if (apply_filters('woocommerce_price_trim_zeros', false) && $decimals > 0) { 
     $price = wc_trim_zeros($price); 
    } 

    $formatted_price = ($negative ? '-' : '') . sprintf($price_format, '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol($currency) . '</span>', $price); 
    $return   = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>'; 

    if ($ex_tax_label && wc_tax_enabled()) { 
     $return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>'; 
    } 

    return apply_filters('wc_price', $return, $price, $args); 
} 

私がやりたいすべてにそれを変更することです:私は次のようにコードがある知っている

function wc_price($price, $args = array()) { 
    extract(apply_filters('wc_price_args', wp_parse_args($args, array(
     'ex_tax_label'  => false, 
     'currency'   => '', 
     'decimal_separator' => wc_get_price_decimal_separator(), 
     'thousand_separator' => wc_get_price_thousand_separator(), 
     'decimals'   => wc_get_price_decimals(), 
     'price_format'  => get_woocommerce_price_format(), 
    )))); 

    $negative  = $price < 0; 
    $price   = apply_filters('raw_woocommerce_price', floatval($negative ? $price * -1 : $price)); 
    $price   = apply_filters('formatted_woocommerce_price', number_format($price, $decimals, $decimal_separator, $thousand_separator), $price, $decimals, $decimal_separator, $thousand_separator); 

    if (apply_filters('woocommerce_price_trim_zeros', false) && $decimals > 0) { 
     $price = wc_trim_zeros($price); 
    } 

    $formatted_price = ($negative ? '-' : '') . sprintf($price_format, '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol($currency) . '</span>', <span class="custom-prc"> . $price . </span>); 
    $return   = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>'; 

    if ($ex_tax_label && wc_tax_enabled()) { 
     $return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>'; 
    } 

    return apply_filters('wc_price', $return, $price, $args); 
} 

すべての任意の助けいただければ幸いです!ありがとう!

答えて

2

価格の周りにあなたのカスタムHTMLタグ<span class="custom-prc">0000</span>を追加するには、formatted_woocommerce_priceフィルターフックでこのようにフック機能を使用する必要があります:

add_filter('formatted_woocommerce_price', 'span_custom_prc', 10, 5); 
function span_custom_prc($number_format, $price, $decimals, $decimal_separator, $thousand_separator){ 
    return '<span class="custom-prc">'.$number_format.'</span>'; 
} 

コードは、あなたのアクティブなのfunction.phpファイルに行きます子供のテーマ(またはテーマ)、またはすべてのプラグインファイルに保存されます。

は、コードがテストされ、WooCommerce 3+


と連携している、あなたは(42,00 eurosと例えば)このHTML出力を取得します:

<span class="price"> 
    <span class="woocommerce-Price-amount amount"> 
     <span class="custom-prc">42,03</span> 
     "&nbsp;" 
     <span class="woocommerce-Price-currencySymbol">€</span> 
    </span> 
</span> 
+0

すごいです!これはまさに私が必要としていたものです。どうもありがとうございました! –

+0

それは私に多くの感謝を助けた – Hafsa