我在woocommerce的单个产品页面中创建了自定义字段,用户可以在其中分别输入值高度和宽度,因为输入值也显示在购物车和结帐页面中。示例:如果用户输入height=\'15\'和width=\'20\',则其显示为购物车页面,如height=15 width=20
现在我试图实现的是,用公式计算高度和宽度,得到最终价格,最终价格应该在购物车和结账页面中更新
总价=(高度/3*宽度/30+3)*1.48
我无法完成此部分,或者我不知道如何完成此部分
到目前为止我试过的代码
/*
* Display input on single product page
* @return html
*/
function kia_satish_option(){
$value = isset( $_POST[\'_satish_option\'] ) ? sanitize_wp_checkbox( $_POST[\'_satish_option\'] ) : \'\';
printf( \'<label>%s</label><input name="_satish_option" value="%s" type="number"/>\', __( \'Height\', \'kia-plugin-textdomain\' ), esc_attr( $value ) );
}
add_action( \'woocommerce_before_add_to_cart_button\', \'kia_satish_option\', 9 );
function kia_satisher_option(){
$value = isset( $_POST[\'_satisher_option\'] ) ? sanitize_wp_checkbox( $_POST[\'_satisher_option\'] ) : \'\';
printf( \'<label>%s</label><input name="_satisher_option" value="%s" type="number"/>\', __( \'width\', \'kia-plugin-textdomain\' ), esc_attr( $value ) );
}
add_action( \'woocommerce_before_add_to_cart_button\', \'kia_satisher_option\', 9 );
function kia_yard_option(){
$value = isset( $_POST[\'_yard_option\'] ) ? sanitize_wp_checkbox( $_POST[\'_yard_option\'] ) : \'\';
printf( \'<label>%s</label><input name="_yard_option" value="%s" type="number"/>\', __( \'yard\', \'kia-plugin-textdomain\' ), esc_attr( $value ) );
}
add_action( \'woocommerce_before_add_to_cart_button\', \'kia_yard_option\', 9 );
/*
* 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[\'_satish_option\'] ) ) {
$cart_item[\'satish_option\'] = sanitize_text_field( $_POST[\'_satish_option\'] );
}
if( isset( $_POST[\'_satisher_option\'] ) ) {
$cart_item[\'satisher_option\'] = sanitize_text_field( $_POST[\'_satisher_option\'] );
}
if( isset( $_POST[\'_yard_option\'] ) ) {
$cart_item[\'yard_option\'] = sanitize_text_field( $_POST[\'_yard_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[\'satish_option\'] ) ){
$cart_item[\'satish_option\'] = $values[\'satish_option\'];
}
if ( isset( $values[\'satisher_option\'] ) ){
$cart_item[\'satisher_option\'] = $values[\'satisher_option\'];
}
if ( isset( $values[\'yard_option\'] ) ){
$cart_item[\'yard_option\'] = $values[\'yard_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[\'satish_option\'] ) ) {
woocommerce_add_order_item_meta( $item_id, \'satish_option\', $values[\'satish_option\'] );
}
if ( ! empty( $values[\'satisher_option\'] ) ) {
woocommerce_add_order_item_meta( $item_id, \'satisher_option\', $values[\'satisher_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[\'satish_option\'] ) ){
$other_data[] = array(
\'name\' => __( \'height\', \'kia-plugin-textdomain\' ),
\'value\' => sanitize_text_field( $cart_item[\'satish_option\'] )
);
}
if ( isset( $cart_item[\'satisher_option\'] ) ){
$other_data[] = array(
\'name\' => __( \'width\', \'kia-plugin-textdomain\' ),
\'value\' => sanitize_text_field( $cart_item[\'satisher_option\'] )
);
}
if ( isset( $cart_item[\'yard_option\'] ) ){
$other_data[] = array(
\'name\' => __( \'Yard\', \'kia-plugin-textdomain\' ),
\'value\' => sanitize_text_field( $cart_item[\'yard_option\'] )
);
}
return $other_data;
}
add_filter( \'woocommerce_get_item_data\', \'kia_get_item_data\', 10, 2 );
i tried to achieve formula section by code below
function woo_add_custom_general_fields_save($post_id)
{
$woocommerce_product_height = $_POST[\'_product_height\'];
$woocommerce_product_width = $_POST[\'_product_width\'];
$woocommerce_final_price = $_POST[\'_final_price\'];
// calculate and save _product_area_price, _regular_price, price as Height*Width
if (!empty($woocommerce_product_height) && !empty($woocommerce_product_width)))
$woocommerce_final_price = $woocommerce_product_height * $woocommerce_product_width ;
但这段代码似乎与我的代码完全无关
最合适的回答,由SO网友:TurtleTread 整理而成
我只是最近才谈到这个。
因此,请使用woocommerce_add_cart_item_data
滤器我看你只接受了两个论点。如果这是variable product,则过滤器中需要所有4个参数。
然后我们进行定价覆盖。您不能使用woocommerce_add_to_cart
在产品数据未转入会话购物车时执行定价覆盖。您需要使用woocommerce_before_calculate_totals
钩子遍历购物车内容并更改产品价格。
此代码直接取自此公认答案:https://stackoverflow.com/questions/43324605/change-cart-item-prices-in-woocommerce-version-3-0
add_action( \'woocommerce_before_calculate_totals\', \'add_custom_price\', 10, 1);
function add_custom_price( $cart_obj ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( \'DOING_AJAX\' ) )
return;
foreach ( $cart_obj->get_cart() as $key => $value ) {
$value[\'data\']->set_price( 40 );
}
}
要详细说明一下,请查看
class-wc-cart.php
\'s
add_to_cart()
方法查看每个内容项存储的内容。基本上
$value[\'data\']
在上面的代码中,是为每个产品存储的产品。您要检查
$value[\'your_custom_info_key\']
您之前在该项目数据过滤器中设置以获取自定义信息,其中
your_custom_info_key
不应为以下任何内容,因为这些内容由WooCommerce本身使用。
\'key\' => $cart_item_key,
\'product_id\' => $product_id,
\'variation_id\' => $variation_id,
\'variation\' => $variation,
\'quantity\' => $quantity,
\'data\' => $product_data,