donglou1866 2017-04-28 13:37
浏览 32

保存产品自定义字段并将其显示在购物车页面中

With WooCommerce and I am doing some customization in my functions.php file, to get a custom field value on Cart page.

I add a custom field before add to cart:

function add_name_on_tshirt_field() {
  echo '<table class="variations" cellspacing="0">
      <tbody>
          <tr>
          <td class="label"><label for="color">Name On T-Shirt</label></td>
          <td class="value">
              <input type="text" name="name-on-tshirt" value="" />
          </td>
      </tr>                             
      </tbody>
  </table>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_name_on_tshirt_field' );

function render_meta_on_cart_item( $title = null, $cart_item = null, $cart_item_key = null ) {
    if( $cart_item_key && is_cart() ) {
        echo $title. '<dl class="">
                 <dt class="">Name On T-Shirt : </dt>
                 <dd class=""><p>'. WC()->session->get( $cart_item_key.'_name_on_tshirt') .'</p></dd>           
              </dl>';
    }else {
        echo $title;
    }

  /*  $d=WC();
    echo "<pre>";
    print_r($d);*/
}
add_filter( 'woocommerce_cart_item_name', 'render_meta_on_cart_item', 1, 3 );


function tshirt_order_meta_handler( $item_id, $values, $cart_item_key ) 
{
    wc_add_order_item_meta( $item_id, "name_on_tshirt", WC()->session->get( $cart_item_key.'_name_on_tshirt') );    
}
add_action( 'woocommerce_add_order_item_meta', 'tshirt_order_meta_handler', 1, 3 );

And i want this custom value on cart page i also used hook

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

But I can't get any value for this custom field.

How it's possible? What I am doing wrong?

Thanks

  • 写回答

1条回答 默认 最新

  • dongqiao1158 2017-04-28 19:13
    关注

    There is a lot of errors and missing things in your code

    1) SETTINGS:

    If you want to display correctly the label "Name On T-Shirt" on customer Orders and email notifications, you need to create an attribute for this label name (under Products > Attributes):

    Create a product attribute:
    Create a product attribute

    Add some value to this attribute:
    Add some value to this attribute

    and …
    enter image description here

    Then save…

    2) The Code:

    // Add the custom field to product pages
    add_action( 'woocommerce_before_add_to_cart_button', 'add_nmy_custom_product_field', 10, 0 );
    function add_nmy_custom_product_field() {
        ?>
            <table class="variations" cellspacing="0">
                <tbody>
                    <tr>
                        <td class="label">
                            <label for="color"><?php _e('Name On T-Shirt', 'woocommerce'); ?></label>
                        </td>
                        <td class="value">
                            <input type="text" name="name-on-tshirt" value="" />
                        </td>
                    </tr>
                </tbody>
            </table>
        <?php
    }
    
    // Save the custom product field data in Cart item
    add_filter( 'woocommerce_add_cart_item_data', 'save_in_cart_my_custom_product_field', 10, 2 );
    function save_in_cart_my_custom_product_field( $cart_item_data, $product_id ) {
        if( isset( $_POST['name-on-tshirt'] ) ) {
            $cart_item_data[ 'name-on-tshirt' ] = $_POST['name-on-tshirt'];
    
            // When add to cart action make an unique line item
            $cart_item_data['unique_key'] = md5( microtime().rand() );
            WC()->session->set( 'custom_data', $_POST['name-on-tshirt'] );
        }
        return $cart_item_data;
    }
    
    // Render the custom product field in cart and checkout
    add_filter( 'woocommerce_get_item_data', 'render_custom_field_meta_on_cart_and_checkout', 10, 2 );
    function render_custom_field_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
    
        $custom_items = array();
    
        if( !empty( $cart_data ) )
            $custom_items = $cart_data;
    
        if( $custom_field_value = $cart_item['name-on-tshirt'] )
            $custom_items[] = array(
                'name'      => __( 'Name On T-Shirt', 'woocommerce' ),
                'value'     => $custom_field_value,
                'display'   => $custom_field_value,
            );
    
        return $custom_items;
    }
    
    // Add the the custom product field as item meta data in the order
    add_action( 'woocommerce_add_order_item_meta', 'tshirt_order_meta_handler', 10, 3 );
    function tshirt_order_meta_handler( $item_id, $cart_item, $cart_item_key ) {
        $custom_field_value = $cart_item['name-on-tshirt'];
        if( ! empty($custom_field_value) )
            wc_update_order_item_meta( $item_id, 'pa_name-on-tshirt', $custom_field_value );
    }
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code works and is tested for WooCommerce version from 2.5 to 3.0+

    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计