Open cdesp opened 9 months ago
@cdesp you'll likely want to review the following comments to see how to extend more deeply for Woo integrations: https://github.com/10up/distributor/issues/1153#issuecomment-1814518156, https://github.com/10up/distributor/issues/1142#issuecomment-1814768713, https://github.com/10up/distributor/issues/1027#issuecomment-1499085292.
I already saw these but they are referring to a variable product not a simple one. Should i hook to product update and then sent a command to update the remote product or this is done automatically whenever the product stock updates after an order is placed?
I assumed that distributor only pushes when something is changed via user interface so i managed to manually push a product using the following code. I changed a function residing on subscriptions.php to fit my needs.
add_action( 'woocommerce_product_set_stock', 'stock_changed' );
add_action( 'woocommerce_variation_set_stock', 'stock_changed' );
function stock_changed( $product ) {
// Do something
// $product->get_id(); //postid
notify_product($product->get_id());
}
/**
* Send notifications on post update to each subscription for that post
*
* @param int|WP_Post $post Post ID or WP_Post, depending on which action the method is hooked to.
* @since 1.0
*/
function notify_product( $post_id ) {
$subscriptions = get_post_meta( $post_id, 'dt_subscriptions', true );
if ( empty( $subscriptions ) ) {
return;
}
$post = get_post( $post_id );
$update_subscriptions = false;
foreach ( $subscriptions as $subscription_key => $subscription_id ) {
$signature = get_post_meta( $subscription_id, 'dt_subscription_signature', true );
$remote_post_id = get_post_meta( $subscription_id, 'dt_subscription_remote_post_id', true );
$target_url = get_post_meta( $subscription_id, 'dt_subscription_target_url', true );
if ( empty( $signature ) || empty( $remote_post_id ) || empty( $target_url ) ) {
continue;
}
$post_body = [
'post_id' => $remote_post_id,
'signature' => $signature,
'post_data' => [
'title' => html_entity_decode( get_the_title( $post_id ), ENT_QUOTES, get_bloginfo( 'charset' ) ),
'slug' => $post->post_name,
'post_type' => $post->post_type,
'content' => '',//\Distributor\Utils\get_processed_content( $post->post_content ),
'excerpt' => $post->post_excerpt,
'distributor_media' => \Distributor\Utils\prepare_media( $post_id ),
'distributor_terms' => \Distributor\Utils\prepare_taxonomy_terms( $post_id, array( 'show_in_rest' => true ) ),
'distributor_meta' => \Distributor\Utils\prepare_meta( $post_id ),
],
];
if ( \Distributor\Utils\is_using_gutenberg( $post ) ) {
if ( \Distributor\Utils\dt_use_block_editor_for_post_type( $post->post_type ) ) {
$post_body['post_data']['distributor_raw_content'] = $post->post_content;
}
}
/**
* Filter the timeout used when calling `\Distributor\Subscriptions\send_notifications`
*
* @hook dt_subscription_post_timeout
*
* @param {int} $timeout The timeout to use for the remote post. Default `5`.
* @param {WP_Post} $post The post object
*
* @return {int} The timeout to use for the remote post.
*/
$request_timeout = apply_filters( 'dt_subscription_post_timeout', 5, $post );
/**
* Filter the arguments sent to the remote server during a subscription update.
*
* @since 1.3.0
* @hook dt_subscription_post_args
*
* @param {array} $post_body The request body to send.
* @param {WP_Post} $post The WP_Post that is being pushed.
*
* @return {array} The request body to send.
*/
$post_body = apply_filters( 'dt_subscription_post_args', $post_body, $post );
$post_arguments = [
'timeout' => $request_timeout,
'body' => wp_json_encode( $post_body ),
'headers' => [
'Content-Type' => 'application/json',
'X-Distributor-Version' => DT_VERSION,
],
];
$request = wp_remote_post(
untrailingslashit( $target_url ) . '/wp/v2/dt_subscription/receive',
$post_arguments
);
if ( ! is_wp_error( $request ) ) {
$response_code = wp_remote_retrieve_response_code( $request );
$headers = wp_remote_retrieve_headers( $request );
if ( 404 === $response_code && ! empty( $headers['X-Distributor-Post-Deleted'] ) ) {
/**
* Post on receiving end has been deleted.
*/
unset( $subscriptions[ $subscription_key ] );
$update_subscriptions = true;
wp_delete_post( $subscription_id, true );
}
}
}
if ( $update_subscriptions ) {
update_post_meta( $post_id, 'dt_subscriptions', $subscriptions );
}
}
Describe the bug
If a product is sold on the master site then the remaining stock after the sale doesn't get automatically propagated to the other site. If i edit the product myself like changing the description or whatever eerything works ok. Is this how it is supposed to work? and if so how i can trigger the push myself every time a sale is done?
Steps to Reproduce
If a product is sold the new stock is not pushed to the other site
Screenshots, screen recording, code snippet
No response
Environment information
No response
WordPress information
No response
Code of Conduct