【问题标题】:Change price from a product variantion if corresponding product variantion is in WooCommerce cart如果相应的产品变体在 WooCommerce 购物车中,则从产品变体更改价格
【发布时间】:2021-07-25 01:52:58
【问题描述】:

我有一系列产品,有一个变体“A”和一个变体“B”。

我需要确保当购物车中出现产品的变体“A”时,同一产品的变体“B”会打折 15%。

要打折的变体始终是“B”,因为“A”已经打折了。

我尝试使用此代码,但它仅适用于一种产品并且我有几种产品,加上此代码有一个问题,如果我先输入产品“B”,然后输入产品“A”,则产品“B”的价格' 不会重新计算。

function woo_in_cart( $disc_product_id ) {
    global $woocommerce;


    if ( ! isset( $disc_product_id ) ) {
    return false;
     }


foreach ( $woocommerce->cart->get_cart() as $cart_myitem ) {
    if ( $cart_myitem['variation_id'] === $disc_product_id ) {
        return true;
    } else {
        return false;
         }
    }
    }
add_action( 'woocommerce_before_calculate_totals', 'webroom_change_price_of_product' );

function webroom_change_price_of_product( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
         return;

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

    $target_product_id = 3161; // PRODUCT ID TO DISCOUNT

    if(woo_in_cart(3162)!=0) {
         foreach ( $cart->get_cart() as $cart_item ) {
             if ( ($disc_product_id-1) == $cart_item['variation_id'] ){
                 // Set your price
                 $price =round($cart_item['data']->price*((100-15) / 100), 2);
        
                 $cart_item['data']->set_price( $price ); 
        
             }
    
        }
    }
}

任何帮助将不胜感激!

【问题讨论】:

    标签: php wordpress woocommerce product cart


    【解决方案1】:

    第一段代码将确保为每个变体显示一个额外的复选框。

    • 其目的是用于仅包含 2 个变体(A 和 B 变体)的可变产品
    • 复选框已选中 2 个变体中的 1 个,这将代表“A 变体”
    // Add checkbox
    function action_woocommerce_variation_options( $loop, $variation_data, $variation ) {
        // Checkbox checked (ticked)
        $checked = get_post_meta( $variation->ID, '_mycheckbox', true ) == 'yes' ? 'checked' : '';
    
        // Output
        ?>
        <label class="tips" data-tip="<?php esc_attr_e( 'This is my data tip', 'woocommerce' ); ?>">
            <?php esc_html_e( 'This my checkbox:', 'woocommerce' ); ?>
            <input type="checkbox" class="checkbox variable_checkbox" name="variable_mycheckbox[<?php echo esc_attr( $loop ); ?>]"<?php echo $checked; ?>/>
        </label>
        <?php
    }
    add_action( 'woocommerce_variation_options', 'action_woocommerce_variation_options', 10, 3 );
    
    // Save checkbox
    function action_woocommerce_admin_process_variation_object( $variation, $i ) {
         // Isset, yes or no
        $value = isset( $_POST['variable_mycheckbox'][$i] ) ? 'yes' : 'no';
        
        // Update
        $variation->update_meta_data( '_mycheckbox', $value );
    }
    add_action( 'woocommerce_admin_process_variation_object', 'action_woocommerce_admin_process_variation_object', 10, 2 );
    



    第二段代码将检查 2 个变体中的 1 个(A 变体)的复选框是否打开

    • 如果“A 变体”是这种情况,并且“B 变体”在购物车中。 然后,“B 变体”将获得 15% 的折扣
    // Used to calculate totals
    function action_woocommerce_before_calculate_totals( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // Discount in percent
        $discount = 15;
    
        // Iterating though each cart items
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            // Only for variations
            if ( $cart_item['data']->get_type() == 'variation' ) {
                // Get the value of the checkbox
                $checked = $cart_item['data']->get_meta( '_mycheckbox' );
                
                // Variant is checked
                if ( $checked == 'yes' ) {              
                    // Get all variations ID of a variable product
                    // Main function for returning products
                    $variation = wc_get_product( $cart_item['product_id'] );
                    
                    // Get product child ids (Array)
                    $child_ids = $variation->get_children();
                    
                    // Normally there are 2 childsIDs, we will remove the current one so that the other variantionID is known
                    $other_variantion_id = array_diff( $child_ids, array( $cart_item['variation_id'] ) );
                    
                    // Convert to integer
                    $other_variantion_id = (int) reset( $other_variantion_id );
                    
                    // Call function - other product ID in cart? yes, then get that cart item
                    $other_cart_item = product_id_in_cart_get_cart_item( $other_variantion_id );
                    
                    // Finds whether a variable is an array
                    if ( is_array( $other_cart_item ) ) {                   
                        // Calculate new price
                        $new_price = round( $other_cart_item['data']->get_price() * ( ( 100 - $discount ) / 100 ), 2 );
                        
                        // Set price
                        $other_cart_item['data']->set_price( $new_price );
                    }
                }
            }
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
    
    // Product ID in cart? return cart_item
    function product_id_in_cart_get_cart_item( $product_id ) {  
        // Iterating though each cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            // Compare
            if ( $product_id == $cart_item['data']->get_id() ) {
                // In cart
                return $cart_item;
            }
        }
        
        return false;
    }
    

    【讨论】:

    • 太好了,谢谢。太好了谢谢。我测试一下,我会告诉你的。
    • 非常感谢您的代码,它似乎可以工作,直到这个周末我才有时间测试一切。很抱歉您认为我没有回答,您的代码确实非常好并且非常有帮助。你可能认为我不回答你不是无礼,有时你必须等待。非常感谢您的努力。
    猜你喜欢
    • 2017-10-12
    • 2017-04-06
    • 2017-08-07
    • 2014-05-13
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多