我正在尝试根据公式更改woocommerce产品的价格,但是对于每种产品,我都会根据所选的变化选项使用不同的公式。那么,有什么方法可以做到吗?
我已经使用JavaScript计算重量,所选的选项也已保存在订单和购物车及结帐页面中。我有以下代码,但它正在更改我所有产品的公式。
add_action('woocommerce_before_add_to_cart_button', 'custom_data_hidden_fields');
function custom_data_hidden_fields() {
global $product;
$id = $product->get_id();
if($id == 10 ){
echo '<div class="imput_fields custom-imput-fields">
<label class="age_prod">Age: <br>
<select name="customer_name" id="MrP">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</label>
<label class="quality_prod">Quality: <br>
<select name="customer_message" id="MrS">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</label>
<input name="weight" id="MrPS"/>
</div><br>
<script>
var MrP = document.getElementById( "MrP" ),
MrPValue = MrP.value;
var MrS = document.getElementById( "MrS" ),
MrSValue = MrS.value;
MrP.onchange=()=>calculate();
MrS.onchange=()=>calculate();
function calculate(){
MrPValue = MrP.value;
MrSValue = MrS.value;
var sum = 0;
var a = +MrPValue;
var b = +MrSValue;
sum = a + b;
document.getElementById("MrPS").value = sum;
}
calculate()
</script>
';
}}
/**
* Display the custom data on cart and checkout page
*/
add_filter( 'woocommerce_get_item_data', 'get_item_data' , 25, 2 );
function get_item_data ( $other_data, $cart_item ) {
if ( isset( $cart_item [ 'custom_data' ] ) ) {
$custom_data = $cart_item [ 'custom_data' ];
$other_data[] = array( 'name' => 'Name',
'display' => $custom_data['customer_name'] );
$other_data[] = array( 'name' => 'Message',
'display' => $custom_data['customer_message'] );
$other_data[] = array( 'name' => 'Weight',
'display' => $custom_data['weight'] );
}
return $other_data;
}
/**
* Add order item meta
*/
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta' , 10, 2);
function add_order_item_meta ( $item_id, $values ) {
if ( isset( $values [ 'custom_data' ] ) ) {
$custom_data = $values [ 'custom_data' ];
wc_add_order_item_meta( $item_id, 'Name', $custom_data['customer_name'] );
wc_add_order_item_meta( $item_id, 'Message', $custom_data['customer_message'] );
wc_add_order_item_meta( $item_id, 'Weight', $custom_data['weight'] );
}
}
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 25, 2 );
function add_cart_item_data( $cart_item_meta, $product_id ) {
if ( isset( $_POST ['customer_name'] ) && isset( $_POST ['customer_message'] ) && isset( $_POST ['weight'] ) ) {
$custom_data = array() ;
$custom_data [ 'customer_name' ] = isset( $_POST ['customer_name'] ) ? sanitize_text_field ( $_POST ['customer_name'] ) : "" ;
$custom_data [ 'customer_message' ] = isset( $_POST ['customer_message'] ) ? sanitize_text_field ( $_POST ['customer_message'] ): "" ;
$custom_data [ 'weight' ] = isset( $_POST ['weight'] ) ? sanitize_text_field ( $_POST ['weight'] ): "" ;
$cart_item_meta ['custom_data'] = $custom_data ;
}
return $cart_item_meta;
}
// Replace the item price by your custom calculation
add_action( 'woocommerce_before_calculate_totals', 'add_custom_item_price', 10 );
function add_custom_item_price( $cart ) {
$targeted_product_id = 10;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
// Product ID
$product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
// Product original price
$original_price = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->price : $cart_item['data']->get_price();
$original_price = $item_values['data']->price;
## --- Get your custom cart item data --- ##
if( isset($cart_item['custom_data']['customer_name']) )
$customer_name = $cart_item['custom_data']['customer_name'];
if( isset($cart_item['custom_data']['customer_message']) )
$customer_message = $cart_item['custom_data']['customer_message'];
// CALCULATION FOR EACH ITEM:
## Make HERE your own calculation to feet your needs <== <== <== <==
$new_price = $original_price + ( ($customer_name * $customer_message) );
## Set the new item price in cart
if( version_compare( WC_VERSION, '3.0', '<' ) )
$cart_item['data']->price = $new_price;
else
$cart_item['data']->set_price($new_price);
}
}