如何更改购物车中的可变产品价格还删除Woocommerce中删除特定产品表格的购物车

时间:2018-07-30 12:46:43

标签: php wordpress woocommerce

我在我的网站上将一个可变商品添加到购物车中。当时,另一个可变商品也添加到了该购物车中,该商品为礼品商品。现在,我想将礼品可变商品价格更改为0,仅在条件符合购物车中提供礼物的产品。我也想通过单击提供礼物的产品来删除这两种产品形式。下面的代码对我不起作用。

import {Router} from "@angular/router";
import {Injectable} from "@angular/core";
import {Observable} from "rxjs/Observable";
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from "@angular/common/http";


@Injectable()
export class HttpService implements HttpInterceptor {

  constructor(private router: Router) {
  }

  private catchErrors() {
    return (res: HttpResponse<any>) => {
      if (this.isError(res)) {
        console.log(`Internal server error occured (${res.status} - ${res.statusText})`);
        this.router.navigateByUrl('/error');
      } else if (this.isUnauthorized(res)) {
        console.log(`User is not authenticated  - not logged in or the session expired? (${res.status} - ${res.statusText})`);
        this.router.navigateByUrl('/logout');
      } else if (this.isForbidden(res)) {
        console.log(`User does not have necessary permissions for the resource (${res.status} - ${res.statusText}): ${res.url}`);
        this.router.navigateByUrl('/forbidden');
      }

      return Observable.throw(res);
    };
  }

  private isError(res: HttpResponse<any>): boolean {
    return res && res.status === 500;
  }

  private isUnauthorized(res: HttpResponse<any>): boolean {
    return res && res.status === 401;
  }

  private isForbidden(res: HttpResponse<any>): boolean {
    return res && res.status === 403;
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).catch(this.catchErrors());
  }

}

1 个答案:

答案 0 :(得分:3)

可以根据此解决方案添加礼品-Buy one get one in woocommerce with out coupon code

您可以将以下内容添加到主题的“ functions.php”中,以删除其他产品自动添加的礼品。

function remove_gift_product($cart_item_key) {
    global $woocommerce;
    $cat_in_cart = false;
    $coupon_in_cart = false;

    $autocoupon = array( 123411 ); // variation ids of products that offers gifts
    $freecoupon =  array( 2046 ); // variation ids of products that are gift coupons

    foreach ( $woocommerce->cart->cart_contents as $key => $values ) {      
        if( in_array( $values['variation_id'], $autocoupon ) ) {  
            $cat_in_cart = true;                
        }       
    }

    if ( !$cat_in_cart ) {          
        foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
            if ( in_array( $cart_item['variation_id'], $freecoupon )) {             
                $woocommerce->cart->remove_cart_item($cart_item_key);
            }
        } 
    }
}
add_action( 'woocommerce_cart_item_removed', 'remove_gift_product' );

如果要降低礼品价格,请添加此内容。

function add_discount_price( $cart_object ) {
    global $woocommerce;
    $cat_in_cart = false;

    $autocoupon = array( 123411 ); // variation ids of products that offers gifts
    $freecoupon =  array( 2046 ); // variation ids of products that are gift coupons

    foreach ( $woocommerce->cart->cart_contents as $key => $values ) {      
        if( in_array( $values['variation_id'], $autocoupon ) ) {  
            $cat_in_cart = true;                
        }       
    }
    if ( $cat_in_cart ) {
        $custom_price = 0; // This will be your custome price     
        foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
            if ( in_array( $cart_item['variation_id'], $freecoupon )) {
                 $cart_item['data']->set_price($custom_price);
            }        
        }
    }
}

add_action( 'woocommerce_before_calculate_totals', 'add_discount_price' );