source

WooCommerce 카트 및 체크아웃에서 상품 가격 변경

lovecheck 2023. 10. 4. 22:02
반응형

WooCommerce 카트 및 체크아웃에서 상품 가격 변경

방문자가 선택한 옵션을 바탕으로 상품을 커스터마이징 할 수 있는 WooCommerce 애드온을 만들고 있으며, 커스터마이징 가격도 계산하여 세션 테이블에 저장하고 있습니다.
저는 카트 페이지에서도 그 가치를 얻을 수 있습니다.

내 문제:
상품의 기본가격을 변경하여 WooCommerce 프로세스에서 카트, 체크아웃, 결제, 메일 알림, 주문 등 새로운 계산값으로 대체하고자 합니다.

조언 좀 해주시겠습니까?

감사해요.

오른쪽 후크를 누르면 작동합니다. 하지만 아래 후크 기능에서 새 가격을 얻으려면 코드를 완료(교체)해야 합니다.

add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_prices', 10, 1 );
function custom_cart_items_prices( $cart ) {

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

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

    // Loop Through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Get the product id (or the variation id)
        $product_id = $cart_item['data']->get_id();

        // GET THE NEW PRICE (code to be replace by yours)
        $new_price = 500; // <== Add your code HERE

        // Updated cart item price
        $cart_item['data']->set_price( $new_price ); 
    }
}

코드가 작동합니다.활성 하위 테마(또는 테마)의 php 파일 또는 플러그인 파일에 있습니다.

이 코드는 WooCommerce 버전 3+에서 테스트되고 작동합니다.하지만 코드를 제시하지 않으셨기 때문에 세션에서 새로운 가격을 얻기 위해 테스트할 수 없습니다.

function save_subscription_wrap_data( $cart_item_data, $product_id ) {
    $include_as_a_addon_subscription = get_field('include_as_a_addon_subscription',$product_id);
    $subscricption_product_data = get_field('subscricption_product',$product_id);
    $current_user  = is_user_logged_in() ? wp_get_current_user() : null;
    $subscriptions = wcs_get_users_subscriptions( $current_user->ID );
    if($include_as_a_addon_subscription == "yes")
    {
        foreach ( $subscriptions as $subscription_id =>  $subscription ) {
            $subscription_status = $subscription->get_status();
        }
        if($subscription_status == 'active')
        {
            $cart_item_data[ "subscribe_product" ] = "YES";  
        }
    }
    return $cart_item_data;
     
}
add_filter( 'woocommerce_add_cart_item_data', 'save_subscription_wrap_data', 99, 2 );

function render_meta_on_cart_and_checkout1( $cart_data, $cart_item = null ) {
   $meta_items = array();
    
    if( !empty( $cart_data ) ) {
        $meta_items = $cart_data;
    }
    if( isset( $cart_item["subscribe_product"] ) ) {
        $meta_items[] = array( "name" => "Product Type", "value" => "Package Addon" );
    }
    return $meta_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout1', 100, 2 );

function calculate_gift_wrap_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        
        $additionalPrice = 100;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["subscribe_product"] ) ) {                
                if( method_exists( $value['data'], "set_price" ) ) {
                                        
                    $orgPrice = floatval( $value['data']->get_price() );
                    //$value['data']->set_price( $orgPrice + $additionalPrice );
                    $value['data']->set_price(0);
                } else {
                        
                    $orgPrice = floatval( $value['data']->price );
                    //$value['data']->price = ( $orgPrice + $additionalPrice );
                    $value['data']->price = (0);
                }           
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_gift_wrap_fee', 99 );

언급URL : https://stackoverflow.com/questions/44974645/change-price-of-product-in-woocommerce-cart-and-checkout

반응형