以编程方式创建时,WooCommerce产品未显示在类别列表页面中

时间:2019-01-29 作者:Yves Gonzaga

我正在创建一个插件,将产品从API过滤到我们的WordPress站点。我确实成功地创建了产品,并在其中自动发布了类别和子类别。

http://prntscr.com/mdl1jb

问题是,当我在分类页面上查看时,它不会显示出来。我需要手动单击更新或发布按钮才能显示产品。

这方面的问题是以下代码:

$post_id = wp_insert_post( array(
                \'post_title\' => $items[\'description\'],
                \'post_content\' => $items[\'detail\'],
                \'post_excerpt\' => $items[\'description_2\'],
                \'post_status\' => \'publish\',
                \'post_type\' => "product",
            ) );

            // Set Product type
            // wp_set_object_terms( $post_id, \'simple\', \'product_type\');                            
            wp_set_post_terms( $post_id, \'simple\', \'product_type\');                         
            // Get parent category returns mixed or objects
            $category_parent = get_term_by(\'name\', \'Lense\', \'product_cat\');                     
            // Get child category returns array
            $category_child = term_exists( \'Canon\', \'product_cat\', $category_parent->term_id);

            // Set product brand
            // wp_set_object_terms( $post_id, \'Canon\', \'product_brand\');
            // wp_set_post_terms( $post_id, \'Canon\', \'product_brand\');

            // Set category for this product post
            // wp_set_object_terms( $post_id, $category_parent->term_id, \'product_cat\', false); 
            // wp_set_object_terms( $post_id, (int)$category_child[\'term_id\'], \'product_cat\', true);

            wp_set_post_terms($post_id, array($category_parent->term_id,(int)$category_child[\'term_id\']), \'product_cat\');


            // add_post_meta( $post_id, \'_wc_booking_availability\', $availability );

            update_post_meta( $post_id, \'_visibility\', \'visible\' );
            update_post_meta( $post_id, \'_booking_option\', \'yes\' );
            update_post_meta( $post_id, \'_stock_status\', \'instock\');
            // update_post_meta( $post_id, \'total_sales\', \'0\' );
            // update_post_meta( $post_id, \'_downloadable\', \'no\' );
            // update_post_meta( $post_id, \'_virtual\', \'yes\' );
            update_post_meta( $post_id, \'_regular_price\', 0);
            update_post_meta( $post_id, \'_sale_price\', 0);
            // update_post_meta( $post_id, \'_purchase_note\', \'\' );
            // update_post_meta( $post_id, \'_featured\', \'no\' );
            // update_post_meta( $post_id, \'_weight\', \'\' );
            // update_post_meta( $post_id, \'_length\', \'\' );
            // update_post_meta( $post_id, \'_width\', \'\' );
            // update_post_meta( $post_id, \'_height\', \'\' );
            // update_post_meta( $post_id, \'_sku\', $items[\'lookup_code\'] );
            // update_post_meta( $post_id, \'_product_attributes\', array() );
            // update_post_meta( $post_id, \'_sale_price_dates_from\', \'\' );
            // update_post_meta( $post_id, \'_sale_price_dates_to\', \'\' );
            update_post_meta( $post_id, \'_price\', $items[\'default_price\']);
            // update_post_meta( $post_id, \'_sold_individually\', \'\' );
            // update_post_meta( $post_id, \'_manage_stock\', \'no\' );
            // update_post_meta( $post_id, \'_backorders\', \'no\' );
            // update_post_meta( $post_id, \'_stock\', \'\' );
            // update_post_meta( $post_id, \'_\', \'\' );


            // ===================== Create product image =================
            $image = $items[\'cover_img_url\'];

            // magic sideload image returns an HTML image, not an ID
            $media = media_sideload_image($image, $post_id);

            // therefore we must find it so we can set it as featured ID
            if(!empty($media) && !is_wp_error($media)){
                $args = array(
                    \'post_type\' => \'attachment\',
                    \'posts_per_page\' => -1,
                    \'post_status\' => \'any\',
                    \'post_parent\' => $post_id
                );

                // reference new image to set as featured
                $attachments = get_posts($args);

                if(isset($attachments) && is_array($attachments)){
                    foreach($attachments as $attachment){
                        // grab source of full size images (so no 300x150 nonsense in path)
                        $image = wp_get_attachment_image_src($attachment->ID, \'full\');
                        // determine if in the $media image we created, the string of the URL exists
                        if(strpos($media, $image[0]) !== false){
                            // if so, we found our image. set it as thumbnail
                            set_post_thumbnail($post_id, $attachment->ID);
                            // only want one image
                            break;
                        }
                    }
                }
            }


            // ================= Create Product Gallery
            $images = $items[\'images\'];
            if(isset($images) && is_array($images)){

                $list_id = "";

                // magic sideload image returns an HTML image, not an ID
                $media = media_sideload_image($images[1][\'full_url\'], $post_id);

                // therefore we must find it so we can set it as featured ID
                if(!empty($media) && !is_wp_error($media)){
                    $args = array(
                        \'post_type\' => \'attachment\',
                        \'posts_per_page\' => -1,
                        \'post_status\' => \'any\',
                        \'post_parent\' => $post_id
                    );

                    // reference new image to set as featured
                    $attachments = get_posts($args);

                    if(isset($attachments) && is_array($attachments)){
                        foreach($attachments as $attachment){
                            // grab source of full size images (so no 300x150 nonsense in path)
                            $image = wp_get_attachment_image_src($attachment->ID, \'full\');
                            // determine if in the $media image we created, the string of the URL exists
                            if(strpos($media, $image[0]) !== false){
                                // if so, we found our image. set it as thumbnail
                                // set_post_thumbnail($post_id, $attachment->ID);
                                $list_id .= $attachment->ID . ",";
                                // only want one image
                                break;
                            }
                        }
                    }
                }

                update_post_meta($post_id,\'_product_image_gallery\',$list_id);
存档产品。PHP代码

<?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates
 * @version 3.4.0
 */

defined( \'ABSPATH\' ) || exit;

get_header( \'shop\' );


/**
 * Hook: woocommerce_before_main_content.
 *
 * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
 * @hooked woocommerce_breadcrumb - 20
 * @hooked WC_Structured_Data::generate_website_data() - 30
 */
do_action( \'woocommerce_before_main_content\' );

?>
<div class="site-main-product">
<header class="woocommerce-products-header">
    <?php /*if ( apply_filters( \'woocommerce_show_page_title\', true ) ) :*/ ?>
        <!-- <h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1> -->
    <?php /*endif;*/ ?>

    <?php
    /**
     * Hook: woocommerce_archive_description.
     *
     * @hooked woocommerce_taxonomy_archive_description - 10
     * @hooked woocommerce_product_archive_description - 10
     */
    /*do_action( \'woocommerce_archive_description\' );*/
    ?>

</header>
<?php
if ( woocommerce_product_loop() ) {

    /**
     * Hook: woocommerce_before_shop_loop.
     *
     * @hooked wc_print_notices - 10
     * @hooked woocommerce_result_count - 20
     * @hooked woocommerce_catalog_ordering - 30
     */
    /*do_action( \'woocommerce_before_shop_loop\' );*/
    ?>

    <div class="shop-page-notices">
        <div class="woocommerce-notices-shortcode woocommerce">
            <?php wc_print_notices(); ?>
        </div>
    </div>

    <?php if(is_shop()) : ?>
        <div class="shop-title">
            Best Rental Item
        </div>

    <?php else : ?>
        <div class="shop-title">
            <?php echo apply_filters( \'woocommerce_categories_header_title\', wp_title( \'»\', false ) ); ?>
        </div>
    <?php endif; ?> 

    <?php

    if ( wc_get_loop_prop( \'total\' ) ) {

        $taxonomy = \'product_cat\';
        $taxonomy_terms = get_terms( $taxonomy );

        foreach ($taxonomy_terms as $key => $value) {
            $is_displayed = false;
            while ( have_posts() ) {
                the_post();

                $brands = get_the_terms(get_the_ID(), $taxonomy);

                if(!empty($brands)){
                    foreach ($brands as $key => $brand) {
                        if($brand->term_taxonomy_id==$value->term_id){
                            $is_displayed = true;
                            break;
                        }
                    }

                }
            }
            if($is_displayed==true){
                $is_displayed = false;
            ?>  

            <div class="shop-archive-brand-title">
                <img src="<?php echo $imageURL = wp_get_attachment_image_url(get_term_meta($value->term_id, \'thumbnail_id\', true), \'medium\'); ?>" style="height: 60px;"/>
                <div class="shop-archive-brand-title-middle-line">
                    <hr>
                </div>
            </div>

            <?php

                woocommerce_product_loop_start();
                while ( have_posts() ) {
                    the_post();

                    $brands = get_the_terms(get_the_ID(), $taxonomy);

                    if(!empty($brands)){
                        foreach ($brands as $key => $brand) {
                                        //var_dump($value);
                            if($brand->term_taxonomy_id==$value->term_id){
                                do_action( \'woocommerce_shop_loop\' );
                                wc_get_template_part( \'content\', \'product\' );
                            }
                        }
                    }
                }
            }
            woocommerce_product_loop_end();
        }
    }



    /**
     * Hook: woocommerce_after_shop_loop.
     *
     * @hooked woocommerce_pagination - 10
     */
    do_action( \'woocommerce_after_shop_loop\' );
} else {
    /**
     * Hook: woocommerce_no_products_found.
     *
     * @hooked wc_no_products_found - 10
     */
    do_action( \'woocommerce_no_products_found\' );
}

/**
 * Hook: woocommerce_after_main_content.
 *
 * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
 */
do_action( \'woocommerce_after_main_content\' );

/**
 * Hook: woocommerce_sidebar.
 *
 * @hooked woocommerce_get_sidebar - 10
 */
?>
</div>

<?php
do_action( \'woocommerce_sidebar\' );

get_footer( \'shop\' );

1 个回复
最合适的回答,由SO网友:Pratik Patel 整理而成

尝试一下

$post_id = wp_insert_post( array(
    \'post_title\' => $items[\'description\'],
    \'post_content\' => $items[\'detail\'],
    \'post_excerpt\' => $items[\'description_2\'],
    \'post_status\' => \'publish\',
    \'post_type\' => "product",
) );

// Set Product type
// wp_set_object_terms( $post_id, \'simple\', \'product_type\');                            
wp_set_post_terms( $post_id, \'simple\', \'product_type\');
// Get parent category returns mixed or objects
$category_parent = get_term_by(\'name\', \'Lense\', \'product_cat\');
// Get child category returns array
$category_child = term_exists( \'Canon\', \'product_cat\', $category_parent->term_id);

// Set product brand
// wp_set_object_terms( $post_id, \'Canon\', \'product_brand\');
// wp_set_post_terms( $post_id, \'Canon\', \'product_brand\');

// Set category for this product post
// wp_set_object_terms( $post_id, $category_parent->term_id, \'product_cat\', false); 
// wp_set_object_terms( $post_id, (int)$category_child[\'term_id\'], \'product_cat\', true);

wp_set_post_terms($post_id, array($category_parent->term_id,(int)$category_child[\'term_id\']), \'product_cat\');


// add_post_meta( $post_id, \'_wc_booking_availability\', $availability );

update_post_meta( $post_id, \'_visibility\', \'visible\' );
update_post_meta( $post_id, \'_stock_status\', \'instock\');
update_post_meta( $post_id, \'total_sales\', \'0\');
update_post_meta( $post_id, \'_downloadable\', \'yes\');
update_post_meta( $post_id, \'_virtual\', \'yes\');
update_post_meta( $post_id, \'_regular_price\', "1" );
update_post_meta( $post_id, \'_sale_price\', "1" );
update_post_meta( $post_id, \'_purchase_note\', "" );
update_post_meta( $post_id, \'_featured\', "no" );
update_post_meta( $post_id, \'_weight\', "" );
update_post_meta( $post_id, \'_length\', "" );
update_post_meta( $post_id, \'_width\', "" );
update_post_meta( $post_id, \'_height\', "" );
update_post_meta($post_id, \'_sku\', "");
update_post_meta( $post_id, \'_product_attributes\', array());
update_post_meta( $post_id, \'_sale_price_dates_from\', "" );
update_post_meta( $post_id, \'_sale_price_dates_to\', "" );
update_post_meta( $post_id, \'_price\', "1" );
update_post_meta( $post_id, \'_sold_individually\', "" );
update_post_meta( $post_id, \'_manage_stock\', "no" );
update_post_meta( $post_id, \'_backorders\', "no" );
update_post_meta( $post_id, \'_stock\', "" );




// ===================== Create product image =================
$image = $items[\'cover_img_url\'];

// magic sideload image returns an HTML image, not an ID
$media = media_sideload_image($image, $post_id);

// therefore we must find it so we can set it as featured ID
if(!empty($media) && !is_wp_error($media)){
    $args = array(
        \'post_type\' => \'attachment\',
        \'posts_per_page\' => -1,
        \'post_status\' => \'any\',
        \'post_parent\' => $post_id
    );

    // reference new image to set as featured
    $attachments = get_posts($args);

    if(isset($attachments) && is_array($attachments)){
        foreach($attachments as $attachment){
            // grab source of full size images (so no 300x150 nonsense in path)
            $image = wp_get_attachment_image_src($attachment->ID, \'full\');
            // determine if in the $media image we created, the string of the URL exists
            if(strpos($media, $image[0]) !== false){
                // if so, we found our image. set it as thumbnail
                set_post_thumbnail($post_id, $attachment->ID);
                // only want one image
                break;
            }
        }
    }
}


// ================= Create Product Gallery
$images = $items[\'images\'];
if(isset($images) && is_array($images)){

    $list_id = "";

    // magic sideload image returns an HTML image, not an ID
    $media = media_sideload_image($images[1][\'full_url\'], $post_id);

    // therefore we must find it so we can set it as featured ID
    if(!empty($media) && !is_wp_error($media)){
        $args = array(
            \'post_type\' => \'attachment\',
            \'posts_per_page\' => -1,
            \'post_status\' => \'any\',
            \'post_parent\' => $post_id
        );

        // reference new image to set as featured
        $attachments = get_posts($args);

        if(isset($attachments) && is_array($attachments)){
            foreach($attachments as $attachment){
                // grab source of full size images (so no 300x150 nonsense in path)
                $image = wp_get_attachment_image_src($attachment->ID, \'full\');
                // determine if in the $media image we created, the string of the URL exists
                if(strpos($media, $image[0]) !== false){
                    // if so, we found our image. set it as thumbnail
                    // set_post_thumbnail($post_id, $attachment->ID);
                    $list_id .= $attachment->ID . ",";
                    // only want one image
                    break;
                }
            }
        }
    }

    update_post_meta($post_id,\'_product_image_gallery\',$list_id);
希望这会有帮助!

相关推荐

WP_DROPDOWN_CATEGORIES-如何在Widget中保存?

我想用wp_dropdown_categories 在自定义小部件中。所有内容都显示得很好,但由于某些原因,无法正确保存。这是form() 和update() 小部件的功能-我做错什么了吗?public function form( $instance ) { /* Set up some default widget settings. */ $defaults = array( \'title\' => \'Classes by Category\' );&#x