• Resolved roberto22

    (@roberto22)


    Hi,

    I have added a custom field in the order called “Cashback” https://prnt.sc/107369f as you can see here https://prnt.sc/107351n and here https://prnt.sc/10735dm using the following snippet:

    function kia_add_cart_item_data( $cart_item, $product_id ){
    if(get_post_meta($product_id,’cashback_’,true) != ”) {
    $cart_item[‘Cashback’] = get_post_meta($product_id,’cashback_’,true);
    }
    return $cart_item;
    }

    add_filter( ‘woocommerce_add_cart_item_data’, ‘kia_add_cart_item_data’, 10, 2 );
    function kia_get_cart_item_from_session( $cart_item, $values ) {
    if ( isset( $values[‘Cashback’] ) ){
    $cart_item[‘Cashback’] = $values[‘Cashback’];
    }
    return $cart_item;
    }

    add_filter( ‘woocommerce_get_cart_item_from_session’, ‘kia_get_cart_item_from_session’, 20, 2 ); function kia_add_order_item_meta( $item_id, $values ) {
    if ( ! empty( $values[‘Cashback’] ) ) {
    wc_add_order_item_meta( $item_id, ‘Cashback’, $values[‘Cashback’] );
    }
    }

    add_action( ‘woocommerce_add_order_item_meta’, ‘kia_add_order_item_meta’, 10, 2 ); function kia_get_item_data( $other_data, $cart_item ) {
    if ( isset( $cart_item[‘Cashback’] ) ){
    $other_data[] = array( ‘key’ => __( ‘Cashback’, ‘kia-plugin-textdomain’ ), ‘display’ => sanitize_text_field( $cart_item[‘Cashback’] ) );
    }
    return $other_data;
    }

    add_filter( ‘woocommerce_get_item_data’, ‘kia_get_item_data’, 10, 2 ); function kia_order_item_product( $product, $order_item ){
    if( $order_item->get_meta( ‘Cashback’ ) ){
    $product->add_meta_data( ‘Cashback’, $order_item->get_meta( ‘Cashback’ ), true );
    }
    return $product;
    }

    add_filter( ‘woocommerce_order_item_product’, ‘kia_order_item_product’, 10, 2 );`

    I am trying to also add the field “Costo de envio” https://prnt.sc/1073828 but if i try to use thesame snippet i get an error saying that Cannot redeclare the kia_add_cart_item_data function.

    https://prnt.sc/1073b86

    I dont know much about coding. Can you please tell me what i need to change so i can add the second custom field value

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there ??

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers!

    Nico

    (@nicolamustone)

    Automattic Happiness Engineer

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘custom field in order’ is closed to new replies.