当前位置: 首页 > 知识库问答 >
问题:

如果某些产品在购物车中,将带有验证的自定义选择字段添加到WooCommerce结账页面

慕容康安
2023-03-14

如果购物车中有某些产品,我希望在WooCommerce结账页面中添加一个带有验证的自定义选择字段。

使用当前代码检索自定义表单字段的值。但是,它在订单页面上显示“array”。

我有一个选择字段,它有一个选项1和0。选择选择字段后,我希望它在订单页面上显示1或0,但我无法这样做。

请指导我如何使用当前代码:

php prettyprint-override">//AMBASSADOR CUSTOM CHECKOUT FIELDS, CONTENT add marketplace for sameday TEST
add_action( 'woocommerce_after_checkout_billing_form', 'aym_custom_checkout_field' );
function aym_custom_checkout_field( $checkout ) {
     //Check if Product in Cart
     $prod_in_cart_17563 = aym_is_conditional_product_in_cart_17563( 212 );
     if ( $prod_in_cart_17563 === true ) {
    $domain  = 'wocommerce';
    $default = 'Y';

         woocommerce_form_field( '_my_field_name', array(
         'type'  => 'select',
         'class' => array( 'form-row-wide' ),
         'label' => __( 'Market Place - Please Select Y to complete this order' ),
        'required'    => true,
         'options' => array(
            'Y' => __('1'),
            'N' => __('0')
        ),'default' => $default), 

$checkout->get_value( '_my_field_name' ) );
     }
}

//AMBSSADOR BUNDLE add marketplace for same day pa rt 2
function aym_is_conditional_product_in_cart_17563( $product_id ) {
 //Check to see if user has product in cart
 global $woocommerce;

 //flag no product in cart
 $prod_in_cart_17563 = false;

 foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
     $_product = $values['data'];

     if ( $_product->id === $product_id ) {
         //product is in cart!
         $prod_in_cart_17563 = true;

     }
 }

 return $prod_in_cart_17563;
}

//process orders in order page

// Custom checkout fields validation
add_action( 'woocommerce_checkout_process', 'custom_checkout_field_process' );
function custom_checkout_field_process() {
    if ( isset($_POST['_my_field_name']) && empty($_POST['_my_field_name']) )
        wc_add_notice( __( 'Please fill in "My 1st new field".' ), 'error' );
}

// Save custom checkout fields the data to the order
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_meta', 10, 2 );
function custom_checkout_field_update_meta( $order, $data ){
    if( isset($_POST['_my_field_name']) && ! empty($_POST['_my_field_name']) )
        $order->update_meta_data( '_my_field_name', sanitize_text_field( $_POST['_my_field_name'] ) );
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['_my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['_my_field_name']));
}


// View fields in Edit Order Page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_fields_value_admin_order', 10, 1 );
function display_custom_fields_value_admin_order( $order ){
    // Display the delivery option
    if( $delivery_option =  $order->get_meta('_my_field_name') )
        echo '<p><strong>'.__('Delivery type').':</strong> ' . $delivery_option . '</p>';

}

// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta( $order ) {
    if( $selectoption = $order->get_meta('_my_field_name') ) {
        //$value = wc_get_hearaboutus_options()[$selectoptions];
        $meta= get_post_meta( $post->ID, $selectoption, true );
        $myvalues = unserialize( $meta );
         echo '<p><strong>'.__('Market_Place').':</strong> ' . $myvalues . '</p>';

       
    }
}

共有1个答案

艾原
2023-03-14

我已经用一些(额外的)功能部分重写/修改了您的代码

  • 能够在购物车中检查多个产品ID
// Add custom 'select' field after checkout billing form (if product ID is in cart) 
function action_woocommerce_after_checkout_billing_form( $checkout ) {
    // Check if Product in Cart
    // Multiple product IDs can be entered, separated by a comma
    $product_in_cart = is_product_in_cart( array( 212, 30, 815 ) );
    
    // True
    if ( $product_in_cart ) {
        $domain  = 'woocommerce';
        $default = 'Y';

        woocommerce_form_field( '_my_field_name', array(
            'type'      => 'select',
            'class'     => array( 'form-row-wide' ),
            'label'     => __( 'Market Place - Please Select Y to complete this order', $domain ),
            'required'  => true,
            'options'   => array(
                'Y' => __( 'Y', $domain ),
                'N' => __( 'N', $domain ),
            ),
            'default'   => $default,
        ), $checkout->get_value( '_my_field_name' ) );
     }
}
add_action( 'woocommerce_after_checkout_billing_form', 'action_woocommerce_after_checkout_billing_form', 10, 1 );

// Function to check if a certain product ID is in cart
function is_product_in_cart( $targeted_ids ) {
    // Flag no product in cart
    $flag = false;
    
    // WC Cart NOT null
    if ( ! is_null( WC()->cart ) ) {
        // Loop through cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            // Check cart item for defined product Ids
            if ( in_array( $cart_item['product_id'], $targeted_ids ) ) {
                // Product is in cart
                $flag = true;
                
                // Break loop
                break;
            }
        }
    }

    return $flag;
}

// Custom checkout 'select' field validation
function action_woocommerce_checkout_process() {
    // Isset    
    if ( isset( $_POST['_my_field_name'] ) ) {
        $domain = 'woocommerce';
        $my_field_name = $_POST['_my_field_name'];
        
        // Empty
        if ( empty ( $my_field_name ) ) {
            wc_add_notice( __( 'Please Select Y to complete this order', $domain ), 'error' );
        }
        
        // NOT empty but value is 'N'
        if ( ! empty ( $my_field_name ) && $my_field_name == 'N' ) {
            wc_add_notice( __( 'Please Select Y to complete this order', $domain ), 'error' );
        }       
    }
}
add_action( 'woocommerce_checkout_process', 'action_woocommerce_checkout_process', 10, 0 );

// Save custom checkout 'select' field
function action_woocommerce_checkout_create_order( $order, $data ) {
    // Isset    
    if ( isset( $_POST['_my_field_name'] ) ) {
        $my_field_name = $_POST['_my_field_name'];
    
        // NOT empty & equal to 'Y'
        if ( ! empty( $my_field_name ) && $my_field_name == 'Y' ) {
            $order->update_meta_data( '_my_field_name', sanitize_text_field( $my_field_name ) );
        }
    } 
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );

// Display the custom 'select' field value on admin order pages after billing adress
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
    $domain = 'woocommerce';
    
    // Get meta
    $my_field_name = $order->get_meta( '_my_field_name' );
    
    // NOT empty
    if ( ! empty ( $my_field_name ) ) {
        echo '<p><strong>' . __( 'Delivery type', $domain ) . ':</strong> ' . $order->get_meta( '_my_field_name' ) . '</p>';
    }
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );

// Display the custom 'select' field value on 'order received' and 'order view' pages (frontend)
function action_woocommerce_order_details_after_order_table( $order ) {
    $domain = 'woocommerce';
    
    // Get meta
    $my_field_name = $order->get_meta( '_my_field_name' );
    
    // NOT empty
    if ( ! empty ( $my_field_name ) ) {
        echo '<p><strong>' . __( 'Delivery type', $domain ) . ':</strong> ' . $order->get_meta( '_my_field_name' ) . '</p>';
    }
}
add_action( 'woocommerce_order_details_after_order_table', 'action_woocommerce_order_details_after_order_table', 10, 1 );
 类似资料:
  • 我正在尝试更改购物车和结帐页面中的产品名称。 我有以下代码添加一些购物车元数据: 但是我也想改变产品的名称。 例如,如果产品名称是,自定义字段值是,我想得到。 我该如何实现这一点?

  • 在Woocommerce结帐部分,我试图添加一个复选框,以添加其他产品。 我有一个工作代码,添加费用并在单击复选框时更新购物车,但我希望它添加一个产品而不是附加费: 这是复选框的代码 代码是有效的… 现在,我尝试更改代码以添加产品: 但是没有成功。任何帮助将不胜感激。

  • 我想添加一个addtionall自定义"添加到购物车按钮"上的单个产品页面有条件地使用此代码: 此变量是来自ACF的字段

  • 我正在尝试获取WooCommerce产品的自定义字段值,以显示在商店系统的不同页面上。我成功地对单个产品页面执行了此操作,但相同的代码不适用于购物车。 对于单个产品,我在函数中使用了此代码。php,它可以很好地工作: 我为购物车尝试了相同的代码,但这次值没有显示在页面上。我还提到了这个线程(获取自定义属性),将$post更改为$product,但仍然没有输出。。。 有什么建议吗?

  • 我的职责是编写以下代码: 有没有人能把我推向正确的方向,让我在购物车、结帐、邮件和订单中获得正确的值? 最初取自这里在WooCommerce 3中到处显示ACF产品自定义字段

  • WooCommerce中,非Ajax情况下,点击加入购物车按钮后跳转到何处可以通过filter:add_to_cart_redirect修改,下面代码可以实现产品加入购物车后直接结账的功能,跳过购物车页面。 如果你希望Shop首页或分类页中的加入购物车按钮也具备这种效果,不要勾选Enable AJAX add to cart buttons on archives功能。否则该代码只在单个产品页面有