第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

將 Woocommerce 產(chǎn)品字段的默認(rèn)值設(shè)置為所有產(chǎn)品

將 Woocommerce 產(chǎn)品字段的默認(rèn)值設(shè)置為所有產(chǎn)品

PHP
森林海 2023-11-03 16:48:33
我使用本教程為我的產(chǎn)品創(chuàng)建了一個(gè)自定義字段,但我需要為此字段設(shè)置默認(rèn)值。我已經(jīng)創(chuàng)建了數(shù)百種產(chǎn)品,我也需要更新此字段。然后我使用這個(gè)值來訂購產(chǎn)品。這是完整的代碼:// Display Fields    add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');    // Save Fields    add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');    function woocommerce_product_custom_fields()    {        global $woocommerce, $post;        echo '<div class="product_custom_field">';        // Custom Product Text Field        woocommerce_wp_text_input(            array(                'id' => 'priority',                'placeholder' => 'Priority of the product - values a>b',                'label' => __('Priority of the product', 'woocommerce'),                'desc_tip' => 'true',                //'default' => '0',            )        );        echo '</div>';    }    function woocommerce_product_custom_fields_save($post_id)    {        // Custom Product Text Field        $woocommerce_custom_product_text_field = $_POST['priority'];        if (!empty($woocommerce_custom_product_text_field))            update_post_meta($post_id, 'priority', esc_attr($woocommerce_custom_product_text_field));    }    function cw_add_postmeta_ordering_args( $args_sort_cw ) {      $cw_orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) :            apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );      switch( $cw_orderby_value ) {           case 'priority':                $args_sort_cw['orderby'] = 'meta_value';                $args_sort_cw['order'] = 'asc';                $args_sort_cw['meta_key'] = 'priority';                break;      }我找不到可行的解決方案,所以我懇請您提供建議。我期望的是,每個(gè)現(xiàn)有產(chǎn)品都會設(shè)置此值,并且在創(chuàng)建新產(chǎn)品時(shí)將顯示默認(rèn)值。那么就應(yīng)該按照這個(gè)值來排序。感謝您的幫助!根據(jù)第一個(gè)答案進(jìn)行編輯:數(shù)據(jù)庫中的數(shù)據(jù)不會更新。在前端我只能看到我手動更改的產(chǎn)品,而看不到使用此功能添加 0 的產(chǎn)品。當(dāng)我嘗試將更改后的值更改回 0 時(shí),它不會更新。實(shí)際上我只需要一個(gè)可以刷新所有產(chǎn)品的函數(shù),以便將值存儲到數(shù)據(jù)庫中。
查看完整描述

2 回答

?
飲歌長嘯

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊

我要做的就是使用參數(shù)value中的參數(shù)woocommerce_wp_text_input。然后,您可以向其傳遞當(dāng)前值或默認(rèn)值(如果尚不存在)。


還添加了新的保存例程woocommerce_admin_process_product_object


// Display Fields

add_action('woocommerce_product_options_general_product_data', 'so_63588126_product_custom_fields');

// Save Fields

add_action('woocommerce_admin_process_product_object', 'so_63588126_product_custom_fields_save');



function so_63588126_product_custom_fields()

{

    global $product_object;


    $value = $product_object->get_meta( 'weight', true );


    if ( ! $value ) {

        $value = 0;

    }


    echo '<div class="product_custom_field">';

    // Custom Product Text Field

    woocommerce_wp_text_input(

        array(

            'id' => 'weight',

            'placeholder' => 'Weight of the product - values a>b',

            'label' => __('Weight of the product', 'your-textdomain'),

            'desc_tip' => 'true',

            'value' => $value 

        )

    );

    echo '</div>';

}


function so_63588126_product_custom_fields_save( $product )

{

    // Custom Product Text Field

    $woocommerce_custom_product_text_field = $_POST['weight'];

    if ( ! empty( $_POST['weight'] ) ) {

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

    } else {

        $product->update_meta_data( 'weight', 0 );

    }


}

補(bǔ)充說明:


