Javascript Woocommerce迷你购物车小部件产品价格覆盖
是否可以在Woocommerce迷你购物车小部件中更改产品价格?我已经使用来自的提示覆盖了购物车中的价格,但它仅适用于购物车页面。小部件中的价格不会更改。您可以使用“woocommerce\u cart\u item\u price”过滤器,而不是使用“woocommerce\u before\u Calculates\u totals”Javascript Woocommerce迷你购物车小部件产品价格覆盖,javascript,php,jquery,wordpress,woocommerce,Javascript,Php,Jquery,Wordpress,Woocommerce,是否可以在Woocommerce迷你购物车小部件中更改产品价格?我已经使用来自的提示覆盖了购物车中的价格,但它仅适用于购物车页面。小部件中的价格不会更改。您可以使用“woocommerce\u cart\u item\u price”过滤器,而不是使用“woocommerce\u before\u Calculates\u totals” add_filter('woocommerce_cart_item_price','modify_cart_product_price',10,3); fu
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;
}
您可以使用“woocommerce\u cart\u item\u price”过滤器,例如
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;
}
以下是我如何将免费产品的价格设置为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;
}
我在类内钩住了,所以钩子看起来像这样:
add_filter( 'set_free_items_price', array(__CLASS__, 'woocommerce_cart_item_price_filter'), 10, 3 );
add_filter( 'set_free_items_price', 'woocommerce_cart_item_price_filter', 10, 3 );
但如果您将其用作过程函数,那么钩子应该如下所示:
add_filter( 'set_free_items_price', array(__CLASS__, 'woocommerce_cart_item_price_filter'), 10, 3 );
add_filter( 'set_free_items_price', 'woocommerce_cart_item_price_filter', 10, 3 );
请记住,这也会影响常规购物车中的价格行。
而且,如果您没有看到迷你购物车中的更改,请尝试在常规购物车页面上更新购物车,然后返回并再次检查迷你购物车值是否已更改。这就是我如何将免费产品的价格设置为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;
}
我在类内钩住了,所以钩子看起来像这样:
add_filter( 'set_free_items_price', array(__CLASS__, 'woocommerce_cart_item_price_filter'), 10, 3 );
add_filter( 'set_free_items_price', 'woocommerce_cart_item_price_filter', 10, 3 );
但如果您将其用作过程函数,那么钩子应该如下所示:
add_filter( 'set_free_items_price', array(__CLASS__, 'woocommerce_cart_item_price_filter'), 10, 3 );
add_filter( 'set_free_items_price', 'woocommerce_cart_item_price_filter', 10, 3 );
请记住,这也会影响常规购物车中的价格行。
而且,如果您没有看到迷你购物车中的变化,请尝试在常规购物车页面上更新购物车,然后返回并再次检查迷你购物车值是否已发生变化。要更改WooCommerce迷你购物车小部件上的价格,您必须使用此过滤器挂钩:WooCommerce\u小部件\u购物车\u商品\u数量 您可以检查文件的第63行:以查看过滤器挂钩是如何创建的
apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key );
apply_过滤器('woocommerce_widget_cart_item_quantity','.sprintf('%s×;%s',$cart_item['quantity',$product_price)。''.$cart_item,$cart_item_key);
正如过滤器挂钩的名称所示,它不仅可以用于价格,还可以用于显示数量
例如,您可以使用它根据条件对某些产品应用折扣。在本例中,我使用了存储在产品元数据中的值
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 × <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;
}
}
add_filter('woocommerce_widget_cart_item_quantity','custom_wc_widget_cart_item_quantity',10,3);
函数自定义\u wc\u小部件\u购物车\u项目\u数量($output、$cart\u item、$cart\u item\u key){
$product_id=$cart_项目['product_id'];
//用你自己的方式来决定哪种产品的价格应该改变
//在本例中,我使用了一个自定义元字段来应用折扣
$special\u discount=get\u post\u meta($product\u id,'special\u discount',true);
if(特别折扣){
$price=$cart_item['data']->get_price();
$final_price=$price-($price*$special_折扣);
//最后一个字符串,包含应用折扣的数量和价格
返回sprintf(“%s×;%s%s',$cart\u item['quantity'],$final\u price,get\u woocmerce\u currency\u symbol());
}否则{
//对于没有折扣的产品,不执行任何操作,初始字符串保持不变
返回$output;
}
}
请注意,这只会更改价格的显示方式,如果您必须在内部更改价格,请同时使用此操作挂钩:woocommerce\u在计算总金额之前要更改woocommerce迷你购物车小部件上的价格,您必须使用此过滤器挂钩:woocommerce\u小部件\u购物车\u商品数量 您可以检查文件的第63行:以查看过滤器挂钩是如何创建的
apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key );
apply_过滤器('woocommerce_widget_cart_item_quantity','.sprintf('%s×;%s',$cart_item['quantity',$product_price)。''.$cart_item,$cart_item_key);
正如过滤器挂钩的名称所示,它不仅可以用于价格,还可以用于显示数量
例如,您可以使用它根据条件对某些产品应用折扣。在本例中,我使用了存储在产品元数据中的值
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 × <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;
}
}
add_filter('woocommerce_widget_cart_item_quantity','custom_wc_widget_cart_item_quantity',10,3);
函数自定义\u wc\u小部件\u购物车\u项目\u数量($output、$cart\u item、$cart\u item\u key){
$product_id=$cart_项目['product_id'];
//用你自己的方式来决定哪种产品的价格应该改变
//在本例中,我使用了一个自定义元字段来应用折扣
$special\u discount=get\u post\u meta($product\u id,'special\u discount',true);
if(特别折扣){
$price=$cart_item['data']->get_price();
$final_price=$price-($price*$special_折扣);
//最后一个字符串,包含应用折扣的数量和价格
返回sprintf(“%s×;%s%s',$cart\u item['quantity'],$final\u price,get\u woocmerce\u currency\u symbol());
}否则{
//对于没有折扣的产品,不执行任何操作,初始字符串保持不变
返回$output;
}
}
请注意,这只会更改价格的显示方式,如果您必须在内部更改价格,请同时使用此操作挂钩:woocommerce\u before\u calculate\u totals