Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更改购物车中的原始价格以进行产品变更_Php_Wordpress_Woocommerce_Cart_Product - Fatal编程技术网

Php 更改购物车中的原始价格以进行产品变更

Php 更改购物车中的原始价格以进行产品变更,php,wordpress,woocommerce,cart,product,Php,Wordpress,Woocommerce,Cart,Product,我试着用这个来改变购物车的原价,但没有骰子。我认为它不起作用,因为我使用的产品是可变产品。产品ID为141,变体ID为142 function sv_change_product_price_cart( $price, $cart_item, $cart_item_key ) { if ( 142 === $cart_item['product_id'] ) { $price = '$50.00 per Unit<br>(7-8 skewers per Uni

我试着用这个来改变购物车的原价,但没有骰子。我认为它不起作用,因为我使用的产品是可变产品。产品ID为141,变体ID为142

function sv_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
    if ( 142 === $cart_item['product_id'] ) {
        $price = '$50.00 per Unit<br>(7-8 skewers per Unit)';
    }
    return $price;
}
add_filter( 'woocommerce_cart_item_price', 'sv_change_product_price_cart', 10, 3 );
函数sv\u change\u product\u price\u cart($price,$cart\u item,$cart\u item\u key){
如果(142==$cart\u item['product\u id']){
$price='每单位$50.00
(每单位7-8根串肉机)'; } 返回$price; } 添加过滤器('woocommerce\u cart\u item\u price'、'sv\u change\u product\u price\u cart',10,3);
如何让它工作


谢谢

您需要将
$cart\u item['product\u id']
替换为
$cart\u item['variation\u id']
,以使其适用于您的条件下的产品变体

此功能仅会更改显示,但不会更改计算:

// Changing the displayed price (with custom label)
add_filter( 'woocommerce_cart_item_price', 'sv_display_product_price_cart', 10, 3 );
function sv_display_product_price_cart( $price, $cart_item, $cart_item_key ) {
    if ( 142 == $cart_item['variation_id'] ) {
        // Displaying price with label
        $price = '$50.00 per Unit<br>(7-8 skewers per Unit)';
    }
    return $price;
}
//更改显示的价格(带有自定义标签)
添加过滤器('woocommerce\u cart\u item\u price'、'sv\u display\u product\u price\u cart',10,3);
功能sv_显示产品价格购物车($price、$cart\u item、$cart\u item\u key){
如果(142=$cart\u item['variation\u id']){
//用标签显示价格
$price='每单位$50.00
(每单位7-8根串肉机)'; } 返回$price; }
以下是钩住的函数,它将根据您的价格更改购物车计算:

// Changing the price (for cart calculation)
add_action( 'woocommerce_before_calculate_totals', 'sv_change_product_price_cart', 10, 1 );
function sv_change_product_price_cart( $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 ( 142 == $cart_item['variation_id'] ){
            // Set your price
            $price = 50;

            // WooCommerce versions compatibility
            if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
                $cart_item['data']->price = $price; // Before WC 3.0
            } else {
                $cart_item['data']->set_price( $price ); // WC 3.0+
            }
        }
    }
}
//更改价格(用于购物车计算)
添加操作('woocommerce'u before'u culate'u totals','sv'u change'u product'u price'u cart',10,1);
功能sv_变更_产品_价格_购物车($cart){
if(定义了('DOING'uajax'))
返回;
如果(did_action('woocommerce_before_calculate_totals')>=2)
返回;
foreach($cart->get\u cart()作为$cart\u项目){
如果(142=$cart\u item['variation\u id']){
//定价
$price=50;
//WooCommerce版本兼容性

如果(版本)比较(WC_版本,'3.0',请告诉我如何根据在购物车中选择的变化更改购物车中的价格。我使用代码
获取选择框中的变化。两个选择框分别作为$first_属性和$second_属性。基于这两个选择,我需要更新购物车中的价格。请提供任何帮助。对不起!我已经解决了问题l限制。有什么建议吗???@BoopathiD删除一些你以前没有回答的问题。然后仔细地提出一个新的详细问题…然后在这里通知我。我一定会按照建议做。