【问题标题】:Making the coupon field mandatory on WooCommerce在 WooCommerce 上强制使用优惠券字段
【发布时间】:2015-02-16 01:34:21
【问题描述】:

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

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

【问题讨论】:

    标签: wordpress woocommerce coupon


    【解决方案1】:

    我不知道这个功能,但你可以通过以下方式修改插件来实现:

    在您的主题文件夹 woocommerce 中创建一个文件夹,并在新创建的 woocommerce 文件夹中创建另一个具有 checkout 名称的文件夹。

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

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

    wp-content > plugins > woocommerce > templates > checkout

    当您进入上述路径时,您会发现一个名为form-coupon.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="" />
    

    并将上面的行替换为

    <input type="text" name="coupon_code" class="input-text" placeholder="<?php _e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" required/>
    

    注意:这里我添加了htmlrequired属性。

    如果您有任何疑问,请告诉我。

    更新:

        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))
                {
                    $woocommerce->add_error("Please enter coupon code to checkout.");
                }
            }
        }
    

    请试一试并告诉我反馈。

    注意UNTESTED

    【讨论】:

    • 您好,非常感谢您的建议。我已经尝试了上面的方法,但是它仍然允许我在不使用优惠券的情况下结帐,我需要的是客户在不使用优惠券代码的情况下无法完成订单
    • 非常感谢您的帮助!如果您有什么想法,请告诉我
    • 嘿,感谢您的回答,只是为了确保我将上面的 php 代码添加到我的子主题的函数 .php 或 form-coupon.php 中?
    • 在您的子主题的functions.php 文件中,请让我知道输出,因为它未经测试。
    • 嘿,我试过上面的方法,它有效,但只是部分有效。基本上让你卡在购物车页面,不让你去结帐页面。虽然我相信这会对这里的大多数其他读者有所帮助,但我的特定商店不使用购物车功能,而是依靠超链接将您带到结帐页面,是否可以对结帐页面本身产生相同的效果,即如果说缺少邮政编码,除非以相同的方式输入了代码,否则用户无法提交订单。再次非常感谢您的帮助。
    【解决方案2】:

    要解决这个问题,试试这个代码:

        <?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中而不是上面那个...

    对我来说很有效

    【讨论】:

      【解决方案3】:

      在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_ids = array(37); 中的产品 ID,您可以拥有多个产品 ID,例如 $targeted_ids = 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
          }
        }
      }
      

      用任何其他类别名称或类别 ID 替换 $product_categories = array( 'clothing' );

      【讨论】:

        猜你喜欢
        • 2021-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多