Skip to content

Instantly share code, notes, and snippets.

@mkhairul
Created March 5, 2015 11:54
Show Gist options
  • Save mkhairul/8ee5842bcd189125d4be to your computer and use it in GitHub Desktop.
Save mkhairul/8ee5842bcd189125d4be to your computer and use it in GitHub Desktop.
Replace placeholder images with attribute values (woocommerce)
<?php
add_filter('woocommerce_cart_item_thumbnail', 'get_attr_image', 10, 3);
function get_attr_image($image, $item, $item_key)
{
$img_url = array_shift( wc_get_product_terms( $item['product_id'], 'pa_image', array( 'fields' => 'names' ) ) );
if($img_url)
{
$dom = new DOMDocument();
$dom->loadHTML($image);
$node = $dom->getElementsByTagName('img');
$node->item(0)->setAttribute('src', $img_url);
$image = $node->item(0)->ownerDocument->saveHTML($node->item(0));
}
return $image;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment