php – 在WooCommerce中设置变量产品的计算价格
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 在WooCommerce中设置变量产品的计算价格,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7299字,纯文字阅读大概需要11分钟。
内容图文
![php – 在WooCommerce中设置变量产品的计算价格](/upload/InfoBanner/zyjiaocheng/789/4d3c64ab902d4b6c8d8780c99f7f0991.jpg)
在Woocommerce中,我使用自定义字段来计算产品的价格,基于此代码 – Add custom fields to custom product calculated price in Woocommerce.感谢LoicTheAztec的帮助.
// Add a custom field before single add to cart
add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_price_field', 5 );
function custom_product_price_field(){
echo '<div class="custom-text text">
<h3>Rental</h3>
<label>Start Date:</label>
<input type="date" name="rental_date" value="" class="rental_date" />
<label>Period Rental:</label>
<select name="custom_price" class="custom_price">
<option value="30" selected="selected">2 days</option>
<option value="35">4 days</option>
</select>
</div>';
}
// Get custom field value, calculate new item price, save it as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 2 );
function add_custom_field_data( $cart_item_data, $product_id ){
if ( isset($_POST['rental_date']) && ! empty($_POST['rental_date']) ){
$cart_item_data['custom_data']['date'] = $_POST['rental_date'];
}
if ( isset($_POST['custom_price']) && ! empty($_POST['custom_price']) ){
$product = wc_get_product($product_id); // The WC_Product Object
$base_price = (float) $product->get_regular_price(); // Product reg price
$custom_price = (float) sanitize_text_field( $_POST['custom_price'] );
$cart_item_data['custom_data']['base_price'] = $base_price;
$cart_item_data['custom_data']['new_price'] = $base_price * $custom_price/100;
$cart_item_data['custom_data']['rental'] = $custom_price;
}
if ( isset($cart_item_data['custom_data']['new_price']) || isset($cart_item_data['custom_data']['date']) ){
$cart_item_data['custom_data']['unique_key'] = md5( microtime() . rand() ); // Make each item unique
}
return $cart_item_data;
}
// Set the new calculated cart item price
add_action( 'woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 20, 1 );
function extra_price_add_custom_price( $cart ) {
if ( is_admin() && !defined('DOING_AJAX') )
return;
foreach ( $cart->get_cart() as $cart_item ) {
if( isset($cart_item['custom_data']['new_price']) )
$cart_item['data']->set_price( (float) $cart_item['custom_data']['new_price'] );
}
}
// Display cart item custom price details
add_filter('woocommerce_cart_item_price', 'display_cart_items_custom_price_details', 20, 3 );
function display_cart_items_custom_price_details( $product_price, $cart_item, $cart_item_key ){
if( isset($cart_item['custom_data']['new_price']) ) {
$product = $cart_item['data'];
$new_price = $cart_item['custom_data']['new_price'];
$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $new_price ) ) ) . '<br>';
if( isset($cart_item['custom_data']['rental']) ) {
$product_price .= $cart_item['custom_data']['rental'] == '30' ? __("2 days") : __("4 days");
}
}
return $product_price;
}
// Display in cart item the selected date
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
if ( isset( $cart_item['custom_data']['date'] ) ){
$cart_item_data[] = array(
'name' => __("Chosen date", "woocommerce" ),
'value' => date('d.m.Y', strtotime($cart_item['custom_data']['date'])),
);
}
if ( isset( $cart_item['custom_data']['rental'] ) ){
$cart_item_data[] = array(
'name' => __("Period Rental", "woocommerce" ),
'value' => $cart_item['custom_data']['rental'] == '30' ? __("2 days") : __("4 days"),
);
}
return $cart_item_data;
}
我决定创建一个新问题,因为我认为在这种情况下它是合适的.
所有代码都完全正常,但存在问题.此代码适用于简单的产品,我有很多变化的产品.
此代码未显示变分产品的价格.它仅显示日期和期间.
如何解决这个问题呢?有什么想法吗?
解决方法:
更新
在您的代码中,您需要更改woocommerce_add_cart_item_data()函数,以处理产品变体.
还有其他一些其他参数可以在此钩子中用作$variation_id.
代码:
// Add a custom field before single add to cart
add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_price_field', 5 );
function custom_product_price_field(){
echo '<div class="custom-text text">
<h3>Rental</h3>
<label>Start Date:</label>
<input type="date" name="rental_date" value="" class="rental_date" />
<label>Period Rental:</label>
<select name="custom_price" class="custom_price">
<option value="30" selected="selected">2 days</option>
<option value="35">4 days</option>
</select>
</div>';
}
// Get custom field value, calculate new item price, save it as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 3 );
function add_custom_field_data( $cart_item_data, $product_id, $variation_id ){
if ( isset($_POST['rental_date']) && ! empty($_POST['rental_date']) ){
$cart_item_data['custom_data']['date'] = $_POST['rental_date'];
}
if ( isset($_POST['custom_price']) && ! empty($_POST['custom_price']) ){
$_product_id = $variation_id > 0 ? $variation_id : $product_id;
$product = wc_get_product($_product_id); // The WC_Product Object
$base_price = (float) $product->get_regular_price(); // Product reg price
$custom_price = (float) sanitize_text_field( $_POST['custom_price'] );
$cart_item_data['custom_data']['base_price'] = $base_price;
$cart_item_data['custom_data']['new_price'] = $base_price/100 * $custom_price;
$cart_item_data['custom_data']['rental'] = $custom_price;
}
if ( isset($cart_item_data['custom_data']['new_price']) || isset($cart_item_data['custom_data']['date']) ){
$cart_item_data['custom_data']['unique_key'] = md5( microtime() . rand() ); // Make each item unique
}
return $cart_item_data;
}
// Set the new calculated cart item price
add_action( 'woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 20, 1 );
function extra_price_add_custom_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
if( isset($cart_item['custom_data']['new_price']) )
$cart_item['data']->set_price( (float) $cart_item['custom_data']['new_price'] );
}
}
// Display cart item custom price details
add_filter('woocommerce_cart_item_price', 'display_cart_items_custom_price_details', 20, 3 );
function display_cart_items_custom_price_details( $product_price, $cart_item, $cart_item_key ){
if( isset($cart_item['custom_data']['base_price']) ) {
$product = $cart_item['data'];
$base_price = $cart_item['custom_data']['base_price'];
$product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $base_price ) ) ) . '<br>';
if( isset($cart_item['custom_data']['rental']) ) {
$product_price .= $cart_item['custom_data']['rental'] == '30' ? __("2 days") : __("4 days");
}
}
return $product_price;
}
// Display in cart item the selected date
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
if ( isset( $cart_item['custom_data']['date'] ) ){
$cart_item_data[] = array(
'name' => __("Chosen date", "woocommerce" ),
'value' => date('d.m.Y', strtotime($cart_item['custom_data']['date'])),
);
}
if ( isset( $cart_item['custom_data']['rental'] ) ){
$cart_item_data[] = array(
'name' => __("Period Rental", "woocommerce" ),
'value' => $cart_item['custom_data']['rental'] == '30' ? __("2 days") : __("4 days"),
);
}
return $cart_item_data;
}
代码位于活动子主题(或活动主题)的function.php文件中.经过测试和工作.
内容总结
以上是互联网集市为您收集整理的php – 在WooCommerce中设置变量产品的计算价格全部内容,希望文章能够帮你解决php – 在WooCommerce中设置变量产品的计算价格所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。