以为我会为社区分享我的学习成果
此函数在产品导入后运行,然后将子产品分配给捆绑的产品包。我还分配了描述字段的元数据,以便以后查找更容易,并且更容易使用 wc 函数
https://docs.woocommerce.com/document/bundles/bundles-functions-reference/
通过我的导入,我只导入产品过滤器的高级属性 第二段代码是更直接的查找,其中我设置了一种提取元数据(包括自定义字段)的方法。这个想法是减少数据重复并保持元表精简仍在寻找一种方法来提高查询效率,但目前它做了我需要它做的事情
最后,大多数概率不会关心这一点,但我已经添加了一个函数来分配所有产品和资产具有相同的作者 ID
/**
* ==================================
* Action: pmxi_saved_post
* ==================================
*
* Called after a post is created/updated by WP All Import.
*
* @param $post_id int - The id of the post just created/updated
* @param $xml_node SimpleXMLElement - An object holding values for the current record
* @param $is_update - Boolean showing whether the post is created or updated
*
*/
function apyc_product_bundle_saved_post($post_id, $xml_node, $is_update)
{
/*
* Here you can use standard WordPress functions like get_post_meta() and get_post() to
* retrieve data, make changes and then save them with update_post() and/or update_post_meta()
*
* There are two ways to access the data from the current record in your import file:
*
* 1) Custom fields. For example, you could import a value to a custom field called "_temp" and
* then retrieve it here. Since it's only temporary, you'd probably want to delete it immediately:
*
* $my_value = get_post_meta($post_id, "_temp", true);
* delete_post_meta($post_id,"_temp");
*
* 2) The $xml param (a SimpleXMLElement object). This can be complex to work with if you're not
* used to iterators and/or xpath syntax. It's usually easiest to convert it a nested array using:
*
* $record = json_decode(json_encode((array) $xml_node), 1);
*/
/*
* You can also conditionally run your code based on the import ID:
*
* $import_id = ( isset( $_GET['id'] ) ? $_GET['id'] : ( isset( $_GET['import_id'] ) ? $_GET['import_id'] : 'new' ) );
* if ( $import_id == '8' ) {
* // run code
* }
*/
/**
* The product_id must exists in the database, if not it wont bundle.
* Also the product type must be simple, variable or simple/variable subs.
**/
if( $xml_node->product_type == 'Product bundle'){
if(isset($xml_node->child2_id)){
$data = [
'product_id' => (int)$xml_node->child2_id,
'bundle_id' => $post_id,
'menu_order' => 0,
'meta_data' => [
'description' => 'child2_title'
],
];
WC_PB_DB::add_bundled_item( $data );
}
if(isset($xml_node->child1_id)){
$data = [
'product_id' => (int)$xml_node->child1_id,
'bundle_id' => $post_id,
'menu_order' => 0,
'meta_data' => [
'description' => 'child1_title'
],
];
WC_PB_DB::add_bundled_item( $data );
}
}
image_author($post_id);
wp_publish_post($post_id);
}
add_action('pmxi_saved_post', 'apyc_product_bundle_saved_post', 10, 3);
function image_author($id)
{
$user_id = get_post_field ('post_author', $id);
$args = array(
'post_parent' => $id,
'post_type' => 'attachment'
//,
// 'post_mime_type' => 'image'
);
$attachments = get_posts($args);
if($attachments) :
foreach ($attachments as $attachment) : setup_postdata($attachment);
$the_post = array();
$the_post['ID'] = $attachment->ID;
$the_post['post_author'] = $user_id;
wp_update_post( $the_post );
endforeach;
endif;
}
用于在 woocommerce 子捆绑项目上提取元数据的新短代码
// Bundled Child Products meta fields shortcode
// Usage
// id is a required parameter
// Will return text wraped in a span matching the meta field
// [child_products_meta_fields id="custom-field-1" childcategory="category-x"]
// Adding output="url" Will return the URL of the meta attached file
// [child_products_meta_fields id="custom-field-1" childcategory="category-x"] output="url"]
function child_products_meta_fields_shortcode( $atts ) {
global $wpdb;
$atts = extract( shortcode_atts( array(
'id' => '',
'childcategory' => '',
'output' => ''
), $atts ) );
$packageid = get_the_ID();
$childcategory = " AND wp_terms.slug = '".$childcategory."'";
// $output ='';
$propertyid = $wpdb->get_results("SELECT
wp_woocommerce_bundled_items.product_id
FROM
wp_woocommerce_bundled_items
INNER JOIN wp_term_relationships ON wp_woocommerce_bundled_items.product_id = wp_term_relationships.object_id
INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE
wp_woocommerce_bundled_items.bundle_id = ".$packageid."
".$childcategory."
Limit 1");
ob_start();
foreach( $propertyid as $propertyid ){
$propertyid = $propertyid->product_id ;
}
if ( ! $id ) return;
$data = get_post_meta( $propertyid, $id, true );
if ( $data && $output == "") {
return '<span class="id-'. $id .'">'. $data .'</span>';
}
if ( $data && $output == "url") {
return wp_get_attachment_url( $data );
}
return ob_get_clean();
}
add_shortcode( 'child_products_meta_fields', 'child_products_meta_fields_shortcode' );