| 1 |
<?php |
| 2 |
|
| 3 |
namespace TierPricingTable\Admin\ProductPage; |
| 4 |
|
| 5 |
use TierPricingTable\Core\ServiceContainerTrait; |
| 6 |
use TierPricingTable\PriceManager; |
| 7 |
use TierPricingTable\Forms\MinimumOrderQuantityForm; |
| 8 |
use TierPricingTable\Forms\TieredPricingRulesForm; |
| 9 |
use WC_Product; |
| 10 |
/** |
| 11 |
* Class ProductManager |
| 12 |
* |
| 13 |
* @package TierPricingTable\Admin\Product |
| 14 |
*/ |
| 15 |
class Product { |
| 16 |
use ServiceContainerTrait; |
| 17 |
public function __construct() { |
| 18 |
// Simple |
| 19 |
add_action( 'woocommerce_product_options_pricing', function () { |
| 20 |
global $product_object; |
| 21 |
if ( $product_object instanceof WC_Product ) { |
| 22 |
$this->renderPricingRulesForm( (int) $product_object->get_id(), null ); |
| 23 |
} |
| 24 |
} ); |
| 25 |
add_action( 'woocommerce_process_product_meta', array($this, 'updatePriceRules') ); |
| 26 |
// Variation |
| 27 |
add_action( |
| 28 |
'woocommerce_variation_options_pricing', |
| 29 |
function ( $loop, $variation_data, $variation ) { |
| 30 |
$minimum = PriceManager::getProductQtyMin( $variation->ID, 'edit' ); |
| 31 |
MinimumOrderQuantityForm::render( null, $loop, $minimum ); |
| 32 |
do_action( 'tiered_pricing_table/admin/after_minimum_order_quantity_field', $variation->ID, $loop ); |
| 33 |
$this->renderPricingRulesForm( (int) $variation->ID, $loop ); |
| 34 |
}, |
| 35 |
10, |
| 36 |
3 |
| 37 |
); |
| 38 |
add_action( |
| 39 |
'woocommerce_save_product_variation', |
| 40 |
function ( $productId, $loop ) { |
| 41 |
if ( wp_verify_nonce( true, true ) ) { |
| 42 |
// as phpcs comments at Woo is not available, we have to do such a trash |
| 43 |
$woo = 'Woo, please add ignoring comments to your phpcs checker'; |
| 44 |
} |
| 45 |
$this->updatePriceRules( $productId, $loop ); |
| 46 |
}, |
| 47 |
10, |
| 48 |
3 |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Update price quantity rules for simple product |
| 54 |
* |
| 55 |
* @param int|string $productId |
| 56 |
* @param ?int|string $loop |
| 57 |
*/ |
| 58 |
public function updatePriceRules( $productId, $loop = null ) { |
| 59 |
if ( wp_verify_nonce( true, true ) ) { |
| 60 |
// as phpcs comments at Woo is not available, we have to do such a trash |
| 61 |
$woo = 'Woo, please add ignoring comments to your phpcs checker'; |
| 62 |
} |
| 63 |
$data = $_POST; |
| 64 |
$prefix = ''; |
| 65 |
if ( null === $loop && !empty( $data['product-type'] ) && 'variable' === $data['product-type'] ) { |
| 66 |
$prefix = '_variable'; |
| 67 |
} |
| 68 |
$tieredPricingData = TieredPricingRulesForm::getDataFromRequest( |
| 69 |
null, |
| 70 |
$loop, |
| 71 |
$prefix, |
| 72 |
$data, |
| 73 |
$productId |
| 74 |
); |
| 75 |
update_post_meta( $productId, '_fixed_price_rules', $tieredPricingData['fixed_tiered_pricing_rules'] ); |
| 76 |
} |
| 77 |
|
| 78 |
protected function renderPricingRulesForm( int $productId, $loop = null ) { |
| 79 |
$type = PriceManager::getPricingType( $productId, 'fixed', 'edit' ); |
| 80 |
$percentageRules = PriceManager::getPercentagePriceRules( $productId, 'edit' ); |
| 81 |
$fixedRules = PriceManager::getFixedPriceRules( $productId, 'edit' ); |
| 82 |
TieredPricingRulesForm::render( |
| 83 |
$productId, |
| 84 |
null, |
| 85 |
$loop, |
| 86 |
$type, |
| 87 |
$percentageRules, |
| 88 |
$fixedRules |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
|