I have created a custom column with a custom plugin.
I dont know if I can use the following to populate this column with a value from an xml.
//POPULATE COLUMN
add_action('manage_posts_custom_column', 'wnetpp_populate_custom_columns3', 10, 2);
function wnetpp_populate_custom_columns3( $column, $post_id ) {
if ($column == 'test_column3') {
$current_product = wc_get_product( $post_id );
$product_sku = $current_product->get_sku();
read_parse_xml($product_sku)
} }
and with a function like the following:
function read_parse_xml($product_sku)
{
$url = 'https://example.com/ProductsUpdates4.xml';
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);
foreach($xml as $x) {
$sku = $x->sku;
$bfsku = $x->bf_sku;
$suppliersku = $x->supplier_sku;
$price = $x->price;
$availability = $x->availability;
if($sku==$product_sku)
{
echo '<div id="_supprice-' . $post_id . '">' . $price . '</div>';
}
}
}
But nothing is working.
I get a blank page for products.
How I am supposed to check if xml has been accessed.
First I forgot to put a semicolumn ; after read_parse_xml($product_sku)
For each column I created a different read_parse_xml($product_sku) function called read_parse_xml1 2 3 etc.
and I used number_format to format my string to Number like
if($sku==$product_sku)
{
echo '<div id="_supprice-' . $post_id . '">' . number_format( (float)$price,2) . € . '</div>';
}
Even though I dont know if there is another proper way with less code to do it, it worked for me
Related
I have constructed the below function to work in conjunction with a button displayed on the edit product page. It is designed to generate some description text based on the title and SKU of the product. The code works perfectly for 'simple' products, but I am struggling to get it to work for 'variable' products too.
What exactly do I need to do to get it to work correctly for both simple and variable products?
The current behaviour is:
When triggered on a simple product, it adds the new description or updates the old one to the new one.
When triggered on a variable product, it updates the main description, but deletes all variations which were previously set up on the product.
add_action('woocommerce_process_product_meta', 'update_and_save_utapd');
function update_and_save_utapd() {
if(isset($_POST['button_new_update_description'])){
$product_id = wc_get_product(get_the_ID());
$wcProduct = new WC_Product($product_id);
$get_title = $wcProduct->get_name();
$get_mpn = $wcProduct->get_meta('sp_wc_barcode_field');
$get_description = $wcProduct->get_description();
$output = '';
if(!empty($get_title)){
$output .= "<p>The " . $get_title;
}if(!empty($get_mpn)){
$output .= " (MPN: " . $get_mpn . ").";
}if(!empty($get_description)){
$wcProduct->set_description($output);
$wcProduct->save();
return "<div>SUCCESS: YOU HAVE UPDATED YOUR DESCRIPTION.</div>";
}elseif(empty($get_description)){
$wcProduct->set_description($output);
$wcProduct->save();
return "<div>SUCCESS: YOU HAVE GENERATED A NEW DESCRIPTION.</div>";
}
}
}
First when using action hooks in backend that save product data, you can't return a string (a text) as you are trying to do and anyway, it will never be displayed
Now since WooCommerce 3 you can use woocommerce_admin_process_product_object much better hook that include the WC_Product Object as function argument and there is no need to use save() method at the end of your code as once this hook is triggered the save() method is auto applied.
So we can simplify your code:
add_action('woocommerce_admin_process_product_object', 'update_and_save_utapd');
function update_and_save_utapd( $product ) {
if( isset($_POST['button_new_update_description']) ){
$name = $product->get_name();
$barcode = $product->get_meta('sp_wc_barcode_field');
$output = '';
if ( ! empty($name) ) {
$output .= "<p>The " . $name;
}
if ( ! empty($barcode) ) {
$output .= " (MPN: " . $barcode . ").";
}
$product->set_description( $output );
}
}
Code goes in functions.php file of the active child theme (or active theme). It should better work now, without throwing errors.
With the woocommerce_process_product_meta hook you already have the product id and the product object available. Here you will find more information.
To verify that the button has been clicked you must also check its value, in addition to the isset() function.
Replace value_button with the value of the element's value
attribute
add_action('woocommerce_process_product_meta', 'update_and_save_utapd', 10, 2 );
function update_and_save_utapd( $product_id, $product ) {
// replace "value_button" with the value of the element's "value" attribute
if ( isset( $_POST['button_new_update_description'] ) && $_POST['button_new_update_description'] == 'value_button' ) {
if ( $product->is_type('simple') || $product->is_type('variable') ) {
$title = $product->get_name();
$mpn = $product->get_meta('sp_wc_barcode_field');
$description = $product->get_description();
$output = '';
if ( ! empty($title) ) {
$output .= "<p>The " . $title;
}
if ( ! empty($mpn) ) {
$output .= " (MPN: " . $mpn . ").";
}
if ( ! empty($get_description) ) {
$product->set_description($output);
$product->save();
return "<div>SUCCESS: YOU HAVE UPDATED YOUR DESCRIPTION.</div>";
} else {
$product->set_description($output);
$product->save();
return "<div>SUCCESS: YOU HAVE GENERATED A NEW DESCRIPTION.</div>";
}
}
}
}
I'm learning PHP and WordPress development, so I assumed that maybe here I'll find the answer or a tip.
I've restriced the_content based on user role. After the end of the_content I'd like to display button whitch is unique to the specific post. So here is the code which display that:
function displaycontent($content) {
if(is_singular( 'custom_post_type' )) {
$aftercontent = 'I Want To Add Code Here';
$fullcontent = $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
And I'd like to insert code below into the underlined place above:
<?php
$post = $wp_query->post;
$group_id = get_field( 'link_number' , $post->ID );
if( $group_id ) {
echo do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
}
?>
How can I do that?
It's probably better to create a custom shortcode for this. If you change how the_content works, it will be global, everywhere.
Note:
This code is completely untested and put together after 5 min of Googling, so if somehting is wrong, feel free to comment and I'll amend it. It should be fairly close and is mostly for explaining the concept instead of a pure copy/paste solution
Register a new shortcode:
add_shortcode('my_awesome_content', 'my_awesome_content_func');
Create the callback function:
Here we've added $atts which will contain our attribute (the post id):
function my_awesome_content_func($atts = [])
{
$postId = $atts['postid'] ?? null;
if ($postId === null) {
// We got no id so let's bail
return null;
}
$post = get_post($postId);
if (!$post) {
// We didn't find any post with the id so again, let's bail
return null;
}
$group_id = get_field( 'link_number' , $post->ID );
$content = $post->content;
if( $group_id ) {
$content .= do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
}
return $content;
}
Usage:
Now you should be able to call it like this:
echo do_shortcode('[my_awesome_content postid="' . $post->ID . '"]');
You can just embed you code below in the above filter with some modifications:
function displaycontent($content) {
if (is_singular('custom_post_type')) {
$post_id = get_queried_object_id();
$group_id = get_field('link_number', $post_id);
$aftercontent = '';
if ($group_id)
$aftercontent = do_shortcode('[checkout_button class="button" level="'.$group_id. '" text="Order"]');
$fullcontent = $content.$aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
OK, thanks everyone, I Got it! Here is the code if anyone would have same problem:
function displaycontent($content) {
if(is_singular( 'custom_post_type' )) {
$group_id = get_field('link_number');
$aftercontent = do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
I have a page called used-cars and I'm trying filter its title and display it as an H1 header. I have rewrite the permalink structure to pass double terms e.g example.com/used-cars/term1/term2 and it works like a charm
So at this point. I'm trying to filter the page title to match the URLs, but I just can't get it to work with this code
`add_filter( 'wp_title', 'new_listing_title', 10, 1 );
function new_listing_title($title)
{
if ( is_page('used-cars') && $id = get_queried_object_id() )
{
$locations = get_query_var('area');
$location = get_term_by('slug', $locations, 'area');
$models = get_query_var('serie');
$model = get_term_by('slug', $models, 'serie');
$title = '';
if($model && $model) $title .= $model->name . ' Used';
else $title .= 'Used';
$title .= ' Cars For Sale';
if($location && $location) $title .= ' In ' . $location->name;
return $title;
}
return $title;
}`
However when I use this. it works
global $wp_query;
echo 'Car : ' . $wp_query->query_vars['serie'];
echo '<br />';
echo 'Area : ' . $wp_query->query_vars['area'];
So how can I Incorporate these two solutions to filter the title of this page's title?
For anybody in search for this solution. This is how I finally resolved this problem.
I hooked into wpseo_title instead of wp_title which has been discontinued since Wordpress 4.4
Hope this helps.
So I'm trying to achieve the following.
My code so far..
add_filter('wpseo_title', 'vehicle_listing_title');
function vehicle_listing_title( $title )
{
if ( get_post_type() == 'vehicles' )
{
$location = get_the_terms($post->ID, 'vehicle_location');
$model = get_the_terms($post->ID, 'vehicle_model');
$title = $model . 'used cars for sale in' . $location .'on'. get_bloginfo('name');
}
return $title;
}
This code results in $location & $model being an object containing the following term_id =>,name=>,slug=>,term_group=>,etc so I want to get the name part of it.
How do I do that?
What do I have to add to the code to still return the modified $title even when there aren't any posts assigned to the queried taxonomies?
Change your code to this:
add_filter('wpseo_title', 'vehicle_listing_title');
function vehicle_listing_title( $title )
{
if ( get_post_type() == 'vehicles' )
{
$location = get_the_terms($post->ID, 'vehicle_location');
$model = get_the_terms($post->ID, 'vehicle_model');
$title = '';
if($model && $model[0]) $title .= $model[0]->name . ' used';
else $title .= 'Used';
$title .= ' cars for sale';
if($location && $location[0]) $title .= ' in ' . $location[0]->name;
$title .= ' on ' . get_bloginfo('name');
return $title;
}
return $title;
}
Basically, you need to construct your title using IF's to check if the terms array could be obtained for the model and the location. Also, wp_terms() returns an array of term arrays, hence why you also need to obtain the first element of the result using a [0] index, and then chaining the ['name'] index to get the name of the term.
I have a coupon site, which basically has many vendors in it. I want to create a related stores widget kind of a thing which would display 5-6 stores on the side bar. and i want to them to relate to the current vendor page. till now i have managed to display 5-6 stores links randomly, but i am not able to display the related stores.
I am using clipper theme in wordpress, and i have kept all my stores in Coupons-Stores and not Post-categories.
<?php
$stores = get_terms('stores'); // Note: this returns NULL non-empty cats. It is not normal, could be because of the widget.
// Get all non-empty stores
$nonempty_stores = array();
foreach( $stores as $store )
if ( !empty( $store ) )
$nonempty_stores[] = $store;
$stores_count = count($nonempty_stores);
global $random_seed;
// Set initial seed value
$random_seed = $last_update;
if (DISPLAY_SAME_STORES_LIST_FOR_ALL_STORE_PAGES == false)
$random_seed = $random_seed + $store_catid;
function mtech_rand($min, $max)
{
global $random_seed;
$random_seed = ((1103515245*$random_seed)+12345)%(1<<31);
return (($random_seed%($max-$min+1))+$min);
}
echo "<div class=\"hdsboxbg\"> ";
$cntr = 0;
$displayed_cntr = 0;
while (($cntr < $stores_count) && ($displayed_cntr < NUM_OF_RANDOM_STORES_TO_DISPLAY))
{
$rand_store_idx = mtech_rand($cntr, $stores_count-1);
$rand_store = $nonempty_stores[$rand_store_idx];
if (($store_catid == 0) || ($rand_store->cat_ID != $store_catid)) // Random Stores Widget should not display a link to the current store page
{
$a_text = $rand_store->name . ' Coupons';
//$a_text = $rand_store->name;
echo "<div class=\"hdsmod\"> $a_text </div>";
$displayed_cntr++;
}
$nonempty_stores[$rand_store_idx] = $nonempty_stores[$cntr];
$nonempty_stores[$cntr] = $rand_store;
$cntr++;
}
echo "</div>";
?>
Any other methods are welcome. i have tried to display all the stores like this,
$terms = get_terms('stores');
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
it displays but i am not able to display the related stores, i am struggling to figure out on which grounds should i proceed. Any help would be appreciated.
got it.
<?php
$taxonomy = 'stores';
$args1=array(
'include'=> array(12,30)
);
$terms = get_terms('stores',$args1 );
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>