Php 特定产品类别的强制优惠券,避免在Woocommerce中结帐
我正在寻找使用woocommerce智能优惠券创建“邀请代码”。为此,我希望特定产品在购物车/结账时需要优惠券代码。我已经看到了实现这一点的方法,但它们会影响所有产品,而不仅仅是我想要的产品 我试图将一个返回的变量传递给另一个函数,但是我不知道怎么做 以下是最新版本:Php 特定产品类别的强制优惠券,避免在Woocommerce中结帐,php,wordpress,woocommerce,categories,coupon,Php,Wordpress,Woocommerce,Categories,Coupon,我正在寻找使用woocommerce智能优惠券创建“邀请代码”。为此,我希望特定产品在购物车/结账时需要优惠券代码。我已经看到了实现这一点的方法,但它们会影响所有产品,而不仅仅是我想要的产品 我试图将一个返回的变量传递给另一个函数,但是我不知道怎么做 以下是最新版本: //Check to see if user has product with a specific product category in cart function check_prod
//Check to see if user has product with a specific product category in cart
function check_product_in_cart() {
global $woocommerce;
//assigns a default negative value
// categories targeted 87, 18, 19
$product_in_cart = false;
// start of the loop that fetches the cart items
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
// second level loop search, in case some items have several categories
foreach ($terms as $term) {
$_categoryid = $term->term_id;
if (( $_categoryid === 87 ) || ( $_categoryid === 18 ) || ( $_categoryid === 19 )) {
//category is in cart!
$product_in_cart = true;
}
}
}
return $product_in_cart;
}
// Force Coupon codes for Woocommerce
add_action('woocommerce_check_cart_items', 'force_coupon_code');
function force_coupon_code( $product_in_cart )
{
global $woocommerce;
if(is_cart() || is_checkout()){
$my_coupon = $woocommerce->cart->applied_coupons;
echo $woocommerce->cart->get_applied_coupons;
if(empty($my_coupon))
{
wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'A coupon code is mandatory for this product.', 'woocommerce' ), 'error' );
}
}
}
//检查用户是否在购物车中有特定产品类别的产品
功能检查\u购物车中的产品\u(){
全球商业;
//指定默认的负值
//目标类别87、18、19
$product\U in\U cart=错误;
//获取购物车项目的循环的开始
foreach($woocommerce->cart->get\u cart()作为$cart\u item\u key=>$value){
$_product=$values['data'];
$terms=获取_术语($_product->id,'product_cat');
//第二级循环搜索,以防某些项目具有多个类别
foreach($terms作为$term){
$\u categoryid=$term->term\u id;
如果($_categoryid==87)| |($_categoryid==18)| |($_categoryid==19)){
//类别在购物车中!
$product\U in\U cart=true;
}
}
}
在购物车中返回$product\u;
}
//电子商务的强制优惠券代码
添加操作(“woocommerce\u check\u cart\u items”、“force\u优惠券\u code”);
功能强制优惠券代码(购物车中的产品)
{
全球商业;
if(is_cart()| | is_checkout()){
$my_优惠券=$woocommerce->cart->applied_优惠券;
echo$woocommerce->cart->get\u applied\u优惠券;
如果(空($my_优惠券))
{
wc_添加通知(“”.$btn['label'].”。uuu(“‘此产品必须有优惠券代码’,‘woocommerce’,‘错误’);
}
}
}
第一部分应将产品$product_返回到_购物车中,第二部分强制提供优惠券。下面是我正在使用的代码的来源。
和。我读到,将变量放在新函数args中可能会实现这一点,但是它根本不起作用
于2018年4月1日更新
您正在使用的代码已经过时且复杂。这可以简单地在一个挂钩函数中完成,使用Wordpresshas_term()
条件函数来检测购物车项目是否属于特定的产品类别
因此,您的代码将更加紧凑和高效:
// Force Coupon codes for Woocommerce
add_action('woocommerce_check_cart_items', 'mandatory_coupon_code');
function mandatory_coupon_code()
{
// set Here your categories IDs, slugs or names
$categories = array(18,19,87);
$found = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item )
if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
// Product category found in cart items
$found = true;
// Exit from loop
break;
}
}
$coupons = WC()->cart->get_applied_coupons();
// The Notice is displayed for that product categories when no mandatory coupon has been entered
if( count( $coupons ) > 0 && $found )
wc_add_notice( __( 'A coupon code is mandatory for this product.', 'woocommerce' ), 'error' );
}
}
这段代码放在活动子主题(或活动主题)的function.php文件中。测试和工作
特定优惠券代码的版本,不含产品类别(适用于所有产品):
这段代码放在活动子主题(或活动主题)的function.php文件中。经过测试,效果良好。漂亮!他很有魅力。我还在学习,所以除了更好的代码外,
if(empty($my_优惠券)&&$has_cat)
这一行将这一切联系在一起。理论上,我是否可以在语句中添加更多参数并进一步缩小范围,例如if(empty($my_优惠券)&&&$has_cat)&&&$color_blue&&等等。
?
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
// HERE set your coupon code
$mandatory_coupon = 'spring10';
$applied_coupons = WC()->cart->get_applied_coupons();
// If coupon is found we exit
if( in_array( $mandatory_coupon, $applied_coupons ) ) return;
// Not found: display an error notice
wc_add_notice( __( 'Please enter coupon code to checkout.', 'woocommerce' ), 'error' );
}