Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用Woocommerce中的自定义字段替换变化价格范围_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 用Woocommerce中的自定义字段替换变化价格范围

Php 用Woocommerce中的自定义字段替换变化价格范围,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我使用以下函数将自定义字段添加并保存到可变产品中: add_action( 'woocommerce_product_options_inventory_product_data', 'wc_add_custom_fields' ); function wc_add_custom_fields() { woocommerce_wp_text_input( array( 'id' => '_custom_p

我使用以下函数将自定义字段添加并保存到可变产品中:

add_action( 'woocommerce_product_options_inventory_product_data', 'wc_add_custom_fields' );

function wc_add_custom_fields() {

    woocommerce_wp_text_input(
         array(
             'id'                => '_custom_product_pricekg_field',
             'label'             => __( 'Price per kg', 'woocommerce' ),
             'placeholder'       => '',
            'desc_tip'            => false,
             'description'       => __( "Here's some really helpful text that appears next to the field.", 'woocommerce' ),
             'type'              => 'number',
             'custom_attributes' => array(
                     'step'     => 'any',
                     'min'    => '0'
                 )
         )
     );    

}

add_action( 'woocommerce_process_product_meta', 'wc_custom_fields_save' );

function wc_custom_fields_save($post_id)
{

    $woocommerce_custom_product_pricekg_field = $_POST['_custom_product_pricekg_field'];
    if (!empty($woocommerce_custom_product_pricekg_field))
        update_post_meta($post_id, '_custom_product_pricekg_field', esc_attr( $_POST['_custom_product_pricekg_field'] ) );
}
我想用这个自定义字段替换所有归档和产品页面上的可变价格范围($XX-XX)。我不知道怎么做。下面的代码段将变量price range替换为默认变量,也许可以修改它以显示我的自定义字段值

add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);

function custom_variation_price( $price, $product ) {

    foreach($product->get_available_variations() as $pav){
        $def=true;
        foreach($product->get_variation_default_attributes() as $defkey=>$defval){
            if($pav['attributes']['attribute_'.$defkey]!=$defval){
                $def=false;             
            }   
        }
        if($def){
            $price = $pav['display_price'];         
        }
    }   
    return woocommerce_price($price);
}

以下代码将价格范围替换为自定义字段中的值,如果您有其他问题,欢迎使用

//添加新字段
函数wc_添加_自定义_字段(){
woocommerce_wp_text_input(
排列(
'id'=>''自定义\产品\价格字段',
“标签”=>“(“每千克价格”,“woocommerce”),
'占位符'=>'',
“描述提示”=>错误,
'description'=>uuuu(“这里有一些非常有用的文本显示在字段旁边。”,'woocommerce'),
'类型'=>'编号',
“自定义_属性”=>数组(
'步骤'=>'任何',
“min”=>“0”
)
)
);
}
添加操作('woocommerce\u product\u options\u inventory\u product\u data'、'wc\u add\u custom\u fields',10,0);
//拯救
函数wc\u自定义字段\u保存($post\u id){
$woocommerce\u custom\u product\u pricekg\u field=$\u POST[“custom\u product\u pricekg\u field]”;
如果(!空($woocmerce\u custom\u product\u pricekg\u field)){
更新发布元数据($发布id,''定制产品价格字段',esc属性($发布[''定制产品价格字段]);
}
}
添加操作('woocommerce\u process\u product\u meta'、'wc\u custom\u fields\u save',10,1);
//变动价格
功能自定义\变化\价格($price,$product){
//获取产品id
$product_id=$product->get_id();
//获取自定义值
$new_price=get_post_meta($product_id,''u custom_product_pricekg_field',true);
//不空
如果(!空($new_price)){
$price=$new_价格;
}
返回$price;
}
添加过滤器('woocommerce\u variable\u price\u html','custom\u variation\u price',10,2);

谢谢!这很有帮助。请原谅,您能告诉我如何保存空的自定义字段吗?当前,如果我想删除并保存输入的值,该字段将不会在“保存”中更新,请将if(!empty($woocmerce\u custom\u product\u pricekg\u field)){替换为if(isset($woocmerce\u custom\u product\u pricekg\u field)){