Bạn cần lấy tất cả các thông liên quan đến sản phẩm: ID, sku, hình ảnh, product meta…
Nếu bạn đang ở trang chi tiết sản phẩm thì bạn chỉ cần sử dụng trực tiếp các hàm của object $product bên dưới.
Tuy nhiên nếu bạn không ở trong trang chi tiết và bạn đã có product_id thì bạn cần sử dụng hàm: $product= wc_get_product( $product_id); sau đó bạn có thể sử dụng các hàm bên dưới để lấy thông tin.
$product_data = array(
'title' => $product->get_name(),
'id' => $product->get_id(),
'created_at' => $product->get_date_created(),
'updated_at' => $product->get_date_modified(),
'type' => $product->get_type(),
'status' => $product->get_status(),
'downloadable' => $product->is_downloadable(),
'virtual' => $product->is_virtual(),
'permalink' => $product->get_permalink(),
'sku' => $product->get_sku(),
'price' => wc_format_decimal( $product->get_price(), 2 ),
'regular_price' => wc_format_decimal( $product->get_regular_price(), 2 ),
'sale_price' => $product->get_sale_price() ? wc_format_decimal( $product->get_sale_price(), 2 ) : null,
'price_html' => $product->get_price_html(),
'taxable' => $product->is_taxable(),
'tax_status' => $product->get_tax_status(),
'tax_class' => $product->get_tax_class(),
'managing_stock' => $product->managing_stock(),
'stock_quantity' => $product->get_stock_quantity(),
'in_stock' => $product->is_in_stock(),
'backorders_allowed' => $product->backorders_allowed(),
'backordered' => $product->is_on_backorder(),
'sold_individually' => $product->is_sold_individually(),
'purchaseable' => $product->is_purchasable(),
'featured' => $product->is_featured(),
'visible' => $product->is_visible(),
'catalog_visibility' => $product->get_catalog_visibility(),
'on_sale' => $product->is_on_sale(),
'weight' => $product->get_weight() ? wc_format_decimal( $product->get_weight(), 2 ) : null,
'dimensions' => array(
'length' => $product->get_length(),
'width' => $product->get_width(),
'height' => $product->get_height(),
'unit' => get_option( 'woocommerce_dimension_unit' ),
),
'shipping_required' => $product->needs_shipping(),
'shipping_taxable' => $product->is_shipping_taxable(),
'shipping_class' => $product->get_shipping_class(),
'shipping_class_id' => ( 0 !== $product->get_shipping_class_id() ) ? $product->get_shipping_class_id() : null,
'description' => apply_filters( 'the_content', $product->get_description() ),
'short_description' => apply_filters( 'woocommerce_short_description', $product->get_short_description() ),
'reviews_allowed' => $product->get_reviews_allowed(),
'average_rating' => wc_format_decimal( $product->get_average_rating(), 2 ),
'rating_count' => $product->get_rating_count(),
'related_ids' => array_map( 'absint', array_values( wc_get_related_products( $product->get_id() ) ) ),
'upsell_ids' => array_map( 'absint', $product->get_upsell_ids() ),
'cross_sell_ids' => array_map( 'absint', $product->get_cross_sell_ids() ),
'categories' => wc_get_object_terms( $product->get_id(), 'product_cat', 'name' ),
'tags' => wc_get_object_terms( $product->get_id(), 'product_tag', 'name' ),
'product_image_id' => $product->get_image_id(),
'gallery_image_ids' => $product->get_gallery_image_ids(),
'featured_src' => wp_get_attachment_url( $product->get_image_id() ),
'download_limit' => $product->get_download_limit(),
'download_expiry' => $product->get_download_expiry(),
'total_sales' => $product->get_total_sales(),
);
Note: Bạn có thể sử dụng hàm $product = wc_get_product( $child_id ); với $child_id là product variation