Advertisement
Fany_VanDaal

úprava množství produktu na stránce pokladny

Nov 3rd, 2022
1,519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.70 KB | None | 0 0
  1. // úprava množství produktu na stránce pokladny
  2. add_filter( 'woocommerce_checkout_cart_item_quantity', 'bbloomer_checkout_item_quantity_input', 9999, 3 );
  3.  
  4. function bbloomer_checkout_item_quantity_input( $product_quantity, $cart_item, $cart_item_key ) {
  5.    $product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
  6.    $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
  7.    if ( ! $product->is_sold_individually() ) {
  8.       $product_quantity = woocommerce_quantity_input( array(
  9.          'input_name'  => 'shipping_method_qty_' . $product_id,
  10.          'input_value' => $cart_item['quantity'],
  11.          'max_value'   => $product->get_max_purchase_quantity(),
  12.          'min_value'   => '0',
  13.       ), $product, false );
  14.       $product_quantity .= '<input type="hidden" name="product_key_' . $product_id . '" value="' . $cart_item_key . '">';
  15.    }
  16.    return $product_quantity;
  17. }
  18.  
  19. // ----------------------------
  20. // Detect Quantity Change and Recalculate Totals
  21.  
  22. add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_update_item_quantity_checkout' );
  23.  
  24. function bbloomer_update_item_quantity_checkout( $post_data ) {
  25.    parse_str( $post_data, $post_data_array );
  26.    $updated_qty = false;
  27.    foreach ( $post_data_array as $key => $value ) {  
  28.       if ( substr( $key, 0, 20 ) === 'shipping_method_qty_' ) {        
  29.          $id = substr( $key, 20 );  
  30.          WC()->cart->set_quantity( $post_data_array['product_key_' . $id], $post_data_array[$key], false );
  31.          $updated_qty = true;
  32.       }      
  33.    }  
  34.    if ( $updated_qty ) WC()->cart->calculate_totals();
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement