首页 > 解决方案 > 通过 WooCommerce 中的 URL (GET) 设置自定义添加到购物车的价格

问题描述

我正在开发一个基于 wordpress 和 woocommerce 的网站,该网站提供有关烹饪相关培训的信息并出售各种厨房材料。

那些想参加培训的人通过填写表格申请。厨房用品也通过woocommerce出售。

培训通过一种称为培训的内容添加到网站。

要求通过 woocommerce 结构出售一些培训。但是,这些想要出售的“培训”希望以教育内容的形式保留。此外,要求不要作为产品添加或移动。

首先,我创建了一个名为 Education 的虚拟产品。我把产品藏在商店里。

然后我为教程添加了一个名为 price 的自定义字段。将在此处输入要出售的每个培训的价格。

我在培训详细信息页面上有一个“注册培训”按钮,我将其更改为“购买”以获取想要出售的培训和链接

?add-to-cart=340&custom_price=600&quantity=1 

我在表格里给了。

这里340是我创建的虚拟产品的id。

单击“购买”按钮后,名为 Education 的虚拟产品将添加到购物篮中。但我想根据打印的培训详细信息页面更新此培训的名称和价格。

我添加到functions.php的代码。

add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals' );
function before_calculate_totals( $_cart ){
// loop through the cart_contents
foreach ( $_cart->cart_contents as $cart_item_key => &$item ) {
    // you will need to determine the product id you want to modify, only when the "donation_amount" is passed
    if ( $item['product_id'] == 340 && isset( $_GET['custom_price'] ) ){
        // custom price from POST
        $custom_price = $_GET['custom_price'] > 0 ? $_GET['custom_price'] : 0;
        // save to the cart data
        //$item['data']->price = $custom_price;
        // new versions of WooCommerce may require (instead of line above)...
        $item['data']->set_price($custom_price);
    }
}
}

function ipe_product_custom_price( $cart_item_data, $product_id ) {
if( isset( $_POST['custom_price'] ) && !empty($_POST['custom_price'])) {        
    $cart_item_data[ "custom_price" ] = $_POST['custom_price'];     
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'ipe_product_custom_price', 99, 2 );

我想用这些代码更新价格,但没有用。

如何动态更新虚拟产品的信息?或者你会建议什么不同的方法?

标签: phpwordpresswoocommercecartprice

解决方案


您的代码中存在一些错误,并且缺少一些使其正常工作的东西。尝试以下操作:

// Set custom data as custom cart data in the cart item
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_price_as_custom_cart_item_data', 30, 3 );
function add_custom_price_as_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
    if( ! isset($_GET['custom_price']) ) {
        $cart_item_data['custom_price'] = (float) esc_attr( $_GET['custom_price'] );
        $cart_item_data['unique_key'] = md5( microtime().rand() ); // Make each item unique  
    }
    return $cart_item_data;
}

// Change cart item price
add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price' );
function change_cart_item_price( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // loop through the cart_contents
    foreach ( $cart->get_cart() as $cart_item ) {
        // you will need to determine the product id you want to modify, only when the "donation_amount" is passed
        if ( isset($cart_item['custom_price']) ) {.
            $item['data']->set_price($cart_item['custom_price']);
        }
    }
}

// Display the custom price on minicart too
add_filter( 'woocommerce_cart_item_price', 'change_minicart_item_price', 10, 3 );
function change_minicart_item_price( $price_html, $cart_item, $cart_item_key ) {
    if( ! is_cart() && isset( $cart_item['custom_price'] ) ) {
        $args = array( 'price' => floatval($cart_item['custom_price']) );

        if ( WC()->cart->display_prices_including_tax() ) {
            $product_price = wc_get_price_including_tax( $cart_item['data'], $args );
        } else {
            $product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
        }
        return wc_price( $product_price );
    }
    return $price_html;
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。它应该有效。


推荐阅读