About Woocommerce product insertion hooks

Viewed 19

I added a field to the product display page. I did some research but was not successful.

I have a form element like the one below, it may be text in this picture.

https://prnt.sc/vql93m

I want to save a field as a post meta while creating the product.

Then I want to show the relevant meta on the order page, actually I did it partially, but I can't save the relevant data.

https://prnt.sc/vqlbfs

And I can only add to cart on the single product display page, all other sections should be removed.

https://prnt.sc/vql8mj

Hooks and codes I used.

add_action('woocommerce_before_add_to_cart_button', 'woocommerce_custom_fields_display');
function woocommerce_custom_fields_display()
{
    ?>
    <label>Product type:</label><br><input type="text" id="product-type" name="product-type"><br><br>
    <?php
}
 
add_action( 'woocommerce_before_order_itemmeta', 'so_32457241_before_order_itemmeta', 10, 3 );
function so_32457241_before_order_itemmeta( $item_id, $item, $_product ){
    $order = json_decode( $item );
    echo get_post_meta( $order['order_id'], 'product-type', true );
}

add_action( 'woocommerce_new_order', 'custom_woocommerce_order' );
function custom_woocommerce_order( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    
    var_dump( $order );

    var_dump( $_POST );

}


add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
function custome_add_to_cart() {
    $p_id=$_POST['product-type'];
    WC()->cart->add_to_cart( $p_id );
}
0 Answers
Related