Wordpress 在WooCommerce上强制设置优惠券字段

Wordpress 在WooCommerce上强制设置优惠券字段,wordpress,woocommerce,coupon,Wordpress,Woocommerce,Coupon,我想知道是否有可能在WooCommerce上强制使用优惠券字段 我知道这是可能的使用功能,但这是略高于我目前的技能水平,所以我想知道你是否可以给我一个逐步版本的如何实现这一点。任何答案都将不胜感激。我不知道该功能,但您可以通过以下方式修改插件以实现此目的: 在主题文件夹woocommerce中创建一个文件夹,并在新创建的woocommerce文件夹中创建另一个名为checkout的文件夹 所以现在它看起来像wp content>themes>your theme>woocommerce>chec

我想知道是否有可能在WooCommerce上强制使用优惠券字段


我知道这是可能的使用功能,但这是略高于我目前的技能水平,所以我想知道你是否可以给我一个逐步版本的如何实现这一点。任何答案都将不胜感激。

我不知道该功能,但您可以通过以下方式修改插件以实现此目的:

在主题文件夹woocommerce中创建一个文件夹,并在新创建的woocommerce文件夹中创建另一个名为checkout的文件夹

所以现在它看起来像wp content>themes>your theme>woocommerce>checkout

现在转到插件目录并按照以下路径操作:

wp-content>plugins>woocommerce>templates>checkout

当您进入上面的路径时,您将发现一个名为
form-toucon.php
的文件。复制该文件并将其粘贴到我们在该答案顶部创建的目录中

wp-content>themes>your-theme>woocommerce>checkout>form-coupon.php

现在是时候修改wp content>themes>your theme>woocommerce>checkout>form-coupon.php中的代码了:

在上述文件中找到以下代码行:

<input type="text" name="coupon_code" class="input-text" placeholder="<?php _e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" />
请尝试一下,并让我知道反馈


注意
未经测试

要解决此问题,请尝试以下代码:

    <?php
 add_action('woocommerce_check_cart_items', 'make_coupon_code');

    function make_coupon_code()
    {
        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> ' . __( 'insert coupon code', 'woocommerce' ), 'error' );
            }
        }
    }
?>

在functions.php中,而不是在上面的函数中


对我来说,它可以工作

在functions.php中添加以下代码

单个产品需要优惠券

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
  $targeted_ids = array(37); // The targeted product ids (in this array)
  $coupon_code = 'summer2'; // The required coupon code
  $coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );

  // Loop through cart items
  foreach(WC()->cart->get_cart() as $cart_item ) {
    // Check cart item for defined product Ids and applied coupon
    if( in_array( $cart_item['product_id'], $targeted_ids ) && ! $coupon_applied ) {
      wc_clear_notices(); // Clear all other notices
      // Avoid checkout displaying an error notice
      wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', 
      $cart_item['data']->get_name() ), 'error' );
      break; // stop the loop
    }
  }
}
将37替换为
$targeted\u ids=array(37)中的您的产品ID
您可以有多个产品标识,如
$targeted\u id=array(37,48,12)

$coupon_code='summer2'中的任何其他优惠券代码替换“summer2”
在使用之前,不要忘记在WooCommerce中添加此优惠券代码


需要所有产品的优惠券

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
  $product_categories = array( 'clothing' ); // Category ID or slug of targeted category
  $coupon_code = 'summer2'; // The required coupon code
  $coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
  // Loop through cart items
  foreach ( WC()->cart->get_cart() as $cart_item ){
    if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && !$coupon_applied ) {
      wc_clear_notices(); // Clear all other notices
      // Avoid checkout displaying an error notice
      wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', 
      $cart_item['data']->get_name() ), 'error' );
      break; // stop the loop
    }
  }
}

替换
$product_categories=array('服装')通过任何其他类别名称或类别ID。

您好,非常感谢您的建议。我已经尝试了上述方法,但是它仍然允许我在不使用优惠券的情况下结帐,我需要的是,客户在不使用优惠券代码的情况下无法完成订单非常感谢您的帮助!如果你想到了什么,请一定要告诉我,谢谢你的回答,只是为了确保我将上面的php代码添加到我孩子主题的functions.php或form-toucon.php?中,在你孩子主题的
functions.php
文件中,请让我知道输出,因为它没有经过测试。我已经尝试了上面的方法,但只部分有效。基本上会让你被卡在购物车页面上,而不让你进入结账页面。虽然我相信这会对这里的大多数读者有所帮助,我的特定商店不使用购物车功能,而是依靠超链接将您带到结帐页面。是否可能对结帐页面本身产生相同的影响,即,如果说缺少邮政编码,则除非以相同的方式输入代码,否则用户无法提交订单。再次感谢你的帮助。
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
  $targeted_ids = array(37); // The targeted product ids (in this array)
  $coupon_code = 'summer2'; // The required coupon code
  $coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );

  // Loop through cart items
  foreach(WC()->cart->get_cart() as $cart_item ) {
    // Check cart item for defined product Ids and applied coupon
    if( in_array( $cart_item['product_id'], $targeted_ids ) && ! $coupon_applied ) {
      wc_clear_notices(); // Clear all other notices
      // Avoid checkout displaying an error notice
      wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', 
      $cart_item['data']->get_name() ), 'error' );
      break; // stop the loop
    }
  }
}
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
  $product_categories = array( 'clothing' ); // Category ID or slug of targeted category
  $coupon_code = 'summer2'; // The required coupon code
  $coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
  // Loop through cart items
  foreach ( WC()->cart->get_cart() as $cart_item ){
    if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) && !$coupon_applied ) {
      wc_clear_notices(); // Clear all other notices
      // Avoid checkout displaying an error notice
      wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', 
      $cart_item['data']->get_name() ), 'error' );
      break; // stop the loop
    }
  }
}