WooCommerce 中特定购物车小计的免费赠品产品

我有一种产品很便宜。当且仅当购物车小计为 200 件或更多时,我想赠送该产品。


如果购物车小计为 200 件或更多,请将产品添加到购物车并将价格设置为 0。


如果购物车小计少于 200,请从购物车中删除该产品。


在添加和删除时,我使用通知来通知客户。除了产品价格发生变化外,一切都按计划进行。


如果有人可以检查我正在使用的代码并修复这一问题,我将非常感激。


add_action( 'woocommerce_before_calculate_totals', 'free_product_if_cart_minimum', 10, 1 );

function free_product_if_cart_minimum( $cart ) {


    // go away if admin

    if (is_admin() && !defined( 'DOING_AJAX' )) return;


    // say no to hook repetition 

    if (did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;


    $minimum_amount = 200;

    $free_product_id = 4576;

    $cart_items_total = 0;


    // cart loop

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){

    

        // is the free product there?

        if ( $cart_item['data']->get_id() == $free_product_id ) {

        

            $free_item_key = $cart_item_key;


        // set the price to zero

        // I've tried them both without success

        

        // $free_item_key->set_price( 0 );

        // $free_product_id->set_price( $price * 0.00 );

    }

    

    // cart subtotal incl. tax and discounts

    $cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];

    

    }


    // add the free product if not already there

    if ( $cart_items_total >= $minimum_amount && !isset( $free_item_key ) ) {


        $cart->add_to_cart( $free_product_id );


        wc_add_notice( 'Thank you! Here\'s your free product.', 'notice' );

    }


    // if below the minimum, remove the free product

    elseif ( $cart_items_total < $minimum_amount && isset( $free_item_key ) ) {


        $cart->remove_cart_item( $free_item_key );


        // display notice after removal

        wc_add_notice( 'Your cart subtotal is less than 200 and therefore, the free product was removed.', 'notice' );

    }

}


qq_遁去的一_1
浏览 94回答 1
1回答

人到中年有点甜

以下内容将完成这项工作,将购物车中的免费产品价格设置为零:add_action( 'woocommerce_before_calculate_totals', 'conditionally_add_free_product' );function conditionally_add_free_product( $cart ) {&nbsp; &nbsp; if ( is_admin() && ! defined( 'DOING_AJAX' ) )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; // Settings&nbsp; &nbsp; $minimum_amount&nbsp; &nbsp;= 200;&nbsp; &nbsp; $free_product_id&nbsp; = 4576;&nbsp; &nbsp; // Initializing&nbsp; &nbsp; $cart_subtotal = 0;&nbsp; &nbsp; $cart_items&nbsp; &nbsp; = $cart->get_cart();&nbsp; &nbsp; // Loop through cart items (first loop)&nbsp; &nbsp; foreach ( $cart_items as $cart_item_key => $cart_item ){&nbsp; &nbsp; &nbsp; &nbsp; // When free productis is cart&nbsp; &nbsp; &nbsp; &nbsp; if ( $cart_item['data']->get_id() == $free_product_id ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $free_item_key = $cart_item_key; // Free product found (get its cart item key)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cart_item['data']->set_price(0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Get cart subtotal incl. tax and discounts (excluding free product)&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cart_subtotal += $cart_item['line_total'] + $cart_item['line_tax'];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // When cart total is up to the minimum amount, add the free product if not already there&nbsp; &nbsp; if ( $cart_subtotal >= $minimum_amount && ! isset($free_item_key) ) {&nbsp; &nbsp; &nbsp; &nbsp; $cart_item_key = $cart->add_to_cart( $free_product_id );&nbsp; &nbsp; &nbsp; &nbsp; // display notice after removal&nbsp; &nbsp; &nbsp; &nbsp; wc_add_notice( __("Thank you! Here's your free product."), 'notice' );&nbsp; &nbsp; }&nbsp; &nbsp; // if below the minimum, remove the free product&nbsp; &nbsp; elseif ( $cart_subtotal < $minimum_amount && isset( $free_item_key ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $cart->remove_cart_item( $free_item_key );&nbsp; &nbsp; &nbsp; &nbsp; // display notice after removal&nbsp; &nbsp; &nbsp; &nbsp; wc_add_notice( sprintf(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;__("Your cart subtotal is less than %s and therefore, the free product was removed."), wc_price($cart_subtotal)&nbsp; &nbsp; &nbsp; &nbsp; ), 'notice' );&nbsp; &nbsp; }}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
打开App,查看更多内容
随时随地看视频慕课网APP