Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.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 在Woocommerce中按购物车项目上的产品类别同步自定义产品价格_Php_Woocommerce_Cart_Hook Woocommerce_Price - Fatal编程技术网

Php 在Woocommerce中按购物车项目上的产品类别同步自定义产品价格

Php 在Woocommerce中按购物车项目上的产品类别同步自定义产品价格,php,woocommerce,cart,hook-woocommerce,price,Php,Woocommerce,Cart,Hook Woocommerce,Price,我必须根据类别来安排产品价格。所以A类产品的价格都是5, B类产品价格为10,c类产品价格为15 在这种安排之后,当产品加入购物车时,我必须将产品价格乘以我们的利润。目前我们的利润率是2.5% 因此,我们在functions.php add_filter('woocommerce_product_get_price', 'product_price_new', 10, 2); function product_price_new($price, $product) { if(!is_

我必须根据类别来安排产品价格。所以A类产品的价格都是5, B类产品价格为10,c类产品价格为15

在这种安排之后,当产品加入购物车时,我必须将产品价格乘以我们的利润。目前我们的利润率是2.5%

因此,我们在
functions.php

add_filter('woocommerce_product_get_price', 'product_price_new', 10, 2);
 function product_price_new($price, $product) {
     if(!is_cart()){
     if(has_term( 'accessories', 'product_cat' ,$product->id)){  $price=5; }
     if(has_term( 'hoodies', 'product_cat' ,$product->id)){ $price=10; }
     if(has_term( 'tshirts', 'product_cat' ,$product->id)){ $price=15;}

     }
     return $price;
 }

add_filter( 'woocommerce_add_cart_item_data', 'margin_price', 30, 3 );
function margin_price( $cart_item_data, $product_id, $variation_id ) {
    $the_id = $variation_id > 0 ? $variation_id : $product_id;
    $product = wc_get_product( $the_id );
    $product_price = (float) $product->get_price(); 
    $cart_item_data['calculated-price'] = $product_price*2.5;
    return $cart_item_data;
}


add_action( 'woocommerce_before_calculate_totals', 'add_caculted', 20, 1 );
function add_caculted( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ){
        if( ! empty( $cart_item['calculated-price'] ) ){
            // Set the calculated item price (if there is one)
            $cart_item['data']->set_price( $cart_item['calculated-price'] );

        }
    }
}
请看看会发生什么

例如,如果我们试图在连帽衫类别中添加产品,那么价格将为10*2.5=25

(1) 在购物车页面中,产品pric显示为25,这是正确的。 [mywebsite.com/cart]

(2) 在结帐页面中,产品价格显示为10,因此总数为 10岁。这是错误的,应该是25

(3) 在菜单附近显示的微型购物车中,它显示1*10,小计为10,但它需要显示1*25,小计=25

请帮助解决这个问题。我错过了什么


我在默认的前端主题中尝试了所有这些代码

仅对于简单产品,您可以尝试以下方法:

// Utility function
function get_special_product_category_prices( $price, $product_id ) {
    // HERE your prices by product category
    $prices_by_cat = array( 5 => 'accessories', 10 => 'hoodies', 15 => 't-shirts' );
    foreach( $prices_by_cat as $key_price => $term ){
        if( has_term( $term, 'product_cat', $product_id ) )
            return $key_price;
    }
    return $price;
}

// Alter product displayed prices (for simple products only)
add_filter( 'woocommerce_get_price_html', 'alter_displayed_price_html', 20, 2 );
function alter_displayed_price_html( $price, $product ) {

    if( $product->is_type('simple') ){
        $raw_price = get_special_product_category_prices( $product->get_price(), $product->get_id() );

        if( $raw_price > 0 )
            $price = wc_price( wc_get_price_to_display( $product, array( 'price' => $raw_price ) ) );
    }
    return $price;
}

add_filter( 'woocommerce_add_cart_item_data', 'add_calculated_margin_price', 30, 3 );
function add_calculated_margin_price( $cart_item_data, $product_id, $variation_id ) {
    $the_id = $variation_id > 0 ? $variation_id : $product_id;
    $product = wc_get_product( $the_id );

    $product_price = (float) get_special_product_category_prices( $product->get_price(), $product_id );

    $cart_item_data['calculated-price'] = $product_price * 2.5;
    return $cart_item_data;
}

add_action( 'woocommerce_before_calculate_totals', 'set_caculated_price', 20, 1 );
function set_caculated_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ){
        if( ! empty( $cart_item['calculated-price'] ) ){
            // Set the calculated item price (if there is one)
            $cart_item['data']->set_price( $cart_item['calculated-price'] );
        }
    }
}

代码进入活动子主题(或主题)的function.php文件。它应该会起作用。

您的商店中的产品类型是什么…它们只是简单的还是可变的?只有简单的productsHow会在您的设置选项中设置您的税:1)使用税输入的价格…2)在商店中显示价格…3)在购物车和签出期间显示价格HI,没有税选项。我刚刚安装了一个wordpress,之后我安装了storefront主题,所以也没有其他插件。当我尝试此代码时,“添加到购物车”按钮消失了。可能是您正在后端产品页面中为每个产品设置价格。我不会在后端产品页面添加任何价格。但真的谢谢你的朋友。我将检查此代码并检查可能性。但是mincart总是出问题。实际上,我尝试了你的代码,我知道我必须在baceknd中手动设置价格,所以我这样做了,并再次测试了代码。因此,它正在发挥作用。但mincart仍然存在问题。添加产品时,此mincart显示产品原始价格,不乘以2.5。但是如果我们进入购物车页面并更新产品数量,那么在刷新后,mincart会显示我想要的新价格。所以问题来了mincart@abilasher我在购物车和迷你购物车中也有同样的东西……如果你不在问题中告诉woocommerce安装的具体设置,你就不能在非正常的woocommerce安装中提问。因为你没有时间,我们(回答者)没有时间,而且问题/答案对社区没有用处