php - 根据 Woocommerce 中的自定义购物车项目数据自定义购物车项目小计

标签 php wordpress woocommerce cart price

基于WooCommerce modify cart totals programmatically ,我试图在“woocommerce_cart_product_subtotal”过滤器 Hook 中获取购物车元,但我被卡住了。我试过 $a_variable = $cart_item['length']; ,但它不起作用。

这是我的代码:

add_filter( 'woocommerce_cart_product_subtotal', 'filter_woocommerce_cart_product_subtotal', 10, 6); 
function filter_woocommerce_cart_product_subtotal( $product_subtotal, $product, $quantity, $instance) { 

$prisen = $product->get_price();
$rope_length =$cart_item['length'];
$product_subtotal = ($prisen*$rope_length)*$quantity;
return $product_subtotal; 
}; 

有人有什么想法吗?

最佳答案

你没有以正确的方式这样做,你需要以不同的方式重新思考......

这是一个完整的示例,它允许您保留默认商品价格(产品价格)并根据您的“长度”自定义购物车商品数据获取计算出的购物车商品小计:

// For testing (example): Add a dropdown to product page for lenght
add_action( 'woocommerce_before_add_to_cart_button', 'add_lenght_custom_field');
function add_lenght_custom_field() {
    echo '<div class="class_dropdown_length">
        <label for="rope_length">Select a length</label>
        <select id ="rope_length" name="rope_length">
            <option value="">1 m</option>
            <option value="5">5 m</option>
            <option value="10">10 m</option>
            <option value="25">25 m</option>
        </select>
    </div>';
}

// Add custom cart item data (lenght) on add to cart and calculate the new price
add_filter( 'woocommerce_add_cart_item_data', 'filter_woocommerce_add_cart_item_data', 10, 3 );
function filter_woocommerce_add_cart_item_data( $cart_item_data, $product_id, $variation_id) {
    if( isset($_POST['rope_length']) && ! empty($_POST['rope_length']) ) {
        $the_id  = $variation_id > 0 ? $variation_id : $product_id;

        $product = wc_get_product( $the_id );

        $length  = (float) esc_attr($_POST['rope_length']); // The chosen lenght

        // Add the dropdown value as custom cart item data
        $cart_item_data['custom'] = array(
            'length'     => $length, // The lenght value from custom field (if needed)
            'price'      => $product->get_price(), // The default product price
            'new_price'  => $product->get_price() * $length, // Calculated price from lenght
            'unique_key' => md5(microtime().rand()), // Make each item unique
        );
    }
    return $cart_item_data;
}

// Display the selected lenght value below cart item name
add_filter( 'woocommerce_cart_item_name', 'display_select_length_after_cart_item_name', 10, 3 );
function display_select_length_after_cart_item_name( $name, $cart_item, $cart_item_key ) {
    if( is_cart() && isset($cart_item['custom']['length']) ) {
        $name .= '<p>'.__("Lenght:") . ' ' . esc_html($cart_item['custom']['length']) . '</p>';
    }
    return $name;
}

// Display the default product price (instead of the calculated one)
add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );
function filter_woocommerce_cart_item_price( $product_price, $cart_item, $cart_item_key  ) {
    if( isset($cart_item['custom']['price']) ) {
        $product_price = wc_price( wc_get_price_to_display( $cart_item['data'], array('price' => $cart_item['custom']['price']) ) );
    }
    return $product_price;
}

// Customizing cart item price subtotal
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_calculated_price', 10, 1 );
function set_cart_item_calculated_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Required since Woocommerce version 3.2 for cart items properties changes
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Set the new calculated price based on lenght
        if( isset($cart_item['custom']['new_price']) ) {
            $cart_item['data']->set_price( $cart_item['custom']['new_price'] );
        }
    }
}

此代码位于事件子主题(或事件主题)的 function.php 文件中。经过测试并有效。

您可以看到产品价格与自定义计算的小计不同(价格 x 长度):

enter image description here

关于php - 根据 Woocommerce 中的自定义购物车项目数据自定义购物车项目小计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55219171/

相关文章:

php - 查询所有没有API的产品,在Woocommerce中使用wc_get_products

php - 网页编程原则

PHP HTTP_POST_VARS 问题

html - 固定元素内的垂直/水平居中

css - 页脚中的小部件无法正确对齐

wordpress - 页面显示wordpress中的所有页面内容

PHP 7.4 修剪字符串变量之间的空格

php - Laravel 4 - 将多个值插入数据透视表

php - 通过 WooCommerce 3+ 中特定类别的 Hook 更改产品价格

mysql - 恢复 woocommerce 订单