Created
May 7, 2018 09:26
-
-
Save wpmudev-sls/b4e3acfb3548369a9795c00fc55550ee to your computer and use it in GitHub Desktop.
[MarketPress] - Filter price html with domDocument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: [MarketPress] - Filter price html | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Filter price html with domDocument | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
add_filter( 'mp_product/display_price', function( $html, $price, $product_id ){ | |
$product = new MP_Product( $product_id ); | |
$dom = new DOMDocument( '1.0', 'utf-8' ); | |
$dom->loadHTML($html); | |
$finder = new DomXPath( $dom ); | |
$classname = "mp_product_price-normal"; | |
$nodes = $finder->query("//span[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]"); | |
foreach ( $nodes as $node ) { | |
$price = $product->manage_price_tax( $price['lowest'] ); | |
$class = $node->getAttribute('class'); | |
// Create new price item | |
$price_item = $dom->createElement( 'span', $price ); | |
$price_item->setAttribute('class', $class); | |
$price_item->setAttribute('itemprop', 'price'); | |
// Create the new currency item | |
$currency_item = $dom->createElement( 'span', mp_format_currency( '') ); | |
$currency_item->setAttribute('class', 'currency'); | |
$currency_item->setAttribute('itemprop', 'priceCurrency'); | |
// Create the new tax item | |
//$tax_item = $dom->createElement( 'span', $product->display_tax_string( false ) ); | |
// Method display_tax_string Returns html so: | |
$tax_item = $dom->createDocumentFragment(); | |
$tax_item->appendXML( $product->display_tax_string( false ) ); | |
// Replace original price item with the new one | |
$node->parentNode->replaceChild( $price_item, $node ); | |
// Add the currency item before the price item | |
$price_item->parentNode->insertBefore( $currency_item, $price_item ); | |
// Add the tax item after the price item | |
$price_item->parentNode->appendChild( $tax_item ); | |
} | |
return $dom->saveHTML(); | |
}, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment