Insert Advanced Custom Fields Rule to post_meta table - wordpress

I'm inserting custom fields to postmeta table if the post title is equal to "Components" or "Landing page".
Inside my add_post_meta() function I'm getting the serialized data with extra string at the beginning with double quote and at the end double quote with a semicolon.
Expected a:5:{s:5:"param";s:4:"post";s:8:"operator";s:2:"==";s:5:"value";i:3309;s:8:"order_no";i:0;s:8:"group_no";i:1;}
Currently inserting s:110:"a:5:{s:5:"param";s:4:"post";s:8:"operator";s:2:"==";s:5:"value";i:3309;s:8:"order_no";i:0;s:8:"group_no";i:1;}";
Here is my code:
if ($menupost->post_title == "Components" || $menupost->post_title == "Landing Page") {
global $wpdb;
$posttitle = 'Tablet';
$postid_ofacf = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "' and post_type='acf'");
$post_meta = get_post_meta($postid_ofacf, "rule", false);
$post_count = count($post_meta);
$last_array = $post_meta[$post_count - 1];
$insert_acf_rule_meta = array();
foreach ($last_array as $key => $value) {
if ($key == "group_no") {
$value = $value + 1;
}
if ($key == "value") {
$value = $post_id;
}
$insert_acf_rule_meta[$key] = $value;
}
add_post_meta($postid_ofacf, 'rule', serialize($insert_acf_rule_meta));
$get_acf_post_args = array(
'post_title' => 'Tablet',
'post_status' => 'publish',
'post_type' => 'acf'
);
}
How to insert a proper serialized sting as expected.
Like I'm inserting rule of "Tablet" Custom field to the postmeta table for the posts with title "Components" OR "Landing page", I need to insert some more custom fields(Desktop,Mobile) rules to the postmeta table.
Any help may greatly appreciated, thanks in advance.

There is no need to pass the serialized array as a parameter to the add_post_meta function, as it calls serialize function in case an array is given, as stated in the documentation.
Rewrite the line:
add_post_meta($postid_ofacf, 'rule', serialize($insert_acf_rule_meta));
To:
add_post_meta($postid_ofacf, 'rule', $insert_acf_rule_meta);
Hope this helps.

Related

Attribute values for product variations

