从WooCommerce接收产品更新的挂钩时,有效负载包含一个“变体”数组,但是,该数组仅包含属于更新产品的变体的ID。
如何发送实际的变化对象以及产品更新的有效负载,而不仅仅是变化的ID(通过这种方式,我不需要向REST API的变化资源发送另一个请求来获取它们)。
谢谢!
答案 0 :(得分:1)
您需要挂钩woocommerce_webhook_payload
来构建有效负载。产品差异的详细信息存储在variations_objs
中。
// Hook to the webhook build process and add your variations objects.
add_filter( 'woocommerce_webhook_payload', 'dolly_woocommerce_webhook_payload', 10, 4 );
function dolly_woocommerce_webhook_payload( $payload, $resource, $resource_id, $id ) {
// Remove the filter to eliminate the recursion calls.
remove_filter( 'woocommerce_webhook_payload', 'dolly_woocommerce_webhook_payload', 10 );
// Create a WC_Webhook class with the webhook id.
$wc_webhook = new WC_Webhook( $id );
// Bail early if the resource is not product.
if ( 'product' !== $resource ) {
return $payload;
}
// Bail early if the product type is not variable.
$product = new WC_Product( $resource_id );
if ( 'variable' === $product->get_type() ) {
return $payload;
}
// Build the payload of each product variation.
$variations = $payload['variations'];
foreach( $variations as $variation ) {
$variations_objs[] = $wc_webhook->build_payload( $variation );
}
// Add the varitions to the payload.
$payload['variations_objs'] = $variations_objs;
// Add the filter again and return the payload.
add_filter( 'woocommerce_webhook_payload', 'dolly_woocommerce_webhook_payload', 10, 4 );
return $payload;
}
这是网络钩子发送的数据。
{
"id" : 94,
"name" : "Nepali Shirt",
"slug" : "nepali-shirt",
"permalink" : "http://online-users.test/product/nepali-shirt/",
"date_created" : "2019-07-14T05:12:52",
"date_created_gmt" : "2019-07-14T05:12:52",
"date_modified" : "2019-07-18T07:52:36",
"date_modified_gmt" : "2019-07-18T07:52:36",
"type" : "variable",
"status" : "publish",
"featured" : false,
"catalog_visibility" : "visible",
"description" : "<p>hello tamang hhh jjjjj sfsfsd hllk ljlkjkl jljk ljlkjkl kjlkjlk jlkj dgdfg jkl ljlk sdfdsf sfsd sfdds</p>\n",
"short_description" : "",
"sku" : "",
"price" : "205",
"regular_price" : "",
"sale_price" : "",
"date_on_sale_from" : null,
"date_on_sale_from_gmt": null,
"date_on_sale_to" : null,
"date_on_sale_to_gmt" : null,
"price_html" : "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">£</span>205.00</span> – <span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">£</span>500.00</span>",
"on_sale" : false,
"purchasable" : true,
"total_sales" : 0,
"virtual" : false,
"downloadable" : false,
"downloads" : [],
"download_limit" : -1,
"download_expiry" : -1,
"external_url" : "",
"button_text" : "",
"tax_status" : "taxable",
"tax_class" : "",
"manage_stock" : false,
"stock_quantity" : null,
"in_stock" : true,
"backorders" : "no",
"backorders_allowed" : false,
"backordered" : false,
"sold_individually" : false,
"weight" : "",
"dimensions" : {
"length": "",
"width" : "",
"height": ""
},
"shipping_required": true,
"shipping_taxable" : true,
"shipping_class" : "",
"shipping_class_id": 0,
"reviews_allowed" : true,
"average_rating" : "0.00",
"rating_count" : 0,
"related_ids" : [
82,
80
],
"upsell_ids" : [],
"cross_sell_ids": [],
"parent_id" : 0,
"purchase_note" : "",
"categories" : [
{
"id" : 15,
"name": "Uncategorized",
"slug": "uncategorized"
}
],
"tags" : [],
"images": [
{
"id" : 0,
"date_created" : "2019-07-18T07:53:30",
"date_created_gmt" : "2019-07-18T07:53:30",
"date_modified" : "2019-07-18T07:53:30",
"date_modified_gmt": "2019-07-18T07:53:30",
"src" : "http://online-users.test/wp-content/uploads/woocommerce-placeholder-324x324.png",
"name" : "Placeholder",
"alt" : "Placeholder",
"position" : 0
}
],
"attributes": [
{
"id" : 1,
"name" : "Color",
"position" : 0,
"visible" : true,
"variation": true,
"options" : [
"Blue",
"Gray",
"Red"
]
}
],
"default_attributes": [
{
"id" : 1,
"name" : "Color",
"option": "blue"
}
],
"variations": [
96,
97,
98
],
"grouped_products": [],
"menu_order" : 0,
"meta_data" : [
{
"id" : 1103,
"key" : "pageview",
"value": "1"
}
],
"store": {
"id" : 1,
"name" : "admin",
"shop_name": "WordPress Biratnagar",
"url" : "http://online-users.test/store/admin/",
"address" : {
"street_1": "Haatkhola",
"street_2": "",
"city" : "Biratnagar",
"zip" : "977",
"country" : "NP",
"state" : "BAG"
}
},
"variations_objs": [
{
"id" : 96,
"name" : "Nepali Shirt - Blue",
"slug" : "nepali-shirt-blue",
"permalink" : "http://online-users.test/product/nepali-shirt/?attribute_pa_color=blue",
"date_created" : "2019-07-14T05:12:12",
"date_created_gmt" : "2019-07-14T05:12:12",
"date_modified" : "2019-07-18T06:52:07",
"date_modified_gmt" : "2019-07-18T06:52:07",
"type" : "variation",
"status" : "publish",
"featured" : false,
"catalog_visibility" : "visible",
"description" : "",
"short_description" : "",
"sku" : "",
"price" : "205",
"regular_price" : "205",
"sale_price" : "",
"date_on_sale_from" : null,
"date_on_sale_from_gmt": null,
"date_on_sale_to" : null,
"date_on_sale_to_gmt" : null,
"price_html" : "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">£</span>205.00</span>",
"on_sale" : false,
"purchasable" : true,
"total_sales" : "0",
"virtual" : false,
"downloadable" : false,
"downloads" : [],
"download_limit" : -1,
"download_expiry" : -1,
"external_url" : "",
"button_text" : "",
"tax_status" : "taxable",
"tax_class" : "",
"manage_stock" : false,
"stock_quantity" : null,
"in_stock" : true,
"backorders" : "no",
"backorders_allowed" : false,
"backordered" : false,
"sold_individually" : false,
"weight" : "",
"dimensions" : {
"length": "",
"width" : "",
"height": ""
},
"shipping_required": true,
"shipping_taxable" : true,
"shipping_class" : "",
"shipping_class_id": 0,
"reviews_allowed" : false,
"average_rating" : "0.00",
"rating_count" : 0,
"related_ids" : [],
"upsell_ids" : [],
"cross_sell_ids" : [],
"parent_id" : 94,
"purchase_note" : "",
"categories" : [],
"tags" : [],
"images" : [
{
"id" : 0,
"date_created" : "2019-07-18T07:54:12",
"date_created_gmt" : "2019-07-18T07:54:12",
"date_modified" : "2019-07-18T07:54:12",
"date_modified_gmt": "2019-07-18T07:54:12",
"src" : "http://online-users.test/wp-content/uploads/woocommerce-placeholder-324x324.png",
"name" : "Placeholder",
"alt" : "Placeholder",
"position" : 0
}
],
"attributes": [
{
"id" : 1,
"name" : "Color",
"option": "Blue"
}
],
"default_attributes": [],
"variations" : [],
"grouped_products" : [],
"menu_order" : 1,
"meta_data" : [],
"store" : {
"id" : 1,
"name" : "admin",
"shop_name": "WordPress Biratnagar",
"url" : "http://online-users.test/store/admin/",
"address" : {
"street_1": "Haatkhola",
"street_2": "",
"city" : "Biratnagar",
"zip" : "977",
"country" : "NP",
"state" : "BAG"
}
}
},
{
"id" : 97,
"name" : "Nepali Shirt - Gray",
"slug" : "nepali-shirt-gray",
"permalink" : "http://online-users.test/product/nepali-shirt/?attribute_pa_color=gray",
"date_created" : "2019-07-14T05:12:13",
"date_created_gmt" : "2019-07-14T05:12:13",
"date_modified" : "2019-07-14T05:12:44",
"date_modified_gmt" : "2019-07-14T05:12:44",
"type" : "variation",
"status" : "publish",
"featured" : false,
"catalog_visibility": "visible",
"description" : "",
"short_description": "",
"sku": "",
"price": "300",
"regular_price": "300",
"sale_price": "",
"date_on_sale_from": null,
"date_on_sale_from_gmt": null,
"date_on_sale_to": null,
"date_on_sale_to_gmt": null,
"price_html": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">£</span>300.00</span>",
"on_sale": false,
"purchasable": true,
"total_sales": "0",
"virtual": false,
"downloadable": false,
"downloads": [],
"download_limit": -1,
"download_expiry": -1,
"external_url": "",
"button_text": "",
"tax_status": "taxable",
"tax_class": "",
"manage_stock": false,
"stock_quantity": null,
"in_stock": true,
"backorders": "no",
"backorders_allowed": false,
"backordered": false,
"sold_individually": false,
"weight": "",
"dimensions": {
"length": "",
"width": "",
"height": ""
},
"shipping_required": true,
"shipping_taxable": true,
"shipping_class": "",
"shipping_class_id": 0,
"reviews_allowed": false,
"average_rating": "0.00",
"rating_count": 0,
"related_ids": [],
"upsell_ids": [],
"cross_sell_ids": [],
"parent_id": 94,
"purchase_note": "",
"categories": [],
"tags": [],
"images": [
{
"id": 0,
"date_created": "2019-07-18T07:54:13",
"date_created_gmt": "2019-07-18T07:54:13",
"date_modified": "2019-07-18T07:54:13",
"date_modified_gmt": "2019-07-18T07:54:13",
"src": "http://online-users.test/wp-content/uploads/woocommerce-placeholder-324x324.png",
"name": "Placeholder",
"alt": "Placeholder",
"position": 0
}
],
"attributes": [
{
"id": 1,
"name": "Color",
"option": "Gray"
}
],
"default_attributes": [],
"variations": [],
"grouped_products": [],
"menu_order": 2,
"meta_data": [],
"store": {
"id": 1,
"name": "admin",
"shop_name": "WordPress Biratnagar",
"url": "http://online-users.test/store/admin/",
"address": {
"street_1": "Haatkhola",
"street_2": "",
"city": "Biratnagar",
"zip": "977",
"country": "NP",
"state": "BAG"
}
}
},
{
"id": 98,
"name": "Nepali Shirt - Red",
"slug": "nepali-shirt-red",
"permalink": "http://online-users.test/product/nepali-shirt/?attribute_pa_color=red",
"date_created": "2019-07-14T05:12:14",
"date_created_gmt": "2019-07-14T05:12:14",
"date_modified": "2019-07-14T05:42:04",
"date_modified_gmt": "2019-07-14T05:42:04",
"type": "variation",
"status": "publish",
"featured": false,
"catalog_visibility": "visible",
"description": "",
"short_description": "",
"sku": "",
"price": "500",
"regular_price": "500",
"sale_price": "",
"date_on_sale_from": null,
"date_on_sale_from_gmt": null,
"date_on_sale_to": null,
"date_on_sale_to_gmt": null,
"price_html": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">£</span>500.00</span>",
"on_sale": false,
"purchasable": true,
"total_sales": "0",
"virtual": false,
"downloadable": false,
"downloads": [],
"download_limit": -1,
"download_expiry": -1,
"external_url": "",
"button_text": "",
"tax_status": "taxable",
"tax_class": "",
"manage_stock": false,
"stock_quantity": null,
"in_stock": true,
"backorders": "no",
"backorders_allowed": false,
"backordered": false,
"sold_individually": false,
"weight": "",
"dimensions": {
"length": "",
"width": "",
"height": ""
},
"shipping_required": true,
"shipping_taxable": true,
"shipping_class": "",
"shipping_class_id": 0,
"reviews_allowed": false,
"average_rating": "0.00",
"rating_count": 0,
"related_ids": [],
"upsell_ids": [],
"cross_sell_ids": [],
"parent_id": 94,
"purchase_note": "",
"categories": [],
"tags": [],
"images": [
{
"id": 0,
"date_created": "2019-07-18T07:54:14",
"date_created_gmt": "2019-07-18T07:54:14",
"date_modified": "2019-07-18T07:54:14",
"date_modified_gmt": "2019-07-18T07:54:14",
"src": "http://online-users.test/wp-content/uploads/woocommerce-placeholder-324x324.png",
"name": "Placeholder",
"alt": "Placeholder",
"position": 0
}
],
"attributes": [
{
"id": 1,
"name": "Color",
"option": "Red"
}
],
"default_attributes": [],
"variations": [],
"grouped_products": [],
"menu_order": 3,
"meta_data": [],
"store": {
"id": 1,
"name": "admin",
"shop_name": "WordPress Biratnagar",
"url": "http://online-users.test/store/admin/",
"address": {
"street_1": "Haatkhola",
"street_2": "",
"city": "Biratnagar",
"zip": "977",
"country": "NP",
"state": "BAG"
}
}
}
]
}