php [MarketPress] - 使用domDocument过滤价格html

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [MarketPress] - 使用domDocument过滤价格html相关的知识,希望对你有一定的参考价值。

<?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 );

以上是关于php [MarketPress] - 使用domDocument过滤价格html的主要内容,如果未能解决你的问题,请参考以下文章

php [MarketPress] - 结帐自定义字段

php [MarketPress] - 出口订单

php [MarketPress] - 重复产品

php [MarketPress] - 过滤地址字段

php [MarketPress - Upfront] - 产品搜索

php 强制MarketPress变化模态100%宽度