首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress自定义字段

Wordpress自定义字段
EN

Stack Overflow用户
提问于 2014-03-10 13:52:57
回答 2查看 1.1K关注 0票数 0

我是PHP的新手,所以这让我有点困惑。我正在使用Wordpress的Woocommerce插件,并试图添加一个自定义字段来显示某些产品的租赁价格。然而,并不是所有的产品都有租赁选项,所以我希望这只在我给出租赁价格的产品上显示。

下面是我正在使用的代码,它工作得很好。唯一的问题是,对于我没有指定租赁价格的产品,它显示的租赁价格为0美元。我只想让它根本不显示,而不是显示$0。

代码语言:javascript
复制
//add rental field
add_action( 'woocommerce_product_options_pricing', 'wc_rent_product_field' );
function wc_rent_product_field() {
    woocommerce_wp_text_input( array( 'id' => 'rent_price', 'class' => 'wc_input_price short', 'label' => __( 'rent', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );
}

//save rental field
add_action( 'save_post', 'wc_rent_save_product' );
function wc_rent_save_product( $product_id ) {
    // If this is a auto save do nothing, we only save when update button is clicked
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( isset( $_POST['rent_price'] ) ) {
        if ( is_numeric( $_POST['rent_price'] ) )
            update_post_meta( $product_id, 'rent_price', $_POST['rent_price'] );
    } else delete_post_meta( $product_id, 'rent_price' );
}

//display rental field
add_action( 'woocommerce_single_product_summary', 'wc_rent_show', 5 );
function wc_rent_show() {
    global $product;
    // Do not show this on variable products
    if ( $product->product_type <> 'variable' ) {
        $rent = get_post_meta( $product->id, 'rent_price', true );
        echo '<div class="woocommerce_msrp">';
        _e( 'Rent: ', 'woocommerce' );
        echo '<span class="woocommerce-rent-price">' . woocommerce_price( $rent ) . '</span>';
        echo '</div>';
    }
}

有人能帮上忙吗?我在互联网上到处寻找答案,但它似乎让我摸不着头脑。

EN

回答 2

Stack Overflow用户

发布于 2014-03-10 14:16:36

代码语言:javascript
复制
  if ( $product->product_type <> 'variable' ) {
    $rent = get_post_meta( $product->id, 'rent_price', true );
    if($rent)>0
    {
    echo '<div class="woocommerce_msrp">';
    _e( 'Rent: ', 'woocommerce' );
    echo '<span class="woocommerce-rent-price">' . woocommerce_price( $rent ) . '</span>';
    echo '</div>';
  }
}
票数 0
EN

Stack Overflow用户

发布于 2015-06-09 17:25:36

这正是我正在寻找的,在对上面的答案进行了一些调整之后,以下代码仅显示具有租赁价格的产品的年度租赁价格。它还在单个产品和归档页面上订购在RRP和折扣价格之后显示的租赁价格。

代码语言:javascript
复制
//add rental field
add_action( 'woocommerce_product_options_pricing', 'wc_rent_product_field' );
function wc_rent_product_field() {
    woocommerce_wp_text_input( array( 'id' => 'rent_price', 'class' => 'wc_input_price short', 'label' => __( 'Annual Rental', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );
}

//save rental field
add_action( 'save_post', 'wc_rent_save_product' );
function wc_rent_save_product( $product_id ) {
    // If this is a auto save do nothing, we only save when update button is clicked
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( isset( $_POST['rent_price'] ) ) {
        if ( is_numeric( $_POST['rent_price'] ) )
            update_post_meta( $product_id, 'rent_price', $_POST['rent_price'] );
    } else delete_post_meta( $product_id, 'rent_price' );
}

//display rental field
add_action( 'woocommerce_single_product_summary', 'wc_rent_show', 20 );
function wc_rent_show() {
    global $product;
    // Do not show this on variable products
    if ( $product->product_type <> 'variable' ) {
        $rent = get_post_meta( $product->id, 'rent_price', true );
    if ($rent > 0) {
        echo '<div class="woocommerce_msrp">';
        _e( 'Annual Rental: ', 'woocommerce' );
        echo '<span class="woocommerce-rent-price">' . woocommerce_price( $rent ) . '</span>';
        echo '</div>';
    }
    }
}

// Optional: To show on archive pages
add_action( 'woocommerce_after_shop_loop_item_title', 'wc_rent_show', 50 );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22292952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档