I am making customization to a cliënts WordPress website. However, I need to retrieve price information from the HTML attribute to my functions.php file. The price is being calculated and added in the attribute data-field-price=”0″ as you choose more products.
<li class="pewc-group pewc-item pewc_group_3414_3415 pewc-group-products pewc-item-products pewc-field-3415 pewc-field-count-0 pewc-item-products-checkboxes pewc-active-field" data-id="pewc_group_3414_3415" data-field-id="3415" data-field-type="products" data-field-price="0" data-field-label="" data-field-value="0"></li>
Since I can’t use Javascript I need to get the value of data-field-price=”0″ ( so in this case the 0 ). How do I achieve this with only PHP? Please help.
3
Answers
You can spilt it as a string using the explode method or create an hidden input on your html to catch the value like that
Add an ID to the
<li>
in the HTML file.Then you can use “JavaScript” in your PHP file!
Use
file_get_contents()
to read the HTML page.The element is produced by WooCommerce in WordPress. WordPress core, and 3rd party plugins contain a huge array of filters and actions to get information like this.
WooCommerce’s is documented here : WooCommerce Hooks and Filters
The filter in particular which may be of interest is : woocommerce_cart_item_price
I am no expert of WooCommerce, but write wordpress plugins myself so I would suggest that you could get your information as follows by adding a filter into your theme (eg in your functions.php) :