Last active
October 20, 2022 10:37
-
-
Save opicron/efa778d2ccba6d66c94fdf991f5cf842 to your computer and use it in GitHub Desktop.
change sku to id for woocommerce product table #php #woocommerce
This file contains hidden or 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 | |
function my_get_product_id_by_sku( $sku = false ) { | |
global $wpdb; | |
if( !$sku ) | |
return null; | |
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) ); | |
if ( $product_id ) | |
return $product_id; | |
return null; | |
} | |
function my_the_content($content) | |
{ | |
if ( strpos($content,"[product_table") !== false ) | |
{ | |
preg_match('/include=\"(.*?)\"/', $content, $match); | |
if ( $match ) | |
{ | |
$skus = explode(',', $match[1]); | |
$ids = array(); | |
foreach ($skus as $key => $id) | |
{ | |
$ids[$key] = my_get_product_id_by_sku($id); | |
} | |
$idlist = implode(',', $ids); | |
$content = preg_replace('/include=\"(.*?)\"/', 'include="'.$idlist.'"',$content); | |
} | |
} | |
return $content; | |
} | |
add_filter('the_content','my_the_content',10,1); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment