第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在自定義函數(shù)中從 WooCommerce 動(dòng)態(tài)獲取優(yōu)惠券代碼

在自定義函數(shù)中從 WooCommerce 動(dòng)態(tài)獲取優(yōu)惠券代碼

PHP
眼眸繁星 2021-06-11 17:12:00
參考“僅當(dāng)在 Woocommerce 中應(yīng)用優(yōu)惠券時(shí)才允許購(gòu)買(mǎi)特定產(chǎn)品”對(duì)我之前的一個(gè)問(wèn)題的回答代碼,優(yōu)惠券代碼在函數(shù)中硬編碼,需要與 Woocommerce 中現(xiàn)有的優(yōu)惠券代碼匹配。但我想從 Woocommerce 動(dòng)態(tài)選擇優(yōu)惠券。如何從woocommerce動(dòng)態(tài)獲取優(yōu)惠券?
查看完整描述

3 回答

?
當(dāng)年話下

TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊

以下代碼擴(kuò)展了我之前的答案,并將在 WooCommerce > 優(yōu)惠券部分添加一個(gè)復(fù)選框,使任何優(yōu)惠券代碼對(duì)于特定定義的項(xiàng)目都是“強(qiáng)制性的”:

http://img1.sycdn.imooc.com//60cd54b70001bc0f12060534.jpg

因此,這樣您就不必在函數(shù)中定義任何優(yōu)惠券代碼。


整個(gè)代碼:


// Add a custom checkbox to Admin coupon settings pages

add_action( 'woocommerce_coupon_options', 'add_coupon_option_checkbox', 10 );

function add_coupon_option_checkbox() {

    woocommerce_wp_checkbox( array(

        'id'            => 'items_mandatory',

        'label'         => __( 'Force specific items', 'woocommerce' ),

        'description'   => __( 'Make this coupon mandatory for specific items.', 'woocommerce' ),

        'desc_tip'      => false,

    ) );

}


// Save the custom checkbox value from Admin coupon settings pages

add_action( 'woocommerce_coupon_options_save', 'save_coupon_option_checkbox', 10, 2 );

function save_coupon_option_checkbox( $post_id, $coupon ) {

    update_post_meta( $post_id, 'items_mandatory', isset( $_POST['items_mandatory'] ) ? 'yes' : 'no' );

}


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)

    $applied_coupons = WC()->cart->get_applied_coupons();

    $coupon_applied  = false;


    if( sizeof($applied_coupons) > 0 ) {

        // Loop through applied coupons

        foreach( $applied_coupons as $coupon_code ) {

            $coupon = new WC_Coupon( $coupon_code );

            if( $coupon->get_meta('items_mandatory') === 'yes' ) {

                $coupon_applied = true;

                break;

            }

        }

    }


    // 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

        }

    }

}

代碼位于活動(dòng)子主題(或活動(dòng)主題)的 functions.php 文件中。測(cè)試和工作。

http://img1.sycdn.imooc.com//60cd54c90001ac8d08630127.jpg

在結(jié)帳時(shí):

http://img1.sycdn.imooc.com//60cd54d60001408f08570160.jpg


查看完整回答
反對(duì) 回復(fù) 2021-06-19
?
天涯盡頭無(wú)女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

試試這個(gè),更改被評(píng)論:


add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );

function mandatory_coupon_for_specific_items() {

    // The targeted product ids (in this array)

    $targeted_ids   = array(37); 

    // Get applied $coupon_names

    $cart_applied_coupons = WC()->cart->get_applied_coupons()

    // Set the variable $coupon_applied to false

    $coupon_applied = false;

    // Create an array to save all coupon names

    $coupon_names = array();

    // Get the available coupons

    $args = array(

        'posts_per_page'   => -1,

        'orderby'          => 'title',

        'order'            => 'asc',

        'post_type'        => 'shop_coupon',

        'post_status'      => 'publish',

    );

    $all_coupons = get_posts( $args );

    if( !empty($all_coupons) ){

      // Loop through the available coupons

      foreach ( $all_coupons as $coupon ) {

          // Get the name for each coupon and add to the previously created array

          $coupon_name = $coupon->post_title;

          array_push( $coupon_names, $coupon_name );

      }

      // If one on the coupons is applied change the value of $coupon_applied to true

      foreach($coupon_names as $coupon_code){

        if( in_array( strtolower($coupon_code), $cart_applied_coupons) ){

          $coupon_applied = true;

          break;

        }

      }

    }



    // 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

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-06-19
?
千巷貓影

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

以為我會(huì)分享我的類(lèi)別版本。使用了此處發(fā)布的解決方案和此解決方案的組合。


// Add a custom checkbox to Admin coupon settings pages

add_action( 'woocommerce_coupon_options', 'add_coupon_option_checkbox', 10 );

function add_coupon_option_checkbox() {

    woocommerce_wp_checkbox( array(

        'id'            => 'items_mandatory',

        'label'         => __( 'Force specific items', 'woocommerce' ),

        'description'   => __( 'Make this coupon mandatory for specific items.', 'woocommerce' ),

        'desc_tip'      => false,

    ) );

}



// Save the custom checkbox value from Admin coupon settings pages

add_action( 'woocommerce_coupon_options_save', 'save_coupon_option_checkbox', 10, 2 );

function save_coupon_option_checkbox( $post_id, $coupon ) {

    update_post_meta( $post_id, 'items_mandatory', isset( $_POST['items_mandatory'] ) ? 'yes' : 'no' );

}



// Force Coupon codes for Woocommerce

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );

function mandatory_coupon_code() {

     // Set your product categories in the array (can be term IDs, slugs or names)

    $product_categories = array( '58');

    $applied_coupons = WC()->cart->get_applied_coupons();

    $found = false;

    

    if( sizeof($applied_coupons) > 0 ) {

        // Loop through applied coupons

        foreach( $applied_coupons as $coupon_code ) {

            $coupon = new WC_Coupon( $coupon_code );

            if( $coupon->get_meta('items_mandatory') === 'yes' ) {

                $coupon_applied = true;

                break;

            }

        }

    }

    // Loop through cart items to check for the product categories

    foreach ( WC()->cart->get_cart() as $cart_item ){

        if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) ){

            $found = true; // cart item from the product category is found

            break; // We can stop the loop

        }

    }


    // If not found we exit

    if( ! $found ) return; // exit

    

    // Coupon not applied and product category found

    

    if( ! $coupon_applied) {

        // Display an error notice preventing checkout

        $message = __( 'The product "%s" requires a coupon for checkout.' );

        wc_add_notice( sprintf($message, $cart_item['data']->get_name() ), 'error' );

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-06-19
  • 3 回答
  • 0 關(guān)注
  • 266 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)