Helpers
We moved these functions to static class WDP_Functions (but legacy class WDP_Frontend can be used too).
adp_functions()->getGiftedCartProducts() just returns array of free items (added by our plugin) as [product_id_1=>qty_1, product_id_2=>qty_2,..]
adp_functions()->getActiveRulesForProduct($productId, $qty = 1, $useEmptyCart = false) adds passed product to existing cart (or empty cart) and returns array of applied rules.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
add_filter( 'woocommerce_get_price_html', function ( $price, $product ) { if ( empty( $product ) or empty( WC()->customer ) ) { return $price; } $context = new \ADP\BaseVersion\Includes\Context(); $rule = null; foreach ( adp_functions()->getActiveRulesForProduct( $product->get_id(), $qty = 50, $use_empty_cart = true ) as $loopRule ) { // discount table only for 'SingleItem' rule if ( $loopRule instanceof \ADP\BaseVersion\Includes\Rule\Structures\SingleItemRule && $loopRule->getProductRangeAdjustmentHandler() ) { $rule = $loopRule; break; } } if ( ! $rule ) { return $price; } $handler = $rule->getProductRangeAdjustmentHandler(); $ranges = $handler->getRanges(); $range = array_pop( $ranges ); if ( ! $range ) { return $price; } $priceProcessor = new \ADP\BaseVersion\Includes\Product\Processor( $context ); $cartBuilder = new \ADP\BaseVersion\Includes\Cart\CartBuilder( $context ); $cart = $cartBuilder->create( WC()->customer, WC()->session ); $priceProcessor->withCart( $cart ); $priceFunctions = new \ADP\BaseVersion\Includes\External\WC\PriceFunctions( $context ); $processedProd = $priceProcessor->calculateProduct( $product, $range->getFrom() ); return "From " . $priceFunctions->format( $priceFunctions->getProcProductPriceToDisplay( $processedProd ) ); }, PHP_INT_MAX, 2 ); |
adp_functions()->calculateProduct($product, $qty, $useEmptyCart = true) allows you to calculate an object \ADP\BaseVersion\Includes\Product\ProcessedProductSimple
or an object \ADP\BaseVersion\Includes\Product\ProcessedVariableProduct.
And in the pro version it calculates an object \ADP\ProVersion\Includes\Product\ProcessedVariableProduct.
adp_functions()->getDiscountedProductsForCart($listOfProducts, $plain = false) allows you to calculate price for list of products. You can use the following parameters:
- listOfProducts
array[][‘product_id’]
array[][‘qty’]
array[][‘cart_item_data’] Optional - $plain
true/false
The function has 2 different return schemas.
It sounds a bit confusing, but you can use this way to get list of products which should be added to pre-build cart to get discounts.
1 2 3 4 5 6 7 |
adp_functions()->getDiscountedProductsForCart( array( // products will be added to empty WC cart to check conditions array( 'product_id' => 93, 'qty' => 1 ), array( 'product_id' => 95, 'qty' => 1 ), // may add 2nd product ), true // TRUE - plain array , FALSE - split by rules ); |
adp_functions()->getDiscountedProductPrice($product, $qty, $useEmptyCart = true) allows you to calculate price for product. You can use the following parameters:
- int|WC_product $product(product id or product object)
- float $qty
- bool $useEmptyCart
The function has 3 different return schemas which are depended on first provided parameter:
float|array|null
float for simple product
array is (min, max) range for variable
null if product is incorrect