使用WooCommerce和我在我的functions.php文件中进行一些自定义,以获取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 );
我想在购物车页面上使用此自定义值我也使用了钩子
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
但我无法为此自定义字段获取任何值。
怎么可能?我做错了什么?
由于
答案 0 :(得分:6)
您的代码中存在许多错误和遗漏的内容
1)设置:
如果您想在客户订单和电子邮件通知上正确显示标签“T恤上的名称”,您需要为此标签名称创建一个属性(在产品&gt;属性下):
然后保存......
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 +