最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to Get WooCommerce Product Price

programmeradmin2浏览0评论

I am trying to manipulate pricing based on measurement. How to get product price. I have here sample codes.

if( isset( $_POST['height_option'] ) )
    $cart_item['custom_data']['height'] = sanitize_key( $_POST['height_option'] );

if( isset( $_POST['width_option'] ) )
    $cart_item['custom_data']['width'] = sanitize_key( $_POST['width_option'] );

// Make calculation and save calculated  price
if( isset( $_POST['height_option'] ) && isset( $_POST['width_option'] ) ){
    $height      = (int) sanitize_key( $_POST['height_option'] );
    $width       = (int) sanitize_key( $_POST['width_option'] );

    if( $width > 0 && $height > 0 ){
        $total_price = ( ( $height ) * ( $width ) ) * 1; // <== The calculation (need to multiply by product price)
        $cart_item['custom_data']['price'] = round($total_price, 2); // Save the price in the custom data
    }
}

return $cart_item;

I am trying to manipulate pricing based on measurement. How to get product price. I have here sample codes.

if( isset( $_POST['height_option'] ) )
    $cart_item['custom_data']['height'] = sanitize_key( $_POST['height_option'] );

if( isset( $_POST['width_option'] ) )
    $cart_item['custom_data']['width'] = sanitize_key( $_POST['width_option'] );

// Make calculation and save calculated  price
if( isset( $_POST['height_option'] ) && isset( $_POST['width_option'] ) ){
    $height      = (int) sanitize_key( $_POST['height_option'] );
    $width       = (int) sanitize_key( $_POST['width_option'] );

    if( $width > 0 && $height > 0 ){
        $total_price = ( ( $height ) * ( $width ) ) * 1; // <== The calculation (need to multiply by product price)
        $cart_item['custom_data']['price'] = round($total_price, 2); // Save the price in the custom data
    }
}

return $cart_item;
Share Improve this question asked May 20, 2020 at 8:10 Mark Gallemit PacatangMark Gallemit Pacatang 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you are looking for to update the cart item price. You can try:

add_action( 'woocommerce_cart_loaded_from_session', array('update_cart_item_price' ),10);

public function update_cart_item_price( $cart_obj ) {
//  This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
    return;
}

if( !WC()->session->__isset( "reload_checkout" )) {

    foreach ( $cart_obj->get_cart() as $key => $value ) {
        
        $_product = wc_get_product($value['product_id']);
        $get_price = $_product->get_price();
        $price_to_add = 10; // write your logic for this

        $value['data']->set_price($get_price + $yards);
    }
  }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论