使用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' );
但我不能为这个自定义字段获得任何 Value . 怎么可能?我做错了什么?
谢谢
1 回答
您的代码中存在许多错误和缺失的内容
1) SETTINGS:
Create a product attribute:
Add some value to this attribute:
and …
然后保存...
2)守则:
此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中 .
此代码适用于WooCommerce版本,测试范围为2.5到3.0