For example user from WooCommerce trigers woocommerce_add_to_cart hook is it possible to send product data to Shopify app as json and that data be accepted via Shopify app as carts/create webhook? If so how could I do this?
UPDATE:
Finally found a way of doing that. Here is and example code, for adding order:
$ch = curl_init("https://api_key:api_pass#shop_name.myshopify.com/admin/orders.json");
$order = array('order' => array(
'line_items' => array(
array(
'title' => 'Big Brown Bear Boots',
'price' => '74.99',
'grams' => '1300',
'quantity' => 3,
),
array(
'title' => 'Clicky Keyboard',
'price' => '150',
'quantity' => 1,
)
)
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($order));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$this->response = curl_exec($ch);
And you hook this code to woocommerce action.
Related
I am trying to use this code on "Wp All Import". If there is a product name in the database, that product should be omitted, but the code will not work as is. What do I need to do for the code to work?
add_filter('wp_all_import_is_post_to_create', 'create_only_if_unique_custom_field', 10, 3);
function create_only_if_unique_custom_field( $continue_import, $data, $import_id ) {
// Only run for import ID 1.
if ( $import_id == 33 || $import_id == 34 ) {
// The custom field to check.
$key_to_look_up = "post_title";
// The value to check where 'num' is the element name.
$value_to_look_up = $data['name'];
// Prepare the WP_Query arguments
$args = array (
// Set the post type being imported.
'post_type' => array( 'post' ),
// Check our custom field for our value.
'meta_query' => array(array(
'key' => $key_to_look_up,
'value' => $value_to_look_up,
)),
);
// Run the query and do not create post if custom field value is duplicated.
$query = new WP_Query( $args );
return !($query->have_posts());
} else {
// Take no action if a different import ID is running.
return $continue_import;
}
}
You can do like this.
<?php
$params = array('posts_per_page' => 5);
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) :
$wc_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata();?>
<?php else: ?>
<p>
<?php _e( 'No Products' ); ?>
</p>
<?php endif; ?>
There are some ways to show the different types of Woocommerce product names with WP_Query.
<?php
//Pulling WooCommerce Products instead of WordPress Posts, Use this param
$params = array(
'posts_per_page' => 5,
'post_type' => 'product'
);
//Displaying products of a given price range, use this param
$params = array(
'posts_per_page' => 100,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_price',
'value' => 5,
'compare' => '<=',
'type' => 'NUMERIC'
),
array(
'key' => '_sales_price',
'value' => 5,
'compare' => '<=',
'type' => 'NUMERIC'
)
)
);
//Displaying available products only, use this param
$params = array(
'posts_per_page' => 5,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_price',
'value' => 5,
'compare' => '<',
'type' => 'NUMERIC'
),
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
);
$wc_query = new WP_Query($params);
if ($wc_query->have_posts()) :
while ($wc_query->have_posts()) :
$wc_query->the_post();
the_title();
endwhile;
endif;
Also will help you the article https://www.gavick.com/blog/wp_query-woocommerce-products
Thank you
This variable controls the title.
// Xml file column name
$value = = $data['product_title'];
// Get wpdb product title
$posts = get_posts([
'post_type' => 'product',
'title' => $value,
]);
I want to update my post by JSON link, I have got all post data by this link.
http://xyz/wp-json/custom/v1/all-posts.
how can I setup cron jobs for auto-update.
$slices = json_decode(file_get_contents('http://27.109.19.234/decoraidnew/wp-json/custom/v1/all-posts'),true); if ($slices) { foreach ($slices as $slice) {
$title = $slice[1];
} } $my_post = array(
'post_title' => $title,
'post_content' => 'This is my content',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39) ); $post_id = wp_insert_post( $my_post, $wp_error );
I'm encoding in JSON a bunch of data from a WP Query:
$args = array(
'posts_per_page' => 20,
'post_type' => 'post',
'category' => 6,
'meta_key' => 'custom_total_hits',
'tag' => 'indie-pop',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
'after' => date('Y-m-d', strtotime('-40 days'))
)
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
foreach( $posts as $post ) {
$output[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'count' => $post->custom_total_hits,
'soundcloud_url' => $post->soundcloud_song,
'soundcloud_id' => $post->soundcloud_ids,
'link' => get_permalink($post),
);
}
echo json_encode($output);
I would like to display in my JSON a key corrisponding to the src of the medium size of the attached image. If I use 'images' => get_attached_media('image', $post->ID) it retrives an array of multiple data which I can not access since I don't know the ID of the attached image when I process the data of my JSON. How can I do to retrieve a first level key - value where the value is the src of the attached image?
get_post_thumbnail_id : Get post thumbnail ID
wp_get_attachment_url : Get attachment URL by attachment id
'images' => parse_url( wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ) );
Would you please try above code?
Try this solution:
$images = array();
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
foreach (get_intermediate_image_sizes() as $size) {
$images[$size] = wp_get_attachment_image_src($post_thumbnail_id, $size);
}
//end
'images' => $images // type_of_size => image_url
How to use WordPress REST API to post article? I can edit post using the following PHP code, i'm using the Basic Authentication.
require( dirname(__FILE__) . '/wp-load.php' );
$headers = array (
'Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 'password' ),
);
// Edit post
$url = rest_url( 'wp/v2/posts/1' );
$data = array(
'title' => 'Test API Edit'
);
$response = wp_remote_post( $url, array (
'method' => 'POST',
'headers' => $headers,
'body' => $data
) );
print_r($response);
But I can not post new article
<?php
require( dirname(__FILE__) . '/wp-load.php' );
$headers = array (
'Authorization' => 'Basic ' . base64_encode( 'username' . ':' . 'password' ),
);
// post new article
$url = rest_url( 'wp/v2/create_post/' );
$data = array(
'title' => 'Post from API',
'content' => 'test contents'
);
$response = wp_remote_post( $url, array (
'method' => 'POST',
'headers' => $headers,
'body' => $data
) );
print_r($response);
The return is always No route was found matching the URL and request method
Any suggestions?
It looks like you are using the wrong endpoint for POSTing to the REST API. As mentioned here the endpoint for creating new posts is:
/wp/v2/posts
Hope this solves your problem!
I have a Wordpress site with 28 users and would like to get the latest post from each user (just one post per user). I've tried with a custom sql using both DISTINCT and GROUP BY, but with no success.
This is the closest I have come. It fetches unique posts, but the oldest post instead of the newest.
function last_post_per_user() {
global $wpdb;
return $wpdb->get_results('SELECT ID FROM '.$wpdb->posts.' WHERE post_status=\'publish\' AND post_type=\'post\' AND post_author!=\'1\' GROUP BY post_author');
}
$posts = last_post_per_user();
foreach ($posts as $post) {
$post_id[] = $post->ID;
}
query_posts( array(
'post_type' => 'post',
'posts_per_page' => 10,
'post__in' => $post_id
) );
Does anyone have any suggestions on how to solve this?
(Have actually tried solving this the whole f*cking day)
Try replacing this:
query_posts( array(
'post_type' => 'post',
'posts_per_page' => 10,
'post__in' => $post_id
) );
with this:
query_posts( array(
'post_type' => 'post',
'posts_per_page' => 10,
'post__in' => $post_id,
'orderby' => 'date',
'order' => 'DESC'
) );
It will show the newest posts.