在 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.产品基本详情标签
产品名称:
<meta itemprop="name" content="Product Name">
在 CONTENT 下分配产品名称。这指定了产品标题,并将在搜索过程中向用户显示。
产品 SKU 代码
<meta itemprop="sku" content="Product SKU">
为您的产品分配唯一的产品代码。
产品URL:
<meta itemprop="url" content="Product URL">
此标签包含您网站的产品页面 URL。这可能像 http://www.YourWebsiteName.com/ProductName.php
产品类别
<meta itemprop="category" content="Category Name">
分配您的产品在您的网站上列出的类别名称。
产品图片
<meta itemprop="image" content="Product Image URL">
此标签包含产品图片 url。您需要将完整的产品图片 url 放在这里。
例如:http://www.YourWebsite.com/images/product_image.png
产品描述
<meta itemprop="description" content="Product Description">
在此处包括产品详细信息。
2。价格标签
- 价格货币:
<meta itemprop="priceCurrency" content="INR" />
在此处提及您的产品货币。
- 产品售价:
<meta itemprop="price" content="Product Price" />
此标签将包含产品销售 price/mrp。这是实际价格,高于此价格的产品无法在市场上买到。价格应该是一个数字,千位或空格之间没有分隔符(例如“8.99”)。
- 产品特价:
<meta itemprop="minPrice" content="Special Price">
为产品提供折扣价、优惠价或特价。这不是强制性的,但对于在搜索结果中排名靠前很重要。如果留空,系统会自动赋值为 0。价格应该是一个数字,千位或空格之间没有分隔符(例如“8.99”)。
- 特价开始日期:
<meta itemprop="validFrom" content="Sale Start Date">
如果提供折扣价,请以 DD/MM/YYYY 格式提及特价开始日期。如果为空,系统将自动分配当前日期。
- 特价结束日期:
<meta itemprop="validThrough" content="Sale End Date">
如果提供折扣价,请以 DD/MM/YYYY
格式提及特价结束日期。如果为空,系统将在 30 天后自动指定日期。
谢谢。
您需要获取 当前产品对象 为此,您将以这种方式一起使用 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_post
table):
$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
模板中的代码…
我对微数据进行了以下格式化,并将其添加到 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.产品基本详情标签
产品名称:
<meta itemprop="name" content="Product Name">
在 CONTENT 下分配产品名称。这指定了产品标题,并将在搜索过程中向用户显示。产品 SKU 代码
<meta itemprop="sku" content="Product SKU">
为您的产品分配唯一的产品代码。产品URL:
<meta itemprop="url" content="Product URL">
此标签包含您网站的产品页面 URL。这可能像 http://www.YourWebsiteName.com/ProductName.php产品类别
<meta itemprop="category" content="Category Name">
分配您的产品在您的网站上列出的类别名称。产品图片
<meta itemprop="image" content="Product Image URL">
此标签包含产品图片 url。您需要将完整的产品图片 url 放在这里。
例如:http://www.YourWebsite.com/images/product_image.png
产品描述
<meta itemprop="description" content="Product Description">
在此处包括产品详细信息。
2。价格标签
- 价格货币:
<meta itemprop="priceCurrency" content="INR" />
在此处提及您的产品货币。 - 产品售价:
<meta itemprop="price" content="Product Price" />
此标签将包含产品销售 price/mrp。这是实际价格,高于此价格的产品无法在市场上买到。价格应该是一个数字,千位或空格之间没有分隔符(例如“8.99”)。 - 产品特价:
<meta itemprop="minPrice" content="Special Price">
为产品提供折扣价、优惠价或特价。这不是强制性的,但对于在搜索结果中排名靠前很重要。如果留空,系统会自动赋值为 0。价格应该是一个数字,千位或空格之间没有分隔符(例如“8.99”)。 - 特价开始日期:
<meta itemprop="validFrom" content="Sale Start Date">
如果提供折扣价,请以 DD/MM/YYYY 格式提及特价开始日期。如果为空,系统将自动分配当前日期。 - 特价结束日期:
<meta itemprop="validThrough" content="Sale End Date">
如果提供折扣价,请以DD/MM/YYYY
格式提及特价结束日期。如果为空,系统将在 30 天后自动指定日期。
谢谢。
您需要获取 当前产品对象 为此,您将以这种方式一起使用 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_post
table):
$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
模板中的代码…