| 1 |
<?php namespace TierPricingTable\Integrations\Plugins; |
| 2 |
|
| 3 |
use TierPricingTable\CalculationLogic; |
| 4 |
use TierPricingTable\PriceManager; |
| 5 |
|
| 6 |
class YithRequestAQuote extends PluginIntegrationAbstract { |
| 7 |
|
| 8 |
public function run() { |
| 9 |
|
| 10 |
add_action( 'ywraq_quote_adjust_price', function ( $raq, \WC_Product $product ) { |
| 11 |
|
| 12 |
$quantity = $this->getProductCount( $product->get_parent_id() ? $product->get_parent_id() : $product->get_id(), |
| 13 |
$raq['quantity'] ? $raq['quantity'] : 1 ); |
| 14 |
|
| 15 |
$pricingRule = PriceManager::getPricingRule( $product->get_id() ); |
| 16 |
|
| 17 |
$newPrice = $pricingRule->getTierPrice( $quantity, false, 'cart', false ); |
| 18 |
|
| 19 |
if ( $newPrice ) { |
| 20 |
$product->set_price( $newPrice ); |
| 21 |
} |
| 22 |
}, 10, 2 ); |
| 23 |
|
| 24 |
add_filter( 'woocommerce_cart_product_subtotal', function ( $product_subtotal, $product, $quantity ) { |
| 25 |
|
| 26 |
if ( ! class_exists( 'YITH_Request_Quote' ) ) { |
| 27 |
return $product_subtotal; |
| 28 |
} |
| 29 |
|
| 30 |
$pricingRule = PriceManager::getPricingRule( $product->get_id() ); |
| 31 |
|
| 32 |
$totalQuantity = $this->getProductCount( $product->get_parent_id() ? $product->get_parent_id() : $product->get_id(), |
| 33 |
$quantity ); |
| 34 |
|
| 35 |
$newPrice = $pricingRule->getTierPrice( $totalQuantity, false, 'cart', false ); |
| 36 |
|
| 37 |
if ( $newPrice ) { |
| 38 |
$product_subtotal = wc_price( $newPrice * $quantity ); |
| 39 |
} |
| 40 |
|
| 41 |
return $product_subtotal; |
| 42 |
|
| 43 |
}, - 99999, 6 ); |
| 44 |
} |
| 45 |
|
| 46 |
protected function getProductCount( $productId, $default ) { |
| 47 |
|
| 48 |
if ( ! function_exists( 'YITH_Request_Quote' ) ) { |
| 49 |
return $default; |
| 50 |
} |
| 51 |
|
| 52 |
if ( CalculationLogic::considerProductVariationAsOneProduct() ) { |
| 53 |
$count = 0; |
| 54 |
|
| 55 |
foreach ( YITH_Request_Quote()->get_raq_return() as $raq ) { |
| 56 |
|
| 57 |
if ( $raq['product_id'] == $productId ) { |
| 58 |
$count += $raq['quantity']; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if ( $count > 0 ) { |
| 63 |
return $count; |
| 64 |
} else { |
| 65 |
return $default; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return $default; |
| 70 |
} |
| 71 |
|
| 72 |
public function getIconURL(): string { |
| 73 |
return $this->getContainer()->getFileManager()->locateAsset( 'admin/integrations/yith-raq-icon.jpeg' ); |
| 74 |
} |
| 75 |
|
| 76 |
public function getAuthorURL(): string { |
| 77 |
return 'https://wordpress.org/plugins/yith-woocommerce-request-a-quote/'; |
| 78 |
} |
| 79 |
|
| 80 |
public function getTitle(): string { |
| 81 |
return __( 'YITH Request a Quote', 'tier-pricing-table' ); |
| 82 |
} |
| 83 |
|
| 84 |
public function getDescription(): string { |
| 85 |
return __( 'Make tiered pricing properly work with request a quote form.', 'tier-pricing-table' ); |
| 86 |
} |
| 87 |
|
| 88 |
public function getSlug(): string { |
| 89 |
return 'yith-request-a-quote'; |
| 90 |
} |
| 91 |
|
| 92 |
public function getIntegrationCategory(): string { |
| 93 |
return 'other'; |
| 94 |
} |
| 95 |
} |
| 96 |
|