I'm facing a big problem with product variations and their attributes in woocommerce. I'm trying to display a table with each attribute for each availabe product variation. But Woocommerce saves the attributes in post meta complete in lowercase, replaces slashes and german special characters like ü,ö,ä etc. I get the attributes with $variation->get_variation_attributes().
I've searched the database for the save values you can see for example in the dropdown in the admin panel, but they are saved like this without a link to the variation they are assigned to:
a:5:{s:10:"bestell-nr";a:6:{s:4:"name";s:11:"Bestell-Nr.";s:5:"value";s:9:"1 | 2 | and so on...
How can I get the attributes in their correct format to display?
Actually product attributes are actually terms in custom taxonomies, so you just need to get the terms in that particular taxonomy. All attribute taxonomies are prefaced with 'pa_'. So a size attribute would be a 'pa_size' taxonomy. And the variation ID is the post ID for a variation.
But depending on how you want to display it, WooCommerce has a built-in function for displaying all a variation's attributes:
The following will display a definition list of all of a variations attributes.
echo wc_get_formatted_variation( $product->get_variation_attributes() );
And passing a second parameter of true will display a flat list:
echo wc_get_formatted_variation( $product->get_variation_attributes(), true );
This seems to work for me. Hope this helps.
$post = get_post();
$id = $post->ID;
$product_variations = new WC_Product_Variable( $id );
$product_variations = $product_variations->get_available_variations();
print_r($product_variations);
This can be found in the class-wc-product-variable.php
So basically if you look around on that page you can find a bunch of useful functions.
Here is something i put together.
$product_children = $product_variations->get_children();
$child_variations = array();
foreach ($product_children as $child){
$child_variations[] = $product_variations->get_available_variation($child);
}
print_r($child_variations);
I only wanted to publish one of the variation attributes rather than all of them; contributing this code in case it's helpful to anyone else.
get_variation_attributes() gets all the attributes
wc_get_formatted_variation() returns a formatted version of the array it's handed
$attributes = $productVariation->get_variation_attributes() ;
if ( $attributes [ 'attribute_pa_colour' ] ) {
$colour = [ 'attribute_pa_colour' => $attributes [ 'attribute_pa_colour'] ];
echo wc_get_formatted_variation ( $colour );
}
The way I do it is by using "get_post_meta":
echo get_post_meta( $variation_id, 'attribute_name_field', true);
Hopes this helps someone.
I used wp_get_post_terms to get correct variance attributes.
global $product;
$variations = $product->get_available_variations();
$var = [];
foreach ($variations as $variation) {
$var[] = $variation['attributes'];
}
var_dump($var);
//xxx to get attribute values with correct lower-upper-mixed-case
foreach ($var as $key => $arr) {
foreach ($arr as $orig_code => $lowercase_value) {
$terms_arr = wp_get_post_terms( $product->id, str_replace('attribute_','',$orig_code), array( 'fields' => 'names' ) );
foreach ($terms_arr as $term) {
if (strtolower($term) == $lowercase_value) {
$var[$key][$orig_code] = $term;
break;
}
}
}
}
var_dump($var);
The results:
Before hard code
array (size=1)
0 =>
array (size=2)
'attribute_pa_width' => string 'none' (length=4)
'attribute_pa_code' => string 'valancese' (length=9)
After hard code:
array (size=1)
0 =>
array (size=2)
'attribute_pa_width' => string 'None' (length=4)
'attribute_pa_code' => string 'ValanceSe' (length=9)
Just went through this ...here is my solution. It handles multiple attributes and all the variations. You just have to know the attribute slug.
$items = $order->get_items();
foreach( $items as $item_id => $product )
{
$ProductName = $product->get_name(); /
if($product->is_type( 'simple' ))
$CoreProductName = $ProductName; // No variance
else
{
list($CoreProductName, $ThrowAway) = explode(" - ",$ProductName, 2);
$variation_id = $product['variation_id'];
$variation = new WC_Product_Variation($variation_id);
$attributes = $variation->get_variation_attributes();
$SelectedAttribute1 = $attributes['attribute_YOUR_SLUG1_PER_PRODUCT'];
$SelectedAttribute2 = $attributes['attribute_YOUR_SLUG2_PER_PRODUCT'];
}
}

How do you query Pods CMS using relationship field?

I have 2 custom content types created by Pods CMS: fights and events. In the fights content type there is a relationship field for the event. How would I select all fights from the database by a specific event id? I tried querying the pods relationships table manually but that gave me incorrect results.
$fights = pods( 'fights' );
$params = array(
'where' => 'event.id = 3'
);
$fights->find( $params );
// loop through a while( $fights->fetch() ) and use $fights->field() to get the values of each field
That should do it, but you'll want to look at the 'find' documentation for your specific content type case as event.id may not be what you want (you may want event.ID).
http://pods.io/docs/code/pods/
http://pods.io/docs/code/pods/find/
http://pods.io/docs/code/pods/field/
http://pods.io/docs/code/pods/display/
Using this function you can easily display all individual relational fields with shortcode:
function get_pod_fields($atts) {
$a = shortcode_atts( array('field' => '', 'pod'=> '', 'relation'=> '', 'type'=>'', 'as'=>'url'), $atts );
$pod = pods( $a['pod'], get_the_id() );
$related = $pod->field( $a['relation']);
$field = get_post_meta( $related['ID'], $a['field'], true );
if($a['type'] == "image") {
if($a['as'] == "image") {
$field = '<img src="'.$field = $field['guid'].'"></img>';
} else {
$field = $field['guid'];
}
} else {
$field = $field;
}
return $field;
}
add_shortcode( 'podfields', 'get_pod_fields' );
[podfields field="profile_picture" type="image" pod="therapy" as="image" relation="therapist"]

Add Delivery Date from the Product Custom Option to Sales Order Grid in magento

I have added Delivery Date as a Custom Option on the Product. I want the Delivery Date to be displayed in the Sales Order Grid in admin.
I have created my local copy of Namespace_Module_Block_Adminhtml_Sales_Order_Grid.
Here in the _prepareCollection() function I am able to get the Product Options:
$collection = Mage::getResourceModel($this->_getCollectionClass())
->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
**'proptions' => new Zend_Db_Expr('group_concat(`sales/order_item`.product_options SEPARATOR ",")'),**
)
);
I then add the column as:
$this->addColumn('proptions', array(
'header' => Mage::helper('Sales')->__('Product Options'),
'width' => '100px',
'index' => 'proptions',
'renderer' => new Namespace_Module_Block_Adminhtml_Renderer_Data(),
));
Now in Namespace_Module_Block_Adminhtml_Renderer_Data() I have a method:
public function _getValue(Varien_Object $row)
{
$val = $row->getData($this->getColumn()->getIndex()); // row value
$array = unserialize($val);
//loop thru the $array and create a format string
//
$options = $array['options'];
$format_val = '';
foreach ($options as $key=> $value) {
$format_val = $format_val . $key . "=>" . $value . " , ";
}
return $format_val;
}
The display is not right. I don't think I'm looping through the array correctly. What am I doing wrong here?
If you want to do it in direct sql quesries open this file., ->app->design->adminhtml->default->default->template-> your template ->info.phtml and add this code
<div class="entry-edit">
<div class="entry-edit-head">
<h4 class="icon-head head-products"><?php echo Mage::helper('sales')->__('Delivery Time Information') ?></h4>
</div>
</div>
<div class="grid np">
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
//$write = Mage::getSingleton('core/resource')->getConnection('core_write');//for writing to database
$sql = "SELECT * FROM tablename";
$results = $connection->fetchAll($sql);
foreach($results as $result) {
echo $result['column_name']."<br/>";
}
</div></div></div>
Resolved by updating MySQL:
SET SESSION group_concat_max_len = 1000000;
This can be set on the Global level as well.
Thanks,
Neet

