Using woocommerce, need to use a specific field from the database, how? - php

I found where in my database the information is that I need to echo out, but I don't know how to echo it out!
It's in the wp_postmeta > web_detail field - see screenshot: https://i.imgur.com/l9AXrX5.png
The data I need to show on the product page is the bit where it currently says: "TJOBBE"
I need this on my content-single-product.php - for every product where this information exists.

You can use get_post_meta() function that retrieves custom fields - https://developer.wordpress.org/reference/functions/get_post_meta/. In your case something like this:
$web_detail = get_post_meta( get_the_ID(), 'web_detail' );
if ( !empty( $web_detail ) ) {
//do smth with this data
}

Related

Update meta field value before custom post load then display post with updated meta value

What I'm trying to do
I'm currently working on a way to update the price of a product when a user go on the product page.
I'm using an API to get the accurate price.
The problem
The function properly update the meta field sellers on back end, but the value of the meta field won't show on front, even after refresh. I have to update the post manually in the back end for the meta field sellers to show on front when refreshing the page.
What I managed to do so far
I managed to handle the API request, then parsed the response. I also managed to fire the API request and modify the price if necessary in the meta field of my product page with an add_action hook. I'm currently using send_headers hook, which is probably not the one to go with but at least it's kinda working :p
The code
add_action( 'send_headers', 'fetch_seller_price' );
function fetch_seller_price(){
//check if is front
if ( !is_admin() ) {
$post_id = get_the_ID();
//Check if is custom post type
if ('bons-plans' == get_post_type( $post_id )) {
//Get ASIN code from custom meta field
$asin_code = get_post_meta( $post_id, 'asin_code', true );
//check if product code is correct
if (strlen( $asin_code ) == 10) {
$sellers = get_post_meta($post_id, 'sellers', true);
foreach ($sellers as $item => $value) {
//Check if seller is Amazon
if ($value["seller"] == "Amazon") {
//get price from api function
$item_price = amazon_price_request( $asin_code );
//If new price != old price, update sellers array with new product price value
if ($value["product-price"] != $item_price) {
$sellers[$item]["product-price"] = $item_price;
}
}
update_post_meta( $post_id, "sellers", $sellers );
}
}
}
}
}
note: sellers meta field is an nested array whitch contains multiple seller arrays of that form:
["item-2"]=>
array(9) {
["seller"]=>
string(6) "Amazon"
["product-link"]=>
string(23) "https://amzn.to/3E90AFS"
["product-price"]=>
string(2) "42"
}
I tried to var_dump() the sellers array after loading the page on front, and also tried with item_price variable. Both return the right number, but the end result appear as it it were empty.
Any help would be greatly appreciated !
I'm kinda new to PHP and I'm well aware my code is kind of trashy, any idea on how to make it proper is also accepted :p
Ok so the problem where simply the way I parsed the json response, variable was float formated, and the expected format was a string :p

Sorting meta field values and select the lowest to store in an other meta field WordPress

What I tried to do:
I have several custom meta field in a custom post type. I want to retrieve the value of all the meta field with the name 'prix-du-produit' for the 'bons-plans' post type, compare them and store the lowest one inside another meta field named 'prix-promo' and do the same thing for the link attached. All of this should happen when the post is saved, published or updated.
It's basically a tool to select the best price of a product to later display it on front end with the corresponding link to the product. For now this isn't working at all and I'm all out of answer.
What I manage to understand or not
As I understood, using usort() is the best way to proceed since it will sort each array inside the first one, which is perfect for what I tried to achieve as it will allow me to select the link within corresponding to the lowest 'prix-du-produit' value. I also understood that using the publish_post hook should do the trick.
The (crappy) code:
function compare_prices($price1, $price2){
if($price1['prix-du-produit'] == $price2['prix-du-produit']){
return rand(0,1) ? 1 : -1//if it is the same then it is random
}
return $price1[0]['prix-du-produit'] > $price2[0]['prix-du-produit'];
// if not it sorts the array
}
function best_product( $id, $post ) {
if ( get_post_type($post->ID) = 'bons-plans' ) {
$products = get_post_meta( $post->ID, 'liens-produits' );
usort($products, 'compare_prices');
$lowest_price = $products[0]['prix-du-produit'];
$best_link = $products[0]['lien-du-produit'];
update_post_meta( $post->ID , 'lien-affilie' , $best_link);
update_post_meta( $post->ID , 'prix-promo' , $lowest_price);
}
}
add_action( 'publish_post', 'best_product', 10, 2 );
Keep in mind that I am still very new to PHP, so for those who are able to give me hint on what I did wrong will have to give me a precise answer.

