Wordpress 通过WP All import导入Woocommerce产品捆绑包
试图找出如何通过WP ALL import()将woocommerce捆绑产品导入Wordpress 对于捆绑产品的wpallimport,没有本机支持,只支持分组产品 我一直在阅读wp all import的API和函数文档,我觉得在保存了一个包之后,我可能可以通过导入函数来完成这项工作 我看不到任何关于如何 1.使用函数中的提要数据,希望确认这是否可行 2.是否可以参考woocommerce功能,即添加捆绑商品元,如果可以,最好的方法是什么? 3.将其中一个捆绑产品的功能图像链接到正在更新的捆绑产品的最佳方法 我的代码是新手级的,我通过逆向工程学习一些东西。 请原谅我缺乏php知识 Woocommerce改变了他们处理捆绑包的方式,因此它不再只是一个导入字段,而是一个单独的表,找到了这个stackoverflow线程,但与旧功能相同。 显影剂标准物质 还有另一个专用的Woocommerce bundle导入插件,但它不是wpallimport 我的想法是在导入函数中执行类似的操作Wordpress 通过WP All import导入Woocommerce产品捆绑包,wordpress,woocommerce,e-commerce,hook-woocommerce,wpallimport,Wordpress,Woocommerce,E Commerce,Hook Woocommerce,Wpallimport,试图找出如何通过WP ALL import()将woocommerce捆绑产品导入Wordpress 对于捆绑产品的wpallimport,没有本机支持,只支持分组产品 我一直在阅读wp all import的API和函数文档,我觉得在保存了一个包之后,我可能可以通过导入函数来完成这项工作 我看不到任何关于如何 1.使用函数中的提要数据,希望确认这是否可行 2.是否可以参考woocommerce功能,即添加捆绑商品元,如果可以,最好的方法是什么? 3.将其中一个捆绑产品的功能图像链接到正在更新的
add_action('pmxi_saved_post', 'update_bundle', 10, 1);
if ({product_type[1]} == 'product bundle'){
function update_bundle($id)
{
// is it a product bundle test?
if ({product_type[1]} == 'product bundle'){
// Get products from import file (not sure if this is correct, but guessing it will be something like this)
$Bundle_product_1 = {Bundle_product_1[1]};
$Bundle_product_2 = {Bundle_product_2[1]};
// Get all bundled ids against product currently
$bundled_ids = WC_PB_DB::query_bundled_items( array(
'return' => 'id=>bundle_id',
'product_id' => array( $id )
) );
if (in_array($Bundle_product_1, $bundled_ids->product_id))
{
echo "";
}else{
$item_id = $Bundle_product_1;
$meta_key = ;
$meta_value = ;
$result = WC_PB_DB::add_bundled_item_meta( $item_id, $meta_key, $meta_value );
}
}
}
我想我会为社区分享我学到的东西 此函数在导入产品后运行,然后将子产品分配给捆绑产品包。我还分配了描述字段的meta,以便以后更容易查找,并使wc函数更容易使用 通过导入,我只导入产品过滤器的高级属性 第二段代码是更直接的查找,我在其中设置了提取元数据(包括自定义字段)的方法。 其思想是减少数据重复并保持元表精简 仍然在寻找一种使查询更高效的方法,但目前它做了我需要它做的事情 最后,大多数prob都不关心这一点,但我在一个函数中添加了一个功能,将所有产品和资产分配为具有相同的作者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;
}
用于提取子捆绑项上元数据的新短代码
// 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' );
//捆绑子产品元字段短码
//用法
//id是必需的参数
//将返回包装在与元字段匹配的范围内的文本
//[child\u products\u meta\u fields id=“custom-field-1”childcategory=“category-x”]
//添加output=“url”将返回元附加文件的url
//[child\u products\u meta\u fields id=“custom-field-1”childcategory=“category-x”]output=“url”]
函数子项\产品\元\字段\短码($atts){
全球$wpdb;
$atts=提取(短码)\附加(数组(
“id'=>”,
“儿童类别”=>“”,
“输出”=>“
)美元(附件);;
$packageid=获取_ID();
$childcategory=“和wp_terms.slug=””$childcategory。“”;
//$output='';
$propertyid=$wpdb->获取结果(“选择
wp\u商业\u捆绑的\u项目。产品\u id
从…起
wp_商业_捆绑_项目
wp\u bundle\u items.product\u id=wp\u term\u relationships.object\u id上的内部连接wp\u term\u关系
wp\u term\u关系上的内部联接wp\u term\u分类法。term\u taxonomy\u id=wp\u term\u taxonomy.term\u taxonomy\u id
wp_term_分类法上的内部联接wp_terms.term_id=wp_terms.term_id
哪里
wp_woodcommerce_bundle_items.bundle_id=“.$packageid.”
“$childcategory。”
限额1”);
ob_start();
foreach($propertyid作为$propertyid){
$propertyid=$propertyid->product\U id;
}
如果(!$id)返回;
$data=get\u post\u meta($propertyid,$id,true);
如果($data&&$output==“”){
返回“”。$data。“;
}
如果($data&&$output==“url”){
返回wp_get_attachment_url($data);
}
返回ob_get_clean();
}
添加_短码('child_products_meta_fields'、'child_products_meta_fields_短码');