【问题标题】:Saving a product custom field and displaying it in cart page保存产品自定义字段并将其显示在购物车页面中
【发布时间】:2017-09-28 04:16:49
【问题描述】:

使用 WooCommerce,我正在我的 functions.php 文件中进行一些自定义,以在购物车页面上获取自定义字段值。

我在添加到购物车之前添加了一个自定义字段:

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 );

我想在购物车页面上使用这个自定义值,我也使用了钩子

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

但我无法获得此自定义字段的任何值。

这怎么可能?我做错了什么?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce cart hook-woocommerce


    【解决方案1】:

    您的代码中有很多错误和遗漏的东西

    1) 设置:

    如果您想在客户订单和电子邮件通知中正确显示标签 “T 恤上的名称”,您需要为此标签名称创建一个属性(在产品 > 属性下):

    创建产品属性:

    为该属性添加一些值:

    还有……

    然后保存……

    2) 代码:

    // 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 );
    }
    

    此代码在您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

    此代码适用于 WooCommerce 版本,从 2.5 到 3.0+ 进行测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      • 2021-07-24
      • 2016-04-03
      • 1970-01-01
      • 2018-08-28
      • 2018-07-16
      相关资源
      最近更新 更多