我想根据单个产品页面中的输入隐藏字段,在将产品添加到购物车时设置自定义价格。
我尝试使用此代码更改购物车中的产品价格:
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = $_POST['_custom_price'];
foreach ( $cart_object->cart_contents as $key => $value ) {
// for WooCommerce version 3+ use:
$value['data']->set_price($custom_price);
}
}
但我得到零价。
这是my test page。任何帮助表示赞赏。
感谢您的帮助,但它很有效...它给出了一个错误 这是我的function.php
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
if(isset($_POST['_custom_price'] )){
$custom_price = $_POST['_custom_price'];
foreach ( $cart_object->cart_contents as $key => $value ) {
// for WooCommerce version 3+ use:
$value['data']->set_price($custom_price);
$_SESSION['custom_price']=$custom_price;
}
}else{
}
}
// STACK HELP CODE
/*
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_price', 20, 2 );
function add_custom_price( $cart_item_data, $product_id ){
// Only when option is passed through the URL
if( ! isset($_POST['_custom_price']) && empty($_POST['_custom_price']) )
return $cart_item_data;
$cart_item_data['custom_price'] = sanitize_text_field( $_POST['_custom_price'] );
return $cart_item_data;
}
add_action( 'woocommerce_before_calculate_totals', 'set_custom_price' );
function set_custom_price( $cart ) {
foreach ( $cart->get_cart() as $cart_item ) {
if( isset($cart_item['custom_price']) ){
$value['data']->set_price($cart_item['custom_price']);
}
}
}
*/
?>
<?php
/*
* Display input on single product page
* @return html
*/
function kia_custom_option(){
$value = isset( $_POST['_custom_option'] ) ? sanitize_text_field( $_POST['_custom_option'] ) : '';
printf( '<label>%s</label><input id="hotel_chambre_selected" name="_custom_option" value="%s" />', __( '', 'kia-plugin-textdomain' ), esc_attr( $value ) );
}
add_action( 'woocommerce_before_add_to_cart_button', 'kia_custom_option', 9 );
/*
* Validate when adding to cart
* @param bool $passed
* @param int $product_id
* @param int $quantity
* @return bool
*/
function kia_add_to_cart_validation($passed, $product_id, $qty){
if( isset( $_POST['_custom_option'] ) && sanitize_text_field( $_POST['_custom_option'] ) == '' ){
$product = wc_get_product( $product_id );
wc_add_notice( sprintf( __( '%s cannot be added to the cart until you enter some custom text.', 'kia-plugin-textdomain' ), $product->get_title() ), 'error' );
return false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'kia_add_to_cart_validation', 10, 3 );
/*
* Add custom data to the cart item
* @param array $cart_item
* @param int $product_id
* @return array
*/
function kia_add_cart_item_data( $cart_item, $product_id ){
if( isset( $_POST['_custom_option'] ) ) {
$cart_item['custom_option'] = sanitize_text_field( $_POST['_custom_option'] );
}
return $cart_item;
}
add_filter( 'woocommerce_add_cart_item_data', 'kia_add_cart_item_data', 10, 2 );
/*
* Load cart data from session
* @param array $cart_item
* @param array $other_data
* @return array
*/
function kia_get_cart_item_from_session( $cart_item, $values ) {
if ( isset( $values['custom_option'] ) ){
$cart_item['custom_option'] = $values['custom_option'];
}
return $cart_item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'kia_get_cart_item_from_session', 20, 2 );
/*
* Add meta to order item
* @param int $item_id
* @param array $values
* @return void
*/
function kia_add_order_item_meta( $item_id, $values ) {
if ( ! empty( $values['custom_option'] ) ) {
woocommerce_add_order_item_meta( $item_id, 'custom_option', $values['custom_option'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'kia_add_order_item_meta', 10, 2 );
/*
* Get item data to display in cart
* @param array $other_data
* @param array $cart_item
* @return array
*/
function kia_get_item_data( $other_data, $cart_item ) {
if ( isset( $cart_item['custom_option'] ) ){
$other_data[] = array(
'name' => __( 'Votre chambre ', 'kia-plugin-textdomain' ),
'value' => sanitize_text_field( $cart_item['custom_option'] )
);
}
return $other_data;
}
add_filter( 'woocommerce_get_item_data', 'kia_get_item_data', 10, 2 );
/*
* Show custom field in order overview
* @param array $cart_item
* @param array $order_item
* @return array
*/
function kia_order_item_product( $cart_item, $order_item ){
if( isset( $order_item['custom_option'] ) ){
$cart_item_meta['custom_option'] = $order_item['custom_option'];
}
return $cart_item;
}
add_filter( 'woocommerce_order_item_product', 'kia_order_item_product', 10, 2 );
/*
* Add the field to order emails
* @param array $keys
* @return array
*/
function kia_email_order_meta_fields( $fields ) {
$fields['custom_field'] = __( 'Votre chambre ', 'kia-plugin-textdomain' );
return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'kia_email_order_meta_fields');
/*
* Order Again
* @param array $cart_item
* @param array $order_item
* @param obj $order
* @return array
*/
function kia_order_again_cart_item_data( $cart_item, $order_item, $order ){
if( isset( $order_item['custom_option'] ) ){
$cart_item_meta['custom_option'] = $order_item['custom_option'];
}
return $cart_item;
}
add_filter( 'woocommerce_order_again_cart_item_data', 'kia_order_again_cart_item_data', 10, 3 );
这是我的函数
中print_r($ _ POST)的result