| 1 |
<?php namespace TierPricingTable\Integrations\Plugins; |
| 2 |
|
| 3 |
use SW_WAPF\Includes\Classes\Fields; |
| 4 |
|
| 5 |
class WombatProductAddons extends PluginIntegrationAbstract { |
| 6 |
|
| 7 |
public function addAddonsPriceToItem( $price, $cart_item ) { |
| 8 |
|
| 9 |
if ( ! $price ) { |
| 10 |
return $price; |
| 11 |
} |
| 12 |
|
| 13 |
// Premium version |
| 14 |
if ( ! empty( $cart_item['wapf_item_price']['options_total'] ) ) { |
| 15 |
$price += $cart_item['wapf_item_price']['options_total']; |
| 16 |
} |
| 17 |
|
| 18 |
// Free version |
| 19 |
if ( class_exists( 'SW_WAPF\Includes\Classes\Fields' ) && ! empty( $cart_item['wapf'] ) ) { |
| 20 |
$optionsTotal = 0; |
| 21 |
|
| 22 |
foreach ( $cart_item['wapf'] as $field ) { |
| 23 |
if ( ! empty( $field['price'] ) ) { |
| 24 |
foreach ( $field['price'] as $_price ) { |
| 25 |
|
| 26 |
if ( 0 === $_price['value'] ) { |
| 27 |
continue; |
| 28 |
} |
| 29 |
|
| 30 |
$optionsTotal = $optionsTotal + Fields::do_pricing( $_price['value'], $cart_item['quantity'] ); |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
$price += $optionsTotal; |
| 36 |
} |
| 37 |
|
| 38 |
return $price; |
| 39 |
} |
| 40 |
|
| 41 |
public function getTitle(): string { |
| 42 |
return 'Product Fields (Product Addons) by StudioWombat'; |
| 43 |
} |
| 44 |
|
| 45 |
public function getDescription(): string { |
| 46 |
return __( 'Calculate tiered pricing accurately when using StudioWombat Advanced Product Fields.', 'tier-pricing-table' ); |
| 47 |
} |
| 48 |
|
| 49 |
public function getSlug(): string { |
| 50 |
return 'wombat_product_addons'; |
| 51 |
} |
| 52 |
|
| 53 |
public function getAuthorURL(): string { |
| 54 |
return 'https://wordpress.org/plugins/advanced-product-fields-for-woocommerce/'; |
| 55 |
} |
| 56 |
|
| 57 |
public function getIconURL(): string { |
| 58 |
return $this->getContainer()->getFileManager()->locateAsset( 'admin/integrations/wombat-addons-icon.png' ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Render compatibility script |
| 63 |
*/ |
| 64 |
public function addCompatibilityScript() { |
| 65 |
|
| 66 |
if ( ! is_product() ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
?> |
| 71 |
<script> |
| 72 |
// Tiered Pricing WOOCS Compatibility |
| 73 |
(function ($) { |
| 74 |
const updateVariationPrice = function (price, variationId) { |
| 75 |
|
| 76 |
if (!variationId) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if ($('[data-product_variations]').length === 0) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
let variationData = $('[data-product_variations]').data('product_variations'); |
| 85 |
|
| 86 |
if (!variationData) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
variationData = variationData.map(variation => { |
| 91 |
if (variation.variation_id === parseInt(variationId)) { |
| 92 |
variation.display_price = price; |
| 93 |
} |
| 94 |
|
| 95 |
return variation; |
| 96 |
}); |
| 97 |
|
| 98 |
$('[data-product_variations]').data('product_variations', variationData); |
| 99 |
}; |
| 100 |
|
| 101 |
$(document).on('tiered_price_update', function (event, data) { |
| 102 |
|
| 103 |
if (typeof WAPF !== 'undefined') { |
| 104 |
// the variable is defined |
| 105 |
WAPF.Filter.add('wapf/pricing/base', function (_price, _wrapper) { |
| 106 |
return data.price; |
| 107 |
}); |
| 108 |
|
| 109 |
// Trigger update totals. |
| 110 |
$('.wapf').find('input, select, textarea').trigger('change'); |
| 111 |
} |
| 112 |
|
| 113 |
// Free version |
| 114 |
if ($('.wapf-product-totals').length) { |
| 115 |
$('.wapf-product-totals').data('product-price', data.price); |
| 116 |
|
| 117 |
const productId = parseInt(data.__instance.$getPricingElement().data('product-id')); |
| 118 |
|
| 119 |
updateVariationPrice(data.price, productId); |
| 120 |
} |
| 121 |
}); |
| 122 |
})(jQuery); |
| 123 |
</script> |
| 124 |
<?php |
| 125 |
} |
| 126 |
|
| 127 |
public function run() { |
| 128 |
|
| 129 |
add_action( 'wp_footer', array( $this, 'addCompatibilityScript' ) ); |
| 130 |
|
| 131 |
add_filter( 'tiered_pricing_table/cart/product_cart_price', array( $this, 'addAddonsPriceToItem' ), 20, 2 ); |
| 132 |
|
| 133 |
add_filter( 'tiered_pricing_table/cart/product_cart_price/item', array( |
| 134 |
$this, |
| 135 |
'addAddonsPriceToItem', |
| 136 |
), 20, 2 ); |
| 137 |
|
| 138 |
add_filter( 'tiered_pricing_table/cart/product_cart_old_price', array( $this, 'addAddonsPriceToItem' ), 20, 2 ); |
| 139 |
} |
| 140 |
|
| 141 |
public function getIntegrationCategory(): string { |
| 142 |
return 'product_addons'; |
| 143 |
} |
| 144 |
} |
| 145 |
|