如何根据产品ID(woocommerce)显示wordpress产品图片

时间:2016-12-08 09:43:14

标签: wordpress

我刚刚工作的woocommerce插件(wordpress)。我有产品价格和名称,但我没有得到产品图片,它是我的ajax功能 ajax.php文件在那里吹,我想要产品image.i现在工作产品详细信息只添加到购物车页面,并显示产品当前价格和名称,但我没有得到图像。

ajax函数重定向ajax.php

第一个文件ajax.php

<script type="text/javascript">

$(document).ready(function(){
    $("#btn").click(function() {
      var val = $('#mce').val();
      var id = $('#p_id').val();
      var name = $('#pro_name').val();

        $.ajax ({
            url: "http://localhost/wordpress/wp-content/plugins/bootsgrid/ajax.php",
            data: { val : val, id : id, p_name : name },
            success: function( result ) {
               // window.location.href=window.location;
            }
        });
    });
});
</script>

第二个文件ajax.php

     <?php
        $productID=$_REQUEST['id'];
        $price=$_REQUEST['val'];
        $post_name=$_REQUEST['p_name'];

        require('../../../wp-load.php' );
        $img_url = $image = wp_get_attachment_image_src( get_post_thumbnail_id($productID), 'single-post-thumbnail' );

        //print_r($img_url);exit;

            global $woocommerce;

            $my_post = array(
              'post_title'    => $post_name,
              'post_content'  => 'This is my post.',
              'post_status'   => 'publish',
              'post_author'   => 1,
              'post_type'     =>'product'
            );
             $args = array(
           'post_type' => 'attachment',
           'numberposts' => -1,
           'post_status' => null,
           'post_parent' => $productID
          );
            $attachments = get_posts( $args );

            // Insert the post into the database
            $product_ID = wp_insert_post( $my_post );


        $attachmentID = $product_ID;


            add_action('add_attachment', function( $product_ID ) {
          if ( ! class_exists( 'WC_Product' ) ) return; // if no WooCommerce do nothing

          // an attachment was jus saved, first of all get the file name
          $src = wp_get_attachment_image_src( $attachmentID, 'full' );
          $filename = pathinfo( $src[0], PATHINFO_FILENAME );

          // now let's see if exits a product with the sku that match filename
          $args = array(
            'meta_key' => '_sku',
            'meta_value' => $filename,
            'post_type' => 'product',
            'posts_per_page' => '1' // assuming sku is unique get only one post
          );
          $prods = get_posts( $args );
          if ( ! empty($prods) ) {

            // ok we have a match, exists a product having sku that match filename
            $product = array_pop( $prods );

            // set the thumbnail for the product
            set_post_thumbnail( $product, $attachmentID );

            // now "attach" the post to the product setting 'post_parent'
            $attachment = get_post( $attachmentID );
            $attachment->post_parent =$attachments[0]->ID;
            wp_update_post( $attachment );
          }
        });

            if ( $product_ID ){
              add_post_meta($product_ID, '_regular_price', $price );
              add_post_meta($product_ID, '_price', $price );
              add_post_meta($product_ID, '_stock_status', 'instock' );

              //Getting error on this line.
              $woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );

            echo "Success";`enter code here`
            }


        ?>

0 个答案:

没有答案