Add a product add-on field to specific products on WooCommerce

一个人想着一个人 提交于 2021-02-17 03:22:57

问题


I need to change the code to not display text area on all of my WooCommerce products but only 2, this is on my WordPress child theme under functions.php file.

I have changed $product_id to $product_id = 2130(my specific product id), not sure if I'm supposed to change all the $product_id or $_POST for this code to display on only 2 products

It is for a specific product or products that gets personalized with a name. I have tried changing the code $product_id to numerous other extensions to no avail. I'm not sure if I should replace all the $product_id with what code, or should it be at $_POST and replace all of them.

add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_add_on', 9 );  
function custom_product_add_on() {
    $value = isset( $_POST['_custom_text_add_on'] ) ? sanitize_text_field( $_POST['_custom_text_add_on'] ) : '';

    echo '<div>
        <label>Custom Text Add-On <abbr class="required" title="required">*</abbr></label>
        <p>
             <input name="_custom_text_add_on" value="' . $value . '">
        </p>
    </div>';
}

add_filter( 'woocommerce_add_to_cart_validation', 'custom_product_add_on_validation', 10, 3 );
function custom_product_add_on_validation( $passed, $product_id, $qty ){
    if( isset( $_POST['_custom_text_add_on'] ) && sanitize_text_field( 
$_POST['_custom_text_add_on'] ) == '' ) {
         wc_add_notice( 'Custom Text Add-On is a required field', 'error' );
         $passed = false;
    }
    return $passed;
}

add_filter( 'woocommerce_add_cart_item_data', 'custom_product_add_on_cart_item_data', 10, 2 );
function custom_product_add_on_cart_item_data( $cart_item, $product_id ){
    if( isset( $_POST['_custom_text_add_on'] ) ) {
        $cart_item['custom_text_add_on'] = sanitize_text_field( 
$_POST['_custom_text_add_on'] );
    }
    return $cart_item;
}

add_filter( 'woocommerce_get_item_data', 'custom_product_add_on_display_cart', 10, 2 );
function custom_product_add_on_display_cart( $_data, $cart_item ) {
    if ( isset( $cart_item['custom_text_add_on'] ) ){
        $data[] = array(
            'name' => 'Custom Text Add-On',
            'value' => sanitize_text_field( $cart_item['custom_text_add_on'] )
        );
    }
    return $data;
}

add_action( 'woocommerce_add_order_item_meta', 'custom_product_add_on_order_item_meta', 10, 2 );
function custom_product_add_on_order_item_meta( $item_id, $values ) {
    if ( ! empty( $values['custom_text_add_on'] ) ) {
        wc_add_order_item_meta( $item_id, 'Custom Text Add-On', 
$values['custom_text_add_on'], true );
    }
}

add_filter( 'woocommerce_order_item_product', 'custom_product_add_on_display_order', 10, 2 );
function custom_product_add_on_display_order( $cart_item, $order_item ){
    if( isset( $order_item['custom_text_add_on'] ) ){
        $cart_item_meta['custom_text_add_on'] = 
$order_item['custom_text_add_on'];
    }
    return $cart_item;
}

add_filter( 'woocommerce_email_order_meta_fields', 'custom_product_add_on_display_emails' );
function custom_product_add_on_display_emails( $fields ) { 
    $fields['custom_text_add_on'] = 'Custom Text Add-On';
    return $fields; 
}

回答1:


I have revisited your code mostly everywhere, making some changes and removing unnecessary functions.

You will have to define your wanted product IDs in the first 2 functions, in $defined_products_ids variable array.

I have added add a space of 12px between dropdowns / add-on and add to cart button in variable products.

The complete code:

// Display Product add on custom fields
add_action( 'woocommerce_before_add_to_cart_button', 'display_product_add_on_custom_fields', 9 );
function display_product_add_on_custom_fields() {
    global $product;

    $defined_products_ids = array( 37, 53 );

    if( in_array($product->get_id(), $defined_products_ids) ) {
        $value = isset($_POST['custom_text']) ? $_POST['custom_text'] : '';

        // Adding some space between dropdowns and add to cart button for variable products
        $style = $product->is_type('variable') ? ' style="padding-bottom:12px;"' : '';

        echo '<div class="product-add-on"' . $style . '>
            <label>'. __('Add your customized text', 'woocommerce') . ' ' .
                '<abbr class="required" title="required">*</abbr>
            </label>
            <p>
                <input name="custom_text" value="' . $value . '">
            </p>
        </div>';
    }
}

// Product add on custom fields validation
add_filter( 'woocommerce_add_to_cart_validation', 'product_add_on_custom_fields_validation', 10, 3 );
function product_add_on_custom_fields_validation( $passed, $product_id, $quantity ){
    $defined_products_ids = array( 37, 53 );

    if( isset( $_POST['custom_text'] ) && empty( $_POST['custom_text'] )
    && in_array($product_id, $defined_products_ids) ) {
         wc_add_notice( '"Custom Text" is a required field', 'error' );
         $passed = false;
    }
    return $passed;
}

// Add custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 10, 2 );
function add_custom_cart_item_data( $cart_item_data, $product_id ){
    if( isset($_POST['custom_text']) ) {
        $cart_item_data['custom_text'] = sanitize_text_field( $_POST['custom_text'] );
        $cart_item_data['unique_key'] = md5( microtime().rand() ); // Make each item unique
    }
    return $cart_item_data;
}

// Display custom cart item data on cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2 );
function display_custom_cart_item_data( $cart_item_data, $cart_item ) {
    if ( isset( $cart_item['custom_text'] ) ){
        $cart_item_data[] = array(
            'name' => __('Your custom text', 'woocommerce'),
            'value' => $cart_item['custom_text'] // Already sanitized field
        );
    }
    return $cart_item_data;
}

// Save and display custom item data everywhere on orders and email notifications
add_action( 'woocommerce_checkout_create_order_line_item', 'add_product_custom_field_as_order_item_meta', 10, 4 );
function add_product_custom_field_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if ( isset($values['custom_text']) ) {
        $item->update_meta_data('Your custom text', $values['custom_text'] );
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and work.



来源:https://stackoverflow.com/questions/56220142/add-a-product-add-on-field-to-specific-products-on-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!