Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created May 7, 2018 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/b4e3acfb3548369a9795c00fc55550ee to your computer and use it in GitHub Desktop.
Save wpmudev-sls/b4e3acfb3548369a9795c00fc55550ee to your computer and use it in GitHub Desktop.
[MarketPress] - Filter price html with domDocument
<?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