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

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

將所有特定訂單商品自定義字段的總和保存為WooCommerce中的訂單元數(shù)據(jù)

將所有特定訂單商品自定義字段的總和保存為WooCommerce中的訂單元數(shù)據(jù)

PHP
慕桂英3389331 2021-05-06 14:19:08
下面我剛才的問題“ 保存WooCommerce訂單項(xiàng)目自定義字段和作為一個(gè)新的元數(shù)據(jù)”,當(dāng)一個(gè)訂單時(shí),一些訂單項(xiàng)目自定義元數(shù)據(jù)quantity,assemblycost以及calculated_field (哪個(gè)值quantityX assemblycost)保存。如何將所有訂單項(xiàng)calculated_field值的總和保存為自定義訂單元數(shù)據(jù)?例如,示例訂單如下所示:Product A:assemblycost: 10quantity: 2calculated_field: 20Product B:assemblycost: 15quantity: 2calculated_field: 30Product C: no assemblycost, quanity or calculated_field - custom fields present. Product D: assemblycost: 30quantity: 2calculated_field: 60我想為order創(chuàng)建一個(gè)新的自定義字段sum_calculated_fields,并將其設(shè)置為等于該訂單中的總和calculated_fields,因此在上面的示例中它等于:20 + 30 + 60 = 110。
查看完整描述

1 回答

?
莫回?zé)o

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

這是calculated_fields匯總所有訂單項(xiàng)自定義字段并將該總和保存為自定義訂單元數(shù)據(jù)的方法:


add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order_callback', 10, 2 );

function action_checkout_create_order_callback( $order, $data ) {

    $calculated_fields_sum = 0; // Initializing


    // Loop Through order items

    foreach ( $order->get_items() as $item ) {

        if( $value = $item->get_meta('calculated_field') ) {

            $calculated_fields_sum += $value;

        }

    }

    // Update order meta data

    if( $calculated_fields_sum > 0 ) {

        $order->update_meta_data( 'calculated_fields_sum', $calculated_fields_sum );

    }

}

代碼進(jìn)入您的活動(dòng)子主題(或活動(dòng)主題)的function.php文件中。它應(yīng)該工作。


查看完整回答
反對(duì) 回復(fù) 2021-05-21
  • 1 回答
  • 0 關(guān)注
  • 177 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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