如果相应的产品变体在 WooCommerce 购物车中,则更改产品变体的价格

时间:2021-05-02 11:04:20

标签: php wordpress woocommerce product cart

我有一系列产品,有一个变体“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 ); 
        
             }
    
        }
    }
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分: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 );

checkbox variation options



第二段代码将检查复选框是否为 2 个变体中的一个(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;
}