在WooCommerce中设置可变产品的计算价格 [英] Set a calculated price for variable products in the WooCommerce

查看:91
本文介绍了在WooCommerce中设置可变产品的计算价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我根据以下代码使用自定义字段来计算产品的价格-



此代码未显示价格各种产品。它仅显示日期和期间。





如何解决这个问题?有什么想法吗?

解决方案

已更新



在您的代码中,您需要更改 woocommerce_add_cart_item_data()函数,以处理产品变化。



还有其他在此钩子中可以用作 $ variation_id 的其他一些参数。



代码:

  //在单个添加到购物车之前添加自定义字段
add_action('woocommerce_before_add_to_cart_button','custom_product_price_field',5);
函数custom_product_price_field(){
echo’< div class = custom-text text>
< h3< / h3>
< label>开始日期:< / label>
< input type = date name = rental_date value = class = rental_date />
< label>租期:< / label>
<选择名称= custom_price class = custom_price>
< option value = 30 selected = selected> 2天< / option>
< option value = 35> 4天< / option>
< / select>
< / div>’;
}

//获取自定义字段值,计算新商品价格,将其另存为自定义购物车商品数据
add_filter('woocommerce_add_cart_item_data','add_custom_field_data',20,3) ;
函数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); // WC_Product对象
$ base_price =(浮动)$ product-> get_regular_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()); //使每个项目都是唯一的
}
返回$ cart_item_data;
}

//设置新的计算出的购物车价格
add_action('woocommerce_before_calculate_totals','extra_price_add_custom_price',20,1);
函数extra_price_add_custom_price($ cart){
if(is_admin()&&!define('DOING_AJAX'))
return;

if(did_action(‘woocommerce_before_calculate_totals')> == 2)
return;

foreach($ cart-> get_cart()as $ cart_item){
if(isset($ set_item ['custom_data'] ['new_price']))
$ cart_item ['data']-> set_price((float)$ cart_item ['custom_data'] ['new_price']);
}
}

//显示购物车商品自定义价格详细信息
add_filter('woocommerce_cart_item_price',‘display_cart_items_custom_price_details',20,3);
函数display_cart_items_custom_price_details($ product_price,$ cart_item,$ cart_item_key){
if(isset($ cart_item ['custom_data'] ['base_price'])){
$ product = $ cart_item [ '数据'];
$ 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($ set_item ['custom_data'] ['rental'])){
$ product_price。= $ cart_item ['custom_data'] ['rental'] ==‘30’吗? __( 2天):__( 4天);
}
}
返回$ product_price;
}

//在购物车商品中显示所选日期
add_filter(woowoo_get_item_data,display_custom_item_data,10,2);
函数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('dmY',strtotime($ cart_item ['custom_data'] ['date'])),
);
}
if(isset($ cart_item ['custom_data'] ['rental'])){
$ cart_item_data [] = array(
'name'=> __ (期间租赁, woocommerce),
'值'=> $ cart_item ['custom_data'] ['出租'] =='30'?__( 2天):__( 4天),
);
}
返回$ cart_item_data;
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效。




In Woocommerce, I use custom fields to calculate the price of a product, based on this code - Add custom fields to custom product calculated price in Woocommerce. Thanks for the help 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;
}

I decided to create a new question, because I think it is appropriate in this case.

All the code is completely working, but there is a problem. This code works for simple products, and I have a lot of variational products.

This code does not show the price of variational products. It shows only the date and the period.

How to solve this problem? Any thoughts?

解决方案

Updated

In your code, you need to change woocommerce_add_cart_item_data() function, to handle product variations.

There is other some other arguments that can be used as $variation_id in this hook.

The code:

// 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;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在WooCommerce中设置可变产品的计算价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