亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

自定義價格字段未保存在 WooCommerce 中的管理產品中

自定義價格字段未保存在 WooCommerce 中的管理產品中

PHP
qq_笑_17 2023-06-18 16:22:38
我向 WooCommerce 添加了一個自定義價格字段。該字段按預期出現,但它不保存輸入的值。我functions.php文件中的代碼是:/* Add custom price field to general page */function wc_cost_product_field() {    woocommerce_wp_text_input( array( 'id' => 'wholesaler_price', 'class' => 'wc_input_price short', 'label' => __( 'Wholesaler price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );}add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' );function pcc_save_custom_price( $post_id ) {    // Grab the custom price from $_POST    $custom_price = isset( $_POST[ 'wholesale_price' ] ) ? sanitize_text_field( $_POST[ 'wholesale_price' ] ) : '';    // grab the product    $product = wc_get_product( $post_id );    // Save the custom price using WooCommerce built-in functions    $product->update_meta_data( 'wholesale_price', $custom_price );    $product->save();}add_action( 'woocommerce_process_product_meta', 'pcc_save_custom_price' );
查看完整描述

1 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

你有一些小錯誤,這應該足夠了


另請注意使用woocommerce_admin_process_product_objectto save 而不是過時的woocommerce_process_product_meta鉤子


/* Add custom price field to general page */

function action_woocommerce_product_options_pricing() { 

    woocommerce_wp_text_input( array(

        'id' => 'wholesaler_price', 

        'class' => 'wc_input_price short', 

        'label' => __( 'Wholesaler price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',

    ) );

}


add_action( 'woocommerce_product_options_pricing', 'action_woocommerce_product_options_pricing', 10, 0 );


// Save Fields

function action_woocommerce_admin_process_product_object( $product ) {

    if( isset($_POST['wholesaler_price']) ) {

        $product->update_meta_data( 'wholesaler_price', sanitize_text_field( $_POST[ 'wholesaler_price'] ) );

    }

}

add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 ); 



查看完整回答
反對 回復 2023-06-18
  • 1 回答
  • 0 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號