Hacking Wordpress Plugin "Google Calendar Events"

I'm currently working with the plugin Google Calendar Events. I'd like to add the ability for users to choose (via a select drop-down menu) which feed they're viewing and then have the calendar automatically display the selected feed. Does anyone know how to do this?
I know that you can use the shortcode to choose what feeds to display, but I'd rather not create an individual page for each feed, and then have links to them (there are 10+ feeds and one is added and deleted with regularity, so it's just not feasible to manually create a calendar for each individual feed).
I've added this code to the gce-script.js file in the plugin:
function changeFeeds(){
var values = jQuery("#gce-view-feed").val();
jQuery("#result").replaceWith(values);
}
jQuery("option").click(function(){
location.reload();
});
jQuery("select").change(changeFeeds);
changeFeeds();
...Where the id "gce-view-feed" is the select menu's id and id "result" is where I display the results. This works great, except how do I then pass that result to the plugin so that it uses it?
Feed ids are defined by a PHP variable $feed_ids. I think the best place to edit the variable is this line of code in the google-calendar-events.php file:
//Check that any feeds have been added
if ( is_array( $options ) && ! empty( $options ) ) {
extract( shortcode_atts( array(
'id' => '',
'type' => 'grid',
'title' => null,
'max' => 0,
'order' => 'asc'
), $atts ) );
$no_feeds_exist = true;
$feed_ids = array();
if ( '' != $id ) {
//Break comma delimited list of feed ids into array
$feed_ids = explode( ',', str_replace( ' ', '', $id ) );
//Check each id is an integer, if not, remove it from the array
foreach ( $feed_ids as $key => $feed_id ) {
if ( 0 == absint( $feed_id ) )
unset( $feed_ids[$key] );
}
//If at least one of the feed ids entered exists, set no_feeds_exist to false
foreach ( $feed_ids as $feed_id ) {
if ( isset($options[$feed_id] ) )
$no_feeds_exist = false;
}
} else {
foreach ( $options as $feed ) {
$feed_ids[] = $feed['id'];
}
$no_feeds_exist = false;
}
But I don't know how to pass the jQuery value to the $feed_ids variable. Is there a way to do this? And if not, is there another way to dynamically select an option and then pass it to a PHP variable?

Wordpress how to prevent duplicate post by checking if post title exist before running "wp_insert_post"?

I have a wordpress site that connects to a soap server. The problem is every time I run the script the wp_insert_post is using the same result again.
I would like to check if existing post_title matches the value from $title then if they match, prevent wp_insert_post from using the same value again.
Here's the code:
try {
$client = new SoapClient($wsdl, array('login' => $username, 'password' => $password));
} catch(Exception $e) {
die('Couldn\'t establish connection to weblink service.');
}
$publications = $client->GetPublicationSummaries();
foreach ($publications->GetPublicationSummariesResult->PublicationSummaries->PublicationSummary as $publication_summary) {
// get the complete publication from the webservice
$publication = $client->getPublication(array('PublicationId' => $publication_summary->ID))->GetPublicationResult->Publication;
// get all properties and put them in an array
$properties = array();
foreach ($publication->Property as $attribute => $value) {
$properties[$attribute] = $value;
}
// Assemble basic title from properties
$title = $properties['Address']->Street . ' ' . $properties['Address']->HouseNumber . $properties['Address']->HouseNumberExtension . ', ' . $properties['Address']->City->_;
}
$my_post = array(
'post_title'=>$title,
'post_content'=>'my contents',
'post_status'=>'draft',
'post_type'=>'skarabeepublication',
'post_author'=>1,
);
wp_insert_post($my_post);
Thank you for any help.
You can use get_page_by_title() as it supports custom post types now.
if (!get_page_by_title($title, OBJECT, 'skarabeepublication')) :
$my_post = array(
'post_title'=>$title,
'post_content'=>'my contents',
'post_status'=>'draft',
'post_type'=>'skarabeepublication',
'post_author'=>1,
);
wp_insert_post($my_post);
endif;
Codex information here
Surprised not to see mention of post_exists function in wp-includes/post.php. See entry on wpseek. There is no entry in the codex. At it's simplest it works like get_page_by_title but returns a post id (or 0 if not found) instead of the object (or null).
$post_id = post_exists( $my_title );
if (!$post_id) {
// code here
}
Sorry for the late response. I used what Robot says in the comment and this solved my problem. Thanks
$post_if = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_title like '$title_from_soap'");
if($post_if < 1){
//code here
}
sampler:
if( !get_page_by_path('mypageslug',OBJECT,'post') ){
//your codes
}

Resources