VanboDevelops'答えは非常に便利だったので、私は先に行って、受け入れ答えとしてそれをマーク。しかし、私はそれをやり終えたのか少し違っていました。私は、テストのためにだけでなく、注文がキャンセルされて印刷番号をロールバックする必要がある場合にも、限定版の番号を簡単に編集できるようにしたかったのです。私にとってので、ここで私はfunctions.php
に追加何だ、解決策は簡単などの価格、大きさ、一緒に編集することができますすべてのバリエーションにカスタムフィールドを作成することでした:
このソリューションはまたかどうか、複数の考慮
add_action('woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3);
function variation_settings_fields($loop, $variation_data, $variation) {
woocommerce_wp_text_input(
array(
'id' => '_print_number[' . $variation->ID . ']',
'label' => __('Print number (most recently sold)', 'woocommerce'),
'desc_tip' => 'true',
'description' => __('The most recently-sold Print Number for this size and print. WARNING: This value plus 1 is used to show the user which print number they will be purchasing. Only update this value manually in specific situations, such as if an order is canceled and the print number needs to be offset.', 'woocommerce'),
'value' => get_post_meta($variation->ID, '_print_number', true),
'custom_attributes' => array(
'step' => '1',
'min' => '1'
)
)
);
}
add_action('woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2);
function save_variation_settings_fields($post_id) {
$number_field = $_POST['_print_number'][ $post_id ];
if(!empty($number_field) || $number_field == 0) {
update_post_meta($post_id, '_print_number', esc_attr($number_field));
}
}
add_filter('woocommerce_available_variation', 'load_variation_settings_fields');
function load_variation_settings_fields($variations) {
$variations['print_number'] = get_post_meta($variations[ 'variation_id' ], '_print_number', true);
return $variations;
}
add_action('woocommerce_checkout_update_order_meta', 'prefix_add_order_meta', 10, 2);
function prefix_add_order_meta($order_id, $data) {
// Generate order number
$order = wc_get_order($order_id);
// Getting the items in the order
$order_items = $order->get_items();
foreach ($order_items as $item_id => $item_data) {
$product = $item_data->get_product();
$product_id = $product->get_id();
// Update print number
$print_number = (int) get_post_meta($product_id, '_print_number', true);
$print_number += wc_clean($item_data['qty']);
update_post_meta($product_id, '_print_number', wc_clean($print_number));
// Add to order meta
$order->add_meta_data($product_id . '_print_number', wc_clean($print_number), true);
$order->save();
}
}
選択されたパターンのうちの1つが注文に含まれているため、たとえば、同じ写真の2枚の小さなプリントを選んだ場合、そのパターンのプリント番号は2ずつ増えます。このような場合、注文メタ情報には最新のプリント番号のみが表示されますが、数量と最終プリント番号を調べることで、出荷する番号を推測することができます。より堅牢なソリューションは、オーダーのメタデータにすべてのプリント番号を表示するのが理想的ですが、これは私の思うように機能します。
ありがとうございました!これは素晴らしく、私はついにそれを試す時間がありました。新製品の注文を完了した後、注文メタおよび注文項目メタは両方とも、印刷番号「1」で正しく設定されている。しかし、製品のページに行くと、カスタムフィールドには「custom_total_sales」はありません。これはバリエーションごとに処理されますか?そうであれば、設定されているかどうかを確認する別の領域がありますか? –
product-> get_meta()でカスタムフィールドを取得してみてください。あなたが販売している製品がバリエーションであれば、はい、バリエーションにメタが保存されます。 – VanboDevelops
クールなので、テストを終えてライブに備える準備が整ったら、バリエーションの 'custom_total_sales'をWordPressの管理者から0にリセットする方法はありますか? –