Home / eCommerce / WC Product Bundle Integration Fix for MOQ incorrectly enforced on product bundle child items
Duplicate Snippet

Embed Snippet on Your Site

WC Product Bundle Integration Fix for MOQ incorrectly enforced on product bundle child items

Code Preview
php
<?php
  /**
   * Fix 1: Product page — prevent WWPP from overriding the quantity input args for
   * WC Product Bundle child items.
   *
   * WC Product Bundles names bundled item quantity fields 'bundle_quantity_{id}'.
   * We capture WC PB's intended args at priority 10 (before WWPP's priority 11)
   * and restore them at priority 12.
   */
  class WWPP_Bundle_Product_Page_Fix {
      private static $bundled_item_args = null;
      public static function capture( $args, $product ) {
          if ( isset( $args['input_name'] ) && false !== strpos( $args['input_name'], 'bundle_quantity_' ) ) {
              self::$bundled_item_args = $args;
          }
          return $args;
      }
      public static function restore( $args, $product ) {
          if ( null !== self::$bundled_item_args ) {
              $args = self::$bundled_item_args;
              self::$bundled_item_args = null;
          }
          return $args;
      }
  }
  add_filter( 'woocommerce_quantity_input_args', array( 'WWPP_Bundle_Product_Page_Fix', 'capture' ), 10, 2 );
  add_filter( 'woocommerce_quantity_input_args', array( 'WWPP_Bundle_Product_Page_Fix', 'restore' ), 12, 2 );
  /**
   * Fix 2: Cart page — prevent WWPP from forcing bundled child item quantities up
   * to the product-level MOQ.
   *
   * WWPP's set_cart_item_quantity() runs at priority 10. We snapshot bundled child
   * quantities at priority 9 and restore them at priority 11.
   */
  class WWPP_Bundle_Cart_Fix {
      private static $bundled_qtys = array();
      public static function capture( $cart ) {
          if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
          self::$bundled_qtys = array();
          foreach ( $cart->get_cart() as $key => $item ) {
              if ( ! empty( $item['bundled_by'] ) ) {
                  self::$bundled_qtys[ $key ] = $item['quantity'];
              }
          }
      }
      public static function restore( $cart ) {
          if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
          foreach ( self::$bundled_qtys as $key => $qty ) {
              if ( isset( $cart->cart_contents[ $key ] ) ) {
                  $cart->cart_contents[ $key ]['quantity'] = $qty;
              }
          }
          self::$bundled_qtys = array();
      }
  }
  add_action( 'woocommerce_before_calculate_totals', array( 'WWPP_Bundle_Cart_Fix', 'capture' ), 9 );
  add_action( 'woocommerce_before_calculate_totals', array( 'WWPP_Bundle_Cart_Fix', 'restore' ), 11 );

Comments

Add a Comment