在 WooCommerce 中获取非格式化的产品值

Fetching non formatted Product values in WooCommerce

我对微数据进行了以下格式化,并将其添加到 content-single-product.php WooCommerce 模板中。下面是我要添加的代码:

<div itemscope itemtype="http://schema.org/Product">
    <meta itemprop="name" content="Product Name">
<div itemscope itemtype="http://schema.org/Offer">
        <meta itemprop="sku" content="Product SKU">
        <meta itemprop="price" content="Product Price" />
        <meta itemprop="priceCurrency" content="Price Currency ex. INR" />
        <meta itemprop="category" content="Category Name">
        <meta itemprop="availability" content="http://schema.org/InStock" />
</div>

<div  itemscope itemtype="http://schema.org/PriceSpecification">
        <meta itemprop="minPrice" content="Special Price">
        <meta itemprop="validFrom" content="Sale Start Date">
        <meta itemprop="validThrough" content="Sale End Date">
</div>

<div  itemscope itemtype="http://schema.org/Thing">
        <meta itemprop="image" content="Product Image URL">
        <meta itemprop="description" content="Product Description">
</div>
</div>

在这个 content-single-product.php 模板中,我在这一行下方添加了一些代码:

<div itemscope itemtype="<?php echo woocommerce_get_product_schema(); ?>" id="product-<?php the_ID(); ?>" <?php post_class(); ?>>

我正在使用 WooCommerce 函数来获取产品详细信息,但它是 returns HTML 格式的值,我需要 non-formatted 产品值

Here are the TAGs details:

1.产品基本详情标签

2。价格标签

谢谢。

您需要获取 当前产品对象 为此,您将以这种方式一起使用 get_the_id() WordPress function with wc_get_product() WooCommerce 函数:

$product = wc_get_product(get_the_ID()); // Getting the Product object from current post ID

使用 $product 对象,您可以访问当前产品的任何原始数据。
您可以使用箭头语法使用所有 WC_Product class 属性和魔术属性:

echo $product->property_slug; // displays the product property (if is a string value)

(此处property_slug应替换为WC_Product class中列出的属性之一)。

$product_id = $product->id;
$product_type = $product->product_type;
$product_post_object =  $product->post; // see below

使用 post 属性 您可以访问所有 post 对象属性(就像 wp_posttable):

$product_slug = $product->post->post_name;
$product_title = $product->post->post_title;
$product_description = $product->post->post_content;
$product_short_description = $product->post->post_excerpt;

然后使用WC_Product class方法,你会得到如下:

$product_sku = $product->get_sku(); // or $product->sku
$product_url = $product->get_permalink();

// As a product can be in many categories (array)
$product_categories_array = get_the_terms( $product->id, 'product_cat' );
// The categories in a coma separated string
$product_categories_string = $product->get_categories(); 

要获取货币,您将使用专用函数 get_woocommerce_currency()

对于价格,您将使用相关的WC_product properties:

$product_price = $product->price;
$product_reg_price = $product->regular_price;

// Product Sale price
$product_sale_price = $product->sale_price;

// Scheduled  Sale price dates (timestamps)
$product_sale_start_ts = get_post_meta($product->id, '_sale_price_dates_from', true);
$product_sale_end_ts   = get_post_meta($product->id, '_sale_price_dates_to', true);

要以 DD/MM/YYYY 格式格式化产品预定销售价格日期,因为它们作为时间戳值存储在数据库中,我们将使用 PHP date() 以这种方式运行:

$product_sale_start_ts = get_post_meta($product->id, '_sale_price_dates_from', true);
$product_sale_start_date = date('d/m/Y', $product_sale_start_ts);
$product_sale_end_ts = get_post_meta($product->id, '_sale_price_dates_to', true);
$product_sale_end_date = date('d/m/Y', $product_sale_end_ts);

您还可以使用 get_post_meta() WordPress 功能访问此产品元数据,例如获取您将使用的 SKU:

$product_sku = get_post_meta($product->id, '_sku', true);

对于产品图片专用功能是get_the_post_thumbnail()功能:

$product_image = get_the_post_thumbnail( $product_id, 'shop_single' );

但作为产品可以有或没有图片,甚至是图片库,您应该查看 woocommerce templates > single-product > product-image.php 模板中的代码…