我使用$product_object->update_meta_data()WooCommerce(自 3.0 起)更喜歡在對象上使用 CRUD(創(chuàng)建、讀取、更新、刪除)方法。update_post_meta()仍然有效,但如果/當(dāng) Woo 決定對產(chǎn)品和產(chǎn)品元使用自定義表時(shí),CRUD 將是面向未來的。


了解有關(guān) CRUD 的更多信息:https ://docs.woocommerce.com/document/developing-using-woocommerce-crud-objects/


關(guān)于文本域...這是自定義代碼,因此為了正確翻譯它,您需要使用您自己的獨(dú)特文本域,而不是“woocommerce”。


閱讀有關(guān)文本域的更多信息:https ://developer.wordpress.org/themes/functionality/internationalization/#text-domain


最后,sanitize_text_field()可能不是清理的正確選擇,但我不知道您在那里存儲了什么樣的數(shù)據(jù)。intval()對于數(shù)字會更好...因此可以通過將參數(shù)設(shè)置為 args將type輸入設(shè)置為數(shù)字輸入類型。'number'woocommerce_wp_text_input()


查看完整回答
反對 回復(fù) 2023-11-03
?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

這是對我有用的解決方案。


// Display Fields

add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');

// Save Fields

add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');

? ? function woocommerce_product_custom_fields()

? ? {

? ? ? ? global $woocommerce, $post, $product_object;

? ? ? ? $value = $product_object->get_meta( 'priority', true );


? ? ? ? if ( ! $value ) {

? ? ? ? ? ? $value = 0;

? ? ? ? }

? ? ? ??

? ? ? ? echo '<div class="product_custom_field">';

? ? ? ? // Custom Product Text Field

? ? ? ? woocommerce_wp_text_input(

? ? ? ? ? ? array(

? ? ? ? ? ? ? ? 'id' => 'priority',

? ? ? ? ? ? ? ? 'placeholder' => 'Priority - number 1>0',

? ? ? ? ? ? ? ? 'label' => __('Priority', 'woocommerce'),

? ? ? ? ? ? ? ? 'type' => 'number',

? ? ? ? ? ? ? ? 'desc_tip' => 'true',

? ? ? ? ? ? ? ? 'value' => $value

? ? ? ? ? ? )

? ? ? ? );

? ? ? ? echo '</div>';

? ? }


? ? function woocommerce_product_custom_fields_save($post_id)

? ? {

? ? ? ? // Custom Product Text Field

? ? ? ? if (array_key_exists('priority', $_POST)) {

? ? ? ? ? ? update_post_meta($post_id, 'priority', intval($_POST['priority']));

? ? ? ? }

? ? }


? ? function cw_add_postmeta_ordering_args( $args_sort_cw ) {

? ? ? $cw_orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) :

? ? ? ? ? ? apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

? ? ? switch( $cw_orderby_value ) {

? ? ? ? ? ?case 'priority':

? ? ? ? ? ? ? ? $args_sort_cw['orderby'] = 'meta_value';

? ? ? ? ? ? ? ? $args_sort_cw['order'] = 'desc';

? ? ? ? ? ? ? ? $args_sort_cw['meta_key'] = 'priority';

? ? ? ? ? ? ? ? break;

? ? ? }

? ? ? return $args_sort_cw;

? ? }

? ? add_filter( 'woocommerce_get_catalog_ordering_args', 'cw_add_postmeta_ordering_args' );

? ? function cw_add_new_postmeta_orderby( $sortby ) {

? ? ? ?$sortby['priority'] = __( 'Recommended', 'woocommerce' );

? ? ? ?return $sortby;

? ? }

? ? add_filter( 'woocommerce_default_catalog_orderby_options', 'cw_add_new_postmeta_orderby' );

? ? add_filter( 'woocommerce_catalog_orderby', 'cw_add_new_postmeta_orderby' );

調(diào)整了該woocommerce_product_custom_fields_save()功能array_key_exists(),現(xiàn)在工作正常。


查看完整回答
反對 回復(fù) 2023-11-03
  • 2 回答
  • 0 關(guān)注
  • 186 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號