How to use product excerpt to look for a db row, find a field value and use it to assign post type pr. category

We are working on a little project, and cannot get it to work properly. Quick info: someone fills out a form, info sends to sql database and creates a wordpress post type product (using zapier)
We are trying to make wordpress get the new post's excerpt, find the row, where the first column would have same value as the excerpt and then in that row look for a column with certain name to see whether it is null or not. If not null, we want get that value of field and assign this value as post type product's category.
Ex: If value is glasses, products category would also be glasses. We tried this code (using $wpdb ) , however it did not work properly, and as we are relatively new to php and would really appreciate your insight...
add_action( 'xmlrpc_call', 'prepare_woocommerce_check' );
function prepare_woocommerce_check( $action ) {``if ( 'wp.newPost' === $action ) {
add_action( 'save_post', 'validate_woocommerce_product' );}}
function validate_woocommerce_product( $product_id ) {
remove_action( 'save_post', 'validate_woocommerce_product' );
$product = wc_get_product( $product_id )
$product->set_regular_price(22);
$myexcerpt = apply_filters('the_excerpt', get_the_excerpt($product_id));
global $wpdb;
$result =$wpdb-> get_results("SELECT select FROM info WHERE clienid == $myexcerpt");
if ($result) {
function wp_insert_term() {
wp_insert_term($result, "product_cat");
}
add_action('init','wp_insert_term');
$product->save();
}

How to display data from cmb2 option page?

I have created a repeatable field with CMB2, and created a normal field. This is the function of https://pastebin.com/XUQgkvbi
If you use foreach for repeatable using a post or page then you can show data as: https://pastebin.com/C35vWGDs
And Call normal field without repeatable, then
<?php $ entries = get_post_meta (get_the_ID (), 'yourprefix_group_demo', true); ?>
<?php echo $ entries; ?>
also work.
But the problem is, I do not want to use the above function on any page or post. I want to use it in the Options Page. The above Function option has been added to the option page, but I can not do the data show of those files in any way.
I've tried get_post_meta () and get_option () with two functions, but in no way can I show data from the option page. How can I get the data from the above fields (option page) to the show in the frontend? Please help with a little bit.
I got solution, The options are stored in a single option field. You would loop through the news-section groups with something like this:
$settings = get_option( 'repeatable-news-options.php', array() );
if ( ! empty( $settings['news-section'] ) ) {
foreach ( $settings['news-section'] as $section ) {
echo $section['title'] . '<br/>';
}
}
that link https://wordpress.org/support/topic/how-to-display-data-from-cmb2-option-page/
problem solved.

Conditional if statement for a particular WP page

I am using Woo Commerce plugin and I want to display extra text for a specific product page.
The product ID seen in my body is:
single single-product postid-2624
So I tried the following code but it didn't work:
<?php
function ip_more_content() {
if ( is_single('2624'); ) {
echo 'show something';
}
else {
echo '';
}
}
add_action( 'woocommerce_product_thumbnails' , 'ip_more_content', 9 );
?>
How can I make WP do something based on a specific product id?
I guess that 'woocommerce_product_thumbnails' is running inside the loop, so you cab grab item id by simply using get_the_ID() function. So, edit your conditional like this:
if ( get_the_ID() == 2624 ) {
And it should work it out.

Categories