Wordpressを使用してサイトを管理しています。
決済ページにWoocommerceを使用しておりまして、
お客さんが受け取る注文確認メールに返金条件(各商品ごとにそれぞれ異なる)が表示されるように設定したく、
以下のようなコードを記述しましたが、
php
1 2add_action('woocommerce_email_order_meta', 'add_cancel_condition_to_email_order_meta', 10, 4); 3 4function add_cancel_condition_to_email_order_meta() { 5 6 if (wc_product1_is_in_the_cart()) { 7 8 echo 'Refundable within 10 days.'; 9 10 } elseif (wc_product2_is_in_the_cart()) { 11 12 echo 'Refundable within 30 days.'; 13 14 } else { 15 16 echo 'Non refundable.'; 17 } 18}
動作確認のため、Product2を注文・確定すると、確認メールには「Non refundable.」と表示されてしまいます。
補足
上記のプログラムの直前に、
php
1 2add_action('woocommerce_email_order_meta', 'my_custom_checkout_field_order_meta_keys', 10, 4); 3 4function my_custom_checkout_field_order_meta_keys($order) 5{ 6 ..... 7}
という記述があります。
また、wc_product1_is_in_the_cart()の定義は以下の通りです。
php
1function wc_product1_is_in_the_cart() 2{ 3 // Add your special product IDs here 4 $ids = array( 5 '6231', 6 '6277', 7 '6233', 8 '6278', 9 '6234', 10 );; 11 12 // Products currently in the cart 13 $cart_ids = array(); 14 15 // Find each product in the cart and add it to the $cart_ids array 16 foreach (WC()->cart->get_cart() as $cart_item_key => $values) { 17 $cart_product = $values['data']; 18 $cart_ids[] = $cart_product->get_id(); 19 } 20 21 // If one of the special products are in the cart, return true. 22 if (!empty(array_intersect($ids, $cart_ids))) { 23 return true; 24 } else { 25 return false; 26 } 27}
他に必要な情報がありましたら、追加で記述いたします。
どなたかお力を貸していただければ幸いです。