在Woocommerce中向购物车添加自定义通知

时间:2017-09-27 10:03:11

标签: php wordpress woocommerce cart discount

每当特定产品(不是所有产品)都添加到购物车时,我想向购物车添加自定义通知。

我想首先检查数量,如果是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');
}
}

如果有人能帮助我,那就太好了。

2 个答案:

答案 0 :(得分:1)

如果您仍在使用旧答案中的代码,可以稍微更改一下以使其正常工作:

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
function add_custom_discount_2nd_at_50( $wc_cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $discount = 0;
    $items_prices = array();
    $qty_notice = 0;  //  <==  Added HERE

    // Set HERE your targeted variable product ID
    $targeted_product_id = 40;

    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $qty = intval( $cart_item['quantity'] );
            $qty_notice += intval( $cart_item['quantity'] ); //  <==  Added HERE
            for( $i = 0; $i < $qty; $i++ )
                $items_prices[] = floatval( $cart_item['data']->get_price());
        }
    }
    $count_items_prices = count($items_prices);
    if( $count_items_prices > 1 ) foreach( $items_prices as $key => $price )
        if( $key % 2 == 1 ) $discount -= number_format($price / 2, 2 );

    if( $discount != 0 ){
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');

        // The discount
        $wc_cart->add_fee( 'Discount 2nd at 50%', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    } 
    //  <==  Here we display your additional notice when there quantity is equal to one
    elseif( $qty_notice == 1){ 
        wc_clear_notices();
        wc_add_notice( __( "Add one more to get 50% off on 2nd product" ), 'notice');
    }
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

此代码在Woocommerce 3+上进行测试并正常运行。

  

注意: wc_add_notice() 函数绝对不需要回显

答案 1 :(得分:0)

你做了相当不错的代码,需要尝试下面的代码:

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');
            }
        } 
}