How to modify one condition inside a function without arg input? - php

I need to call a function that output a html. Inside this function, there's one condition to exclude one row from the html, I need this row. This condition is not an argument of the function. It's hard coded. So, I have to code my own, or, is there a way to modify this condition?
function get_media_item( $attachment_id, $args = null ) {
//....lots stuffs, then, arrive this part:
$gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
$order = '';
foreach ( $form_fields as $key => $val ) {
if ( 'menu_order' == $key ) {
if ( $gallery )
$order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ). "' /></div>";
else
$order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
unset( $form_fields['menu_order'] );
break;
}
}
//... other stuffs
}
I need to call this function in other tabs, not "gallery" tab. So, I can't get the $order input box.

If you don't want to pass a parameter to the function(which would be recommended btw) to modify the condition, you can use globalize variable(or session).
Example:
function test(){
global $instructions;
if (isset($instructions['fetch'])){
// new codes
} else {
// existing codes
}
return $result;
}
// implement:
$instructions = array();
$instructions['fetch'] = TRUE;
echo test();

Related

Adding CSS class as a variable for PHP loop

I have a PHP function that takes a table that has been filtered using gravity flow. This function loops through each row of the table. I want to change the styling of this table. I have a specific CSS class I want the table styling to have. This class is "tr-shadow". I would apply this CSS class to each row of the table. I have a $style variable that is used for the CSS. How do I add the CSS class as a string for the $style variable so that the table can print out with the CSS from that class? Here is the code below.
'''
public function single_row_columns( $item ) {
list( $columns, $hidden ) = $this->get_column_info();
foreach ( $columns as $column_name => $column_display_name ) {
$class = "class='$column_name column-$column_name'";
$style = "class='tr-shadow'";
if ( in_array( $column_name, $hidden ) ) {
$style = "class='tr-shadow'";
}
$data_label = ( ! empty( $column_display_name ) ) ? " data-label='$column_display_name'" : '';
$attributes = "$class$style$data_label";
if ( 'cb' == $column_name ) {
echo '<th data-label="' . esc_html__( 'Select', 'gravityflow' ) . '" scope="row" class="check-column">';
echo $this->column_cb( $item );
echo '</th>';
} elseif ( method_exists( $this, 'column_' . $column_name ) ) {
echo "<td $attributes>";
echo call_user_func( array( $this, 'column_' . $column_name ), $item );
echo '</td>';
} else {
echo "<td $attributes>";
echo $this->column_default( $item, $column_name );
echo '</td>';
}
}
}
'''
As you can see I have already tried adding "$style="class='tr-shadow". However, this doesn't add any style to the tables. I am assuming that I am not correctly formatting the $style variable in a way that the css class is recognizable. How can I use the $style variable to successfully output the css class to each row of the table.
Your $style variable is including the attribute style again, which will result in something like:
class="column_name column-bar class=' ...
which is invalid.
I'd recommend changing your code to something like this:
public function single_row_columns( $item ) {
list( $columns, $hidden ) = $this->get_column_info();
foreach ( $columns as $column_name => $column_display_name ) {
$class = "$column_name column-$column_name";
$class .= " tr-shadow";
if ( in_array( $column_name, $hidden ) ) {
$class .= " tr-shadow";
}
$data_label = ( ! empty( $column_display_name ) ) ? " data-label='$column_display_name'" : '';
$attributes = "$data_label";
if ( 'cb' == $column_name ) {
echo '<th data-label="' . esc_html__( 'Select', 'gravityflow' ) . '" scope="row" class="check-column">';
echo $this->column_cb( $item );
echo '</th>';
} elseif ( method_exists( $this, 'column_' . $column_name ) ) {
echo "<td class=\"$class\" $attributes>";
echo call_user_func( array( $this, 'column_' . $column_name ), $item );
echo '</td>';
} else {
echo "<td class=\"$class\" $attributes>";
echo $this->column_default( $item, $column_name );
echo '</td>';
}
}
}
Also, it looks like class is always tr-shadow regardless of what you do.
In this code fragment you set the classes:
$class = "class='$column_name column-$column_name'";
$style = "class='tr-shadow'";
And then you concatenate it in one string:
$attributes = "$class$style$data_label";
So that, I think your second class is ignored.

Conditional function that check if products are already in cart in Woocommerce 3

Hi solution provided here WooCommerce - Check if item's are already in cart working perfect. This is the function code:
function woo_in_cart($arr_product_id) {
global $woocommerce;
$cartarray=array();
foreach($woocommerce->cart->get_cart() as $key => $val ) {
$_product = $val['product_id'];
array_push($cartarray,$_product);
}
if (!empty($cartarray)) {
$result = array_intersect($cartarray,$arr_product_id);
}
if (!empty($result)) {
return true;
} else {
return false;
};
}
Usage
$my_products_ids_array = array(22,23,465);
if (woo_in_cart($my_products_ids_array)) {
echo 'ohh yeah there some of that products in!';
}else {
echo 'no matching products :(';
}
But I need use as if(in_array) but no luck so far. What i doing wrong?
$my_products_ids_array = array("69286", "69287",);
if (in_array("69286", $my_products_ids_array)) {
echo '<p>' . the_field ( 'cart_field', 'option' ) . '</p>';
}
if (in_array("69287", $my_products_ids_array)) {
echo '<p>' . the_field ( 'cart_field-1', 'option' ) . '</p>';
}
Thank you
Your main function code is outdated.
For Advanced custom fields (ACF):
you need to use get_field() (that return the field value) instead of the_field() (that echo the field value).
You may need to add a product ID as 2nd argument in get_field('the_slug', $product_id ).
So try:
function is_in_cart( $ids ) {
// Initialise
$found = false;
// Loop Through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
// For an array of product IDS
if( is_array($ids) && ( in_array( $cart_item['product_id'], $ids ) || in_array( $cart_item['variation_id'], $ids ) ) ){
$found = true;
break;
}
// For a unique product ID (integer or string value)
elseif( ! is_array($ids) && ( $ids == $cart_item['product_id'] || $ids == $cart_item['variation_id'] ) ){
$found = true;
break;
}
}
return $found;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
The custom conditional function is_in_cart( $ids ) accept a string (a unique product Id) or an array of product Ids.
Your revisited usage (ACF get_field may need a post ID (product ID)):
if ( is_in_cart( "69286" ) ) {
echo '<p>' . get_field ( 'cart_field' ) . '</p>'; // or get_field ( 'cart_field', "69286" )
}
if ( is_in_cart( "69287" ) ) {
echo '<p>' . get_field ( 'cart_field-1' ) . '</p>'; // or get_field ( 'cart_field', "69287" )
}

Looping through ACF in custom post types using functions in php to display the values in page. foreach quits working when placed in the function

I created this code to display the location from ACF that matches part of the URL and it works as expected.
<?php
$myurl= htmlspecialchars($_SERVER["REQUEST_URI"]);
$myexplodes = ( explode ('/', $myurl) );
$posts = get_posts(array(
'post_type' => 'my_vars',
));
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
?>
But when I try to place this code into a function nothing is displayed when I call it.
function local (){
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
}
I suspected that it is a scope problem with the vars but I have tried to make the vars global but had no luck.
The first one probably works because $post is a WordPress global variable, so I advise to use another variable name in your foreach.
For the thing that you want to do you should use also the post id in the get_field function call:
$value = get_field( "location", $article->ID );

Get the date an order was paid on Woocommerce

How can I get, on Woocommerce, the date an order had its status changed to paid/complete?
I saw something about getting the orders from a costumer, but this would be just the first step of my algorithm. Then I would need to know when it changed to complete.
The idea is to make a membership area: a payment lasts 3 months. So I will count the days passed since it was bought
Something related
https://www.skyverge.com/blog/get-all-woocommerce-orders-for-a-customer/
And this is what I use to know if a product was bought by the costumer
if (wc_customer_bought_product($customer_email, $user_id,$loop->post->ID)){
$courses[] = $this->find($loop->post->ID);
}
I think you should look into:
/mySite/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-order.php
There is a __get function:
public function __get( $key ) {
// Get values or default if not set.
if ( 'completed_date' === $key ) {
$value = ( $value = get_post_meta( $this->id, '_completed_date', true ) ) ? $value : $this->modified_date;
} elseif ( 'user_id' === $key ) {
$value = ( $value = get_post_meta( $this->id, '_customer_user', true ) ) ? absint( $value ) : '';
} elseif ( 'status' === $key ) {
$value = $this->get_status();
} else {
$value = get_post_meta( $this->id, '_' . $key, true );
}
return $value;
}
So my understanding is that if you pass 'completed date' as the argument then it will return the completed_date.
It also gives you a hint where this date is i.e.
get_post_meta
Well, at least that's where I would start.
Probably the "most correct" way is:
$order = new WC_Order($order_id);
$date_obj = $order->get_date_paid();
echo $date_obj->date('d/m/Y');
This should to the job on recente WP/WooCommerce.
$order = new WC_Order($sale_id);
echo $order->get_date_paid();

WordPress Plugin Footer Menu Error

I am getting an out of the box error for a plugin and I am wondering if there is a quick fix.
I have googled the issue and checked the creator of the plugin's website. No help.
The plugin needs:
<?php do_shortcode('[print_wp_footer]'); ?>
Added to Footer.php for the theme.
WordPress then gives an error of:
Warning: Invalid argument supplied for foreach() in /.../wp-content/plugins/wp-footer-menu/main.php on line 192
Here is the line by itself
foreach ($values as $attr=>$val) {
Here is the section of code with line "192" with arrows.
<?php
}
function wp_footer_menu_confirm_delete ($delete) {
$base_uri = wp_footer_menu_base_uri();
// Find out about this menu item.
$menu_items = get_option ( 'footer_menu_links' );
$item_to_delete = explode( ',', $menu_items[$delete] );
$item_title = $item_to_delete[0];
$item_address = $item_to_delete[1];
// Create form for user to confirm option.
echo '<h3>Confirm Delete</h3>';
echo '<p>Are you sure you want to delete this menu item?</p>';
echo '<p>Title: ' . $item_title . '</p>' ;
echo '<p>Address: ' . $item_address . '</p>';
echo '<form method="post" action="' . $base_uri . '">';
echo '<input type="hidden" name="delete_key" value="' . $delete . '" />';
echo wp_nonce_field( 'confirm_delete', 'delete' );
echo '<input type="submit" class="button-primary" value="Delete item" />';
echo '</form>';
}
function wp_footer_menu_process() {
if ( isset( $_GET[ 'delete' ] ) ) {
$nonce = $_GET ['nonce'];
if ( wp_verify_nonce( $nonce, 'footer_delete' ) ) {
wp_footer_menu_confirm_delete ( $_GET[ 'delete' ] );
}
return 0;
} else if ( isset( $_POST[ 'delete_key' ] ) && check_admin_referer ( 'confirm_delete', 'delete' ) ) {
$menu_items = get_option ( 'footer_menu_links' );
$key = $_POST['delete_key'];
unset ( $menu_items[$key] );
update_option ( 'footer_menu_links', $menu_items );
}
if ( isset( $_POST[ 'link_title' ] ) && check_admin_referer( 'footer_menu', 'add' ) ) {
$link_title = $_POST ['link_title'];
$link_address = $_POST ['link_address'];
$link_order = $_POST ['link_order'];
$new_link = $link_title . ',' . $link_address . ',' . $link_order;
$footer_links = get_option( 'footer_menu_links' );
if ($footer_links == '') {
$footer_links = array();
}
$new_links = array_push( $footer_links, $new_link );
update_option ( 'footer_menu_links', $footer_links );
}
if ( isset( $_POST[ 'font-size' ] ) && check_admin_referer( 'footer_menu', 'save' ) ) {
if (empty($_POST['auto-footer'])) {
$_POST['auto-footer'] = 'no';
}
if (empty($_POST['auto-sticky'])) {
$_POST['auto-sticky'] = 'no';
}
update_option('wp_footer_values', $_POST);
echo '<div class="wp_footer_info" style="margin:0 auto;margin-top:5px;text-align:center;">Customizations Saved</div>';
}
return 1;
}
function wp_footer_print_menu() {
$menu = wp_footer_get_menu();
$values = get_option('wp_footer_values');
--192--> foreach ($values as $attr=>$val) {
$menu = str_replace('%' . $attr . '%', stripslashes($val), $menu);
}
echo $menu;
if ($values['auto-sticky'] == 'yes') {
?>
<style type="text/css">
.wp_footer_sticky {
position:fixed;
bottom: 0;
width: 100%;
}
</style>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#wp_footer_menu').addClass('wp_footer_sticky');
});
</script>
<?php
It seems to depend to returned value of the following code :
$values = get_option('wp_footer_values');
Depending on the WordPress codex, if the option does not exist it return a boolean FALSE. To you will need to verify if that the $values variable is not empty. Check with the following code.
function wp_footer_print_menu() {
$menu = wp_footer_get_menu();
$values = get_option('wp_footer_values');
if (!empty($values)) { // <-- verify it's not empty
foreach ($values as $attr=>$val) {
$menu = str_replace('%' . $attr . '%', stripslashes($val), $menu);
}
echo $menu;
// [...}
} // <-- don't forget to close the if statement just after the end of the foreach statement
Hope that help.
Source : get_option()

Categories