Woocommerce产品捆绑包通过WP All Import导入

时间:2019-08-07 17:13:46

标签: wordpress woocommerce e-commerce hook-woocommerce wpallimport

试图弄清楚如何通过WP ALL Import(http://www.wpallimport.com)将woocommerce捆绑产品导入Wordpress。

对于捆绑产品,只有分组产品,wpallimport没有本地支持。

我一直在阅读wp所有导入的API和函数文档,并觉得保存捆绑包后也许可以通过导入功能来做到这一点。

我看不到任何有关如何操作的文档 1.使用功能中的提要数据,希望确认是否可行 2.是否可以引用woocommerce函数,即add_bundled_item_meta,如果是的话,最好的方法是什么? https://docs.woocommerce.com/document/bundles/bundles-functions-reference/#add_bundled_item_meta 3.将其中一个捆绑产品的特征图像链接到要更新的捆绑产品的最佳方法

我的编码是新手,我通过反向工程学到一些东西。 对不起,我缺乏php知识

Woocommerce改变了他们处理包的方式,因此它不再只是导入字段,而是一个单独的表,发现了这个stackoverflow线程,但与旧功能相同。 https://docs.woocommerce.com/document/bundles/bundles-v5-0-whats-new/ Bulk import Product Bundles – WooCommerce?

开发人员参考资料 https://docs.woocommerce.com/document/bundles/#developer-resources https://docs.woocommerce.com/document/bundles/bundles-functions-reference/#add_bundled_item_meta https://docs.woocommerce.com/document/bundles/bundles-rest-api-reference/

还有另一个专用的Woocommerce捆绑包导入插件,但不是wpallimport

https://www.webtoffee.com/woocommerce-import-and-export-of-bundle-products/

我的想法是在导入功能中做类似的事情



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 );
}    
}
}

1 个答案:

答案 0 :(得分:0)

我想为社区分享我的经验

此功能在产品导入后运行,然后将子产品分配给捆绑的产品包装。我还为描述字段分配了meta元素,以便以后查找,并使使用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' );