1

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

捆绑产品的 wpallimport 没有原生支持,只有分组产品。

我一直在阅读 wp all import 的 API 和函数文档,并且觉得我可以在保存包后通过导入函数来完成它。

我看不到有关如何 1. 在函数中使用 feed 数据的任何文档,想确认是否可行 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/ 批量导入产品包 – 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 );
}    
}
}
4

1 回答 1

1

以为我会为社区分享我的学习成果

此函数在产品导入后运行,然后将子产品分配给捆绑的产品包。我还分配了描述字段的元数据,以便以后查找更容易,并且更容易使用 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' );
于 2019-08-13T06:34:53.740 回答