简体   繁体   English

Woocommerce Mini Cart Widget 产品价格覆盖

[英]Woocommerce Mini Cart Widget product price override

is it possible to change product price in Woocommerce Mini Cart Widget?是否可以在 Woocommerce Mini Cart Widget 中更改产品价格? I've overrided price in cart using tips from WooCommerce: Add product to cart with price override?我使用WooCommerce 中的提示覆盖了购物车中的价格:使用价格覆盖将产品添加到购物车? but it only works for cart page.但它只适用于购物车页面。 Prices in widget are not changed.小部件中的价格不变。

Instead of using "woocommerce_before_calculate_totals" you can use "woocommerce_cart_item_price" filter, some thing like您可以使用“woocommerce_cart_item_price”过滤器,而不是使用“woocommerce_before_calculate_totals”,例如

add_filter('woocommerce_cart_item_price','modify_cart_product_price',10,3);

function modify_cart_product_price( $price, $cart_item, $cart_item_key){
    $price = wc_price($custom_price, 4); 
    return $price;
}

This is how I set price to 0 for free products:这就是我将免费产品的价格设置为 0 的方式:

function set_free_items_price( $price, $cart_item, $cart_item_key ) {

     $returned_item_price = $cart_item['data']->get_price();

     if (isset($cart_item['free_item']['value'])
         && $cart_item['free_item']['value'] == true) {
         $returned_item_price = 0;
     }

     return get_woocommerce_currency_symbol() . $returned_item_price;
}

I hooked inside class, so it hook looks like this:我在课堂上挂钩,所以它看起来像这样:

add_filter( 'set_free_items_price', array(__CLASS__, 'woocommerce_cart_item_price_filter'), 10, 3 );

But if you are using it as procedural function, your hook should look like this:但是如果你将它用作过程函数,你的钩子应该是这样的:

add_filter( 'set_free_items_price', 'woocommerce_cart_item_price_filter', 10, 3 );

Keep in mind this is also affecting price row in regular cart.请记住,这也会影响常规购物车中的价格行。 And, if you are not seeing changes in mini cart, try to update cart on regular cart page, and the go back and check again if mini-cart values has been changed.而且,如果您没有看到迷你购物车的变化,请尝试在常规购物车页面上更新购物车,然后返回并再次检查迷你购物车的值是否已更改。

To change the price on the WooCommerce Mini Cart Widget you have to use this filter hook: woocommerce_widget_cart_item_quantity要更改 WooCommerce Mini Cart Widget 上的价格,您必须使用此过滤器挂钩: woocommerce_widget_cart_item_quantity

You can check the line 63 of the file: woocommerce/templates/cart/mini-cart.php to see how the filter hook is created.您可以查看文件的第 63 行: woocommerce/templates/cart/mini-cart.php以查看过滤器钩子是如何创建的。

apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s &times; %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key );

As the name of the filter hook indicates it can be used not only for the price but also to show the quantity.正如过滤钩的名称所示,它不仅可以用于显示价格,还可以用于显示数量。

For example you can use it to apply a discount to certain products based on a condition.例如,您可以使用它根据条件对某些产品应用折扣。 In this case I used a value stored in the meta data of the product.在这种情况下,我使用了存储在产品元数据中的值。

add_filter('woocommerce_widget_cart_item_quantity', 'custom_wc_widget_cart_item_quantity', 10, 3 );

function custom_wc_widget_cart_item_quantity( $output, $cart_item, $cart_item_key ) {
  $product_id = $cart_item['product_id'];
  // Use your own way to decide which product's price should be changed
  // In this case I used a custom meta field to apply a discount
  $special_discount = get_post_meta( $product_id, 'special_discount', true );
  if ($special_discount) {
    $price = $cart_item['data']->get_price();
    $final_price = $price - ( $price * $special_discount );
    // The final string with the quantity and price with the discount applied
    return sprintf( '<span class="quantity">%s &times; <span class="woocommerce-Price-amount amount">%s <span class="woocommerce-Price-currencySymbol">%s</span></span></span>', $cart_item['quantity'], $final_price, get_woocommerce_currency_symbol() );
  } else {
    // For the products without discount nothing is done and the initial string remains unchanged
    return $output;
  }
}

Note that this will only change how price is displayed, if you have to change it internally use also this action hook: woocommerce_before_calculate_totals请注意,这只会更改价格的显示方式,如果您必须在内部更改它,请使用此操作挂钩: woocommerce_before_calculate_totals

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM