オプションのニュースレターサブスクリプション用に、WooCommerceショップのチェックアウトページにカスタムチェックボックスを追加しました。
add_action( 'woocommerce_after_order_notes', 'add_checkout_newsletter_subscribe', 9 );
function add_checkout_newsletter_subscribe() {
woocommerce_form_field( 'checkout_newsletter', array(
'type' => 'checkbox',
'class' => array('form-row checkout-newsletter-box'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => false,
'label' => 'Subscribe me.',
));
}
[マイアカウント]ページにも同様のチェックボックスを作成しました。
add_action( 'woocommerce_edit_account_form', 'display_checkbox_in_account_page' );
function display_checkbox_in_account_page() {
woocommerce_form_field( 'newsletter-account', array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => __( 'Subscribe me.', 'woocommerce' ),
'clear' => true,
), get_user_meta(get_current_user_id(), 'newsletter-account', true ) );
}
add_action( 'woocommerce_save_account_details', 'save_checkbox_value_to_account_details', 10, 1 );
function save_checkbox_value_to_account_details( $user_id ) {
$value = isset( $_POST['newsletter-account'] ) ? '1' : '0';
update_user_meta( $user_id, 'newsletter-account', $value );
}
今、私は2つのチェックボックスを接続する方法を探しています。
私が取得したいのは、顧客がチェックアウトページのチェックボックスにフラグを立て、それが登録ユーザーである場合、[マイアカウント]ページのチェックボックスにもフラグを立てる必要があるということです。
woocommerce_edit_account_formフックを使用する必要があると思いますが、残念ながら、そのような結果を取得する方法について他の手がかりはありません。
皆さんは私を正しい方向に向けることができますか?
ご助力ありがとうございます!
以下は、顧客がまだサブスクライブしていない場合、チェックアウトページのサブスクリプションチェックボックスを表示し、送信時にユーザーメタデータとして保存します。
// Display custom checkout field on checkout page
add_action( 'woocommerce_after_order_notes', 'add_checkout_newsletter_subscribe', 9 );
function add_checkout_newsletter_subscribe() {
$user_id = (int) get_current_user_id();
$newsletter = get_user_meta( $user_id, 'newsletter-account', true );
// If newsletter hasn't been subscribed yet we show the subscription checkbox on checkout
if ( $user_id > 0 && ! $newsletter ) :
woocommerce_form_field( 'checkout_newsletter', array(
'type' => 'checkbox',
'class' => array('form-row checkout-newsletter-box'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => false,
'label' => __('Subscribe me'),
) );
endif;
}
// Save custom checkout field value as user meta data
add_action('woocommerce_checkout_update_customer','custom_checkout_checkbox_update_customer', 10, 2 );
function custom_checkout_checkbox_update_customer( $customer, $data ){
if ( ! is_user_logged_in() || is_admin() )
return;
// Update user meta data
if ( isset($_POST['checkout_newsletter']) )
update_user_meta( $customer->get_id(), 'newsletter-account', '1' );
}
コードは、アクティブな子テーマ(またはアクティブなテーマ)のfunctions.phpファイルに入ります。テストされ、動作します。
それは私のアカウントページのあなたの質問コードで動作します:
add_action( 'woocommerce_edit_account_form', 'display_checkbox_in_account_page' );
function display_checkbox_in_account_page() {
woocommerce_form_field( 'newsletter-account', array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => __( 'Subscribe me.', 'woocommerce' ),
'clear' => true,
), get_user_meta(get_current_user_id(), 'newsletter-account', true ) );
}
add_action( 'woocommerce_save_account_details', 'save_checkbox_value_to_account_details', 10, 1 );
function save_checkbox_value_to_account_details( $user_id ) {
$value = isset( $_POST['newsletter-account'] ) ? '1' : '0';
update_user_meta( $user_id, 'newsletter-account', $value );
}
注文時にチェックアウトオプションがチェックされている場合、アカウント編集フォームのチェックボックスもチェックされます…
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加