WooCommerce 折扣:买一送一 50% 的折扣并附上通知
WooCommerce discount: buy one get one 50% off with a notice
在我上一个问题之后 我想在购物车中添加自定义通知,每当特定产品(不是所有产品)被添加到购物车时。
我想先查看数量,如果是 1,那么我想显示一条通知以增加该产品的数量。我自己从互联网上找到了一些东西,但我认为我的解决方案不正确:
add_action( 'wp', 'sp_custom_notice' );
function sp_custom_notice() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
//to display notice only on cart page
if ( ! is_cart() ) {
return;
}
global $product;
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if($product_id == 15730){
//check for quantify if equal to 1 in cart
wc_clear_notices();
wc_add_notice( __("Add one more to get 50% off on 2nd product"), 'notice');
}
}
如果有人能帮助我,那就太好了。
你的代码写得很好,需要一些小的改动试试下面的代码:
add_action('woocommerce_before_cart', 'sp_custom_notice');
function sp_custom_notice() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
//to display notice only on cart page
if ( ! is_cart() ) {
return;
}
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = wc_get_product( $values['data']->get_id());
if($values['data']->get_id() == 190 && $values['quantity'] == 1 ){
wc_clear_notices();
echo wc_add_notice( __("Add one more to get 50% off on 2nd product"), 'notice');
}
}
}
已更新 2020 年 7 月
如果您仍在使用代码 来解决您之前的问题,您可以稍微更改一下以使其也能正常工作:
add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
function add_custom_discount_2nd_at_50( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// YOUR SETTINGS:
$targeted_product_id = 40; // Set HERE your targeted product ID
// Initializing variables
$discount = $qty_notice = 0;
$items_prices = array();
// Loop through cart items
foreach ( $cart->get_cart() as $key => $cart_item ) {
if( in_array( $targeted_product_id, [$cart_item['product_id'], $cart_item['variation_id']] ) ){
$quantity = (int) $cart_item['quantity'];
$qty_notice += $quantity;
for( $i = 0; $i < $quantity; $i++ ) {
$items_prices[] = floatval( $cart_item['data']->get_price());
}
}
}
$count_items = count($items_prices); // Count items
rsort($items_prices); // Sorting prices descending order
if( $count_items > 1 ) {
foreach( $items_prices as $key => $price ) {
if( $key % 2 == 1 )
$discount -= number_format( $price / 2, 2 );
}
}
// Applying the discount
if( $discount != 0 ){
$cart->add_fee('Buy one get one 50% off', $discount );
// Displaying a custom notice (optional)
wc_clear_notices(); // clear other notices on checkout page.
if( ! is_checkout() ){
wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');
}
}
// Display a custom notice on cart page when quantity is equal to 1.
elseif( $qty_notice == 1 ){
wc_clear_notices(); // clear other notices on checkout page.
if( ! is_checkout() ){
wc_add_notice( __( "Add one more to get 50% off on 2nd item" ), 'notice');
}
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件 已测试并有效。
Note: The wc_add_notice()
function absolutely don't need to be echoed
在我上一个问题之后
我想先查看数量,如果是 1,那么我想显示一条通知以增加该产品的数量。我自己从互联网上找到了一些东西,但我认为我的解决方案不正确:
add_action( 'wp', 'sp_custom_notice' );
function sp_custom_notice() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
//to display notice only on cart page
if ( ! is_cart() ) {
return;
}
global $product;
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if($product_id == 15730){
//check for quantify if equal to 1 in cart
wc_clear_notices();
wc_add_notice( __("Add one more to get 50% off on 2nd product"), 'notice');
}
}
如果有人能帮助我,那就太好了。
你的代码写得很好,需要一些小的改动试试下面的代码:
add_action('woocommerce_before_cart', 'sp_custom_notice');
function sp_custom_notice() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
//to display notice only on cart page
if ( ! is_cart() ) {
return;
}
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = wc_get_product( $values['data']->get_id());
if($values['data']->get_id() == 190 && $values['quantity'] == 1 ){
wc_clear_notices();
echo wc_add_notice( __("Add one more to get 50% off on 2nd product"), 'notice');
}
}
}
已更新 2020 年 7 月
如果您仍在使用代码
add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
function add_custom_discount_2nd_at_50( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// YOUR SETTINGS:
$targeted_product_id = 40; // Set HERE your targeted product ID
// Initializing variables
$discount = $qty_notice = 0;
$items_prices = array();
// Loop through cart items
foreach ( $cart->get_cart() as $key => $cart_item ) {
if( in_array( $targeted_product_id, [$cart_item['product_id'], $cart_item['variation_id']] ) ){
$quantity = (int) $cart_item['quantity'];
$qty_notice += $quantity;
for( $i = 0; $i < $quantity; $i++ ) {
$items_prices[] = floatval( $cart_item['data']->get_price());
}
}
}
$count_items = count($items_prices); // Count items
rsort($items_prices); // Sorting prices descending order
if( $count_items > 1 ) {
foreach( $items_prices as $key => $price ) {
if( $key % 2 == 1 )
$discount -= number_format( $price / 2, 2 );
}
}
// Applying the discount
if( $discount != 0 ){
$cart->add_fee('Buy one get one 50% off', $discount );
// Displaying a custom notice (optional)
wc_clear_notices(); // clear other notices on checkout page.
if( ! is_checkout() ){
wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');
}
}
// Display a custom notice on cart page when quantity is equal to 1.
elseif( $qty_notice == 1 ){
wc_clear_notices(); // clear other notices on checkout page.
if( ! is_checkout() ){
wc_add_notice( __( "Add one more to get 50% off on 2nd item" ), 'notice');
}
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件 已测试并有效。
Note: The
wc_add_notice()
function absolutely don't need to be echoed