将高级自定义字段添加到WooCommerce产品变体 [英] Add Advanced Custom Fields to WooCommerce Product Variation

查看:142
本文介绍了将高级自定义字段添加到WooCommerce产品变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用名为



但是因为我没有在 product>下看到这些字段变化。我进行了搜索,看来必须编写自定义代码来容纳这些字段。我尝试搜索问题,发现的大部分建议和教程都是关于通过代码而不是使用ACF创建自定义字段的,这对我无济于事,因为这些字段必须使用ACF,这是因为我正在使用Visual Composer来提取这些内容前端的ACF字段。



解决方案

这不是acf,但您只需在 /public_html/wp-content/themes/yourtheme-child/function.php 下的子主题的 function.php中添加一些代码。 / p>

请查看本教程 http://www.remicorson.com/mastering-woocommerce-products-custom-fields/



例如,在我的代码中, ve为RRP添加了2个字段,另一个为个人使用(每对价格):

  / *添加RRP或PPP *用户整个商店中每个产品价格之后的价格!=客户&来宾
。由于不在每个购物车中,因此无需显示在购物车中,因此我们无需这样做。
PPP =每对价格(用于产品组合/捆绑展示)
============================= = / ================== * /

//显示字段
add_action('woocommerce_product_options_general_product_data','woo_add_custom_general_fields');

//保存字段
add_action(‘woocommerce_process_product_meta’,‘woo_add_custom_general_fields_save’);

函数woo_add_custom_general_fields(){

全球$ woocommerce,$ post;

//文本字段创建
woocommerce_wp_text_input(
array(
'id'=>'_rrpprice',
'label'=> __ ('RRP price($)','woocommerce'),
'placeholder'=>'eg 499',
'desc_tip'=>'true',
'description' => __('Enter the RRP。','woocommerce')

);
woocommerce_wp_text_input(
array(
'id'=>'_pppprice',
'label'=> __('每对价格*','woocommerce'),
'占位符'=>'例如122',
'desc_tip'=>'真',
'描述'=> __('输入PPP(每对价格)如果是Bundle / composite。','woocommerce')

);
}
函数woo_add_custom_general_fields_save($ post_id){

// TextField保存
$ woocommerce_rrpprice = $ _POST ['_ rrpprice'];
update_post_meta($ post_id,’_rrpprice',esc_html($ woocommerce_rrpprice));
if(!empty($ emptcommerce_rrpprice))

// TextField保存
$ woocommerce_pppprice = $ _POST [’_ pppprice’];
if(!empty($ woocommerce_pppprice))
update_post_meta($ post_id,'_pppprice',esc_html($ woocommerce_pppprice)));
}

//显示自定义字段值

if(is_user_logged_in()&&!(current_user_can('customer'))){
函数sv_change_product_price_display($ price){
$ product = wc_get_product(get_the_ID());
$ priceRRP = get_post_meta(get_the_ID(), _ rrpprice,true);
$ pricePPP = get_post_meta(get_the_ID(), _ pppprice,true);
if((is_shop()|| is_product())&!is_cart()){//双皮带检查
if($ product-> is_type('variable')){
$ price。='+ GST< br />< span class = rrp-price> RRP:$'。 $ priceRRP。’< / span>’;
} else {

$ price =’< span class = rrp-price>< b> $’。 $ pricePPP。’+ GST< / b>< / span>’。 ’< br />< span class = rrp-price> RRP:$’。 $ priceRRP。’< / span>’;
}
}
返回$ price;
}
add_filter(‘woocommerce_get_price_html’,’sv_change_product_price_display’);
add_filter(‘woocommerce_cart_item_price’,‘sv_change_product_price_display’);
}

如果您有任何疑问,请随时提问。


I am using plugins called Advanced Custom Fields (ACF) and WooCommerce. I want to create a text and image field for WooCommerce product variation. I created fields in ACF and assigned them to "Post Type" > "product_variation".

But for I don't see these fields under product > Variations. I searched and it looks like I have to write a custom code to accommodate these fields. I tried searching the problem and most of the suggestions and tutorials I founds are about creating custom fields via code and not using ACF which is not helping me as the fields have to be using ACF and that is because I am using Visual Composer to pull these ACF fields on front end.

解决方案

It's not acf but you just have to add some code to your "function.php" of your child theme under /public_html/wp-content/themes/yourtheme-child/function.php.

Please have a look to this tutorial http://www.remicorson.com/mastering-woocommerce-products-custom-fields/

For instance, in my code I've added 2 fields for the RRP and another one for personal use (Price per Pair):

        /* Adds RRP or PPP* price after each product price throughout the shop for user != Customer & Guest
    .Not displayed in cart as it's not per var and we don't need to.
    PPP = Price Per Pair (for product composite/bundle display)
    ================================================== */

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {

    global $woocommerce, $post;

    // Text Field creation
woocommerce_wp_text_input( 
    array( 
        'id'          => '_rrpprice', 
        'label'       => __( 'RRP price ($)', 'woocommerce' ), 
        'placeholder' => 'e.g. 499',
        'desc_tip'    => 'true',
        'description' => __( 'Enter the RRP .', 'woocommerce' )
    )
);
woocommerce_wp_text_input( 
    array(  
        'id'          => '_pppprice', 
        'label'       => __( 'Price Per Pair*', 'woocommerce' ),
        'placeholder' => 'e.g. 122',
        'desc_tip'    => 'true',
        'description' => __( 'Enter the PPP (Price Per Pair) if Bundle/composite .', 'woocommerce' ) 
    )
);  
}
function woo_add_custom_general_fields_save( $post_id ){

    // TextField save
    $woocommerce_rrpprice = $_POST['_rrpprice'];
    update_post_meta( $post_id, '_rrpprice', esc_html( $woocommerce_rrpprice ) );
    if( !empty( $woocommerce_rrpprice ) )

    // TextField save
    $woocommerce_pppprice = $_POST['_pppprice'];
    if( !empty( $woocommerce_pppprice ) )
    update_post_meta( $post_id, '_pppprice', esc_html( $woocommerce_pppprice ) );
}

// Display Custom Field Value

if ( is_user_logged_in() && !(current_user_can('customer'))) {
    function sv_change_product_price_display( $price ) {
        $product = wc_get_product( get_the_ID() );
        $priceRRP = get_post_meta( get_the_ID(), '_rrpprice', true );
        $pricePPP = get_post_meta( get_the_ID(), '_pppprice', true );
          if ( (is_shop() || is_product()) && !is_cart() ) { //double belt check
            if($product->is_type( 'variable' )){
                $price .= ' + GST<br /><span class="rrp-price">RRP: $' . $priceRRP .'</span>';
            }else{

                $price = ' <span class="rrp-price"><b>$' . $pricePPP .' + GST </b></span>' . '<br /><span class="rrp-price">RRP: $' . $priceRRP .'</span>';
            }
          }
        return $price;          
    }
    add_filter( 'woocommerce_get_price_html', 'sv_change_product_price_display' );
    add_filter( 'woocommerce_cart_item_price', 'sv_change_product_price_display' );
}

If you have any question please feel free to ask.

这篇关于将高级自定义字段添加到WooCommerce产品变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