本文整理汇总了PHP中WC_Product::get_name方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product::get_name方法的具体用法?PHP WC_Product::get_name怎么用?PHP WC_Product::get_name使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product
的用法示例。
在下文中一共展示了WC_Product::get_name方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_product_data
/**
* Get standard product data that applies to every product type
*
* @since 2.1
* @param WC_Product $product
* @return array
*/
private function get_product_data($product)
{
return array('title' => $product->get_name(), 'id' => $product->get_id(), 'created_at' => $this->server->format_datetime($product->get_date_created(), false, true), 'updated_at' => $this->server->format_datetime($product->get_date_modified(), false, true), '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'), 'images' => $this->get_images($product), 'featured_src' => wp_get_attachment_url(get_post_thumbnail_id($product->get_id())), 'attributes' => $this->get_attributes($product), 'downloads' => $this->get_downloads($product), 'download_limit' => $product->get_download_limit(), 'download_expiry' => $product->get_download_expiry(), 'download_type' => 'standard', 'purchase_note' => apply_filters('the_content', $product->get_purchase_note()), 'total_sales' => $product->get_total_sales(), 'variations' => array(), 'parent' => array());
}
示例2:
/**
* Test creating a new product.
*
* @since 2.7.0
*/
function test_product_create()
{
$product = new WC_Product();
$product->set_regular_price(42);
$product->set_name('My Product');
$product->save();
$read_product = new WC_Product($product->get_id());
$this->assertEquals('42', $read_product->get_regular_price());
$this->assertEquals('My Product', $read_product->get_name());
}
示例3: save_variations
/**
* Save variations
*
* @since 2.2
* @param WC_Product $product
* @param array $request
* @return WC_Product
* @throws WC_API_Exception
*/
protected function save_variations($product, $request)
{
global $wpdb;
$id = $product->get_id();
$attributes = $product->get_variation_attributes();
foreach ($request['variations'] as $menu_order => $data) {
$variation_id = isset($data['id']) ? absint($data['id']) : 0;
$variation = new WC_Product_Variation($variation_id);
// Create initial name and status.
if (!$variation->get_slug()) {
/* translators: 1: variation id 2: product name */
$variation->set_name(sprintf(__('Variation #%1$s of %2$s', 'woocommerce'), $variation->get_id(), $product->get_name()));
$variation->set_status(isset($data['visible']) && false === $data['visible'] ? 'private' : 'publish');
}
// Parent ID.
$variation->set_parent_id($product->get_id());
// Menu order.
$variation->set_menu_order($menu_order);
// Status.
if (isset($data['visible'])) {
$variation->set_status(false === $data['visible'] ? 'private' : 'publish');
}
// SKU.
if (isset($data['sku'])) {
$variation->set_sku(wc_clean($data['sku']));
}
// Thumbnail.
if (isset($data['image']) && is_array($data['image'])) {
$image = current($data['image']);
if (is_array($image)) {
$image['position'] = 0;
}
$variation = $this->save_product_images($variation, array($image));
}
// Virtual variation.
if (isset($data['virtual'])) {
$variation->set_virtual($data['virtual']);
}
// Downloadable variation.
if (isset($data['downloadable'])) {
$is_downloadable = $data['downloadable'];
$variation->set_downloadable($is_downloadable);
} else {
$is_downloadable = $variation->get_downloadable();
}
// Downloads.
if ($is_downloadable) {
// Downloadable files.
if (isset($data['downloads']) && is_array($data['downloads'])) {
$variation = $this->save_downloadable_files($variation, $data['downloads']);
}
// Download limit.
if (isset($data['download_limit'])) {
$variation->set_download_limit($data['download_limit']);
}
// Download expiry.
if (isset($data['download_expiry'])) {
$variation->set_download_expiry($data['download_expiry']);
}
}
// Shipping data.
$variation = $this->save_product_shipping_data($variation, $data);
// Stock handling.
$manage_stock = (bool) $variation->get_manage_stock();
if (isset($data['managing_stock'])) {
$manage_stock = $data['managing_stock'];
}
$variation->set_manage_stock($manage_stock);
$stock_status = $variation->get_stock_status();
if (isset($data['in_stock'])) {
$stock_status = true === $data['in_stock'] ? 'instock' : 'outofstock';
}
$variation->set_stock_status($stock_status);
$backorders = $variation->get_backorders();
if (isset($data['backorders'])) {
$backorders = $data['backorders'];
}
$variation->set_backorders($backorders);
if ($manage_stock) {
if (isset($data['stock_quantity'])) {
$variation->set_stock_quantity($data['stock_quantity']);
}
} else {
$variation->set_backorders('no');
$variation->set_stock_quantity('');
}
// Regular Price.
if (isset($data['regular_price'])) {
$variation->set_regular_price($data['regular_price']);
}
// Sale Price.
//.........这里部分代码省略.........
示例4: save_variations_data
/**
* Save variations.
*
* @throws WC_REST_Exception REST API exceptions.
* @param WC_Product $product Product instance.
* @param WP_REST_Request $request Request data.
* @param bool $single_variation True if saving only a single variation.
* @return bool
*/
protected function save_variations_data($product, $request, $single_variation = false)
{
global $wpdb;
if ($single_variation) {
$variations = array($request);
} else {
$variations = $request['variations'];
}
foreach ($variations as $menu_order => $data) {
$variation_id = isset($data['id']) ? absint($data['id']) : 0;
$variation = new WC_Product_Variation($variation_id);
// Create initial name and status.
if (!$variation->get_slug()) {
/* translators: 1: variation id 2: product name */
$variation->set_name(sprintf(__('Variation #%1$s of %2$s', 'woocommerce'), $variation->get_id(), $product->get_name()));
$variation->set_status(isset($data['visible']) && false === $data['visible'] ? 'private' : 'publish');
}
// Parent ID.
$variation->set_parent_id($product->get_id());
// Menu order.
$variation->set_menu_order($menu_order);
// Status.
if (isset($data['visible'])) {
$variation->set_status(false === $data['visible'] ? 'private' : 'publish');
}
// SKU.
if (isset($data['sku'])) {
$variation->set_sku(wc_clean($data['sku']));
}
// Thumbnail.
if (isset($data['image']) && is_array($data['image'])) {
$image = $data['image'];
$image = current($image);
if (is_array($image)) {
$image['position'] = 0;
}
$variation = $this->save_product_images($variation, array($image));
}
// Virtual variation.
if (isset($data['virtual'])) {
$variation->set_virtual($data['virtual']);
}
// Downloadable variation.
if (isset($data['downloadable'])) {
$variation->set_downloadable($data['downloadable']);
}
// Downloads.
if ($variation->get_downloadable()) {
// Downloadable files.
if (isset($data['downloads']) && is_array($data['downloads'])) {
$variation = $this->save_downloadable_files($variation, $data['downloads']);
}
// Download limit.
if (isset($data['download_limit'])) {
$variation->set_download_limit($data['download_limit']);
}
// Download expiry.
if (isset($data['download_expiry'])) {
$variation->set_download_expiry($data['download_expiry']);
}
}
// Shipping data.
$variation = $this->save_product_shipping_data($variation, $data);
// Stock handling.
if (isset($data['manage_stock'])) {
$variation->set_manage_stock($data['manage_stock']);
}
if (isset($data['in_stock'])) {
$variation->set_stock_status(true === $data['in_stock'] ? 'instock' : 'outofstock');
}
if (isset($data['backorders'])) {
$variation->set_backorders($data['backorders']);
}
if ($variation->get_manage_stock()) {
if (isset($data['stock_quantity'])) {
$variation->set_stock_quantity($data['stock_quantity']);
} elseif (isset($data['inventory_delta'])) {
$stock_quantity = wc_stock_amount($variation->get_stock_amount());
$stock_quantity += wc_stock_amount($data['inventory_delta']);
$variation->set_stock_quantity($stock_quantity);
}
} else {
$variation->set_backorders('no');
$variation->set_stock_quantity('');
}
// Regular Price.
if (isset($data['regular_price'])) {
$variation->set_regular_price($data['regular_price']);
}
// Sale Price.
if (isset($data['sale_price'])) {
//.........这里部分代码省略.........
示例5: add_product
/**
* Add a product line item to the order. This is the only line item type with
* it's own method because it saves looking up order amounts (costs are added up for you).
* @param \WC_Product $product
* @param int $qty
* @param array $args
* @return int order item ID
* @throws WC_Data_Exception
*/
public function add_product($product, $qty = 1, $args = array())
{
if ($product) {
$default_args = array('name' => $product->get_name(), 'tax_class' => $product->get_tax_class(), 'product_id' => $product->is_type('variation') ? $product->get_parent_id() : $product->get_id(), 'variation_id' => $product->is_type('variation') ? $product->get_id() : 0, 'variation' => $product->is_type('variation') ? $product->get_attributes() : array(), 'subtotal' => wc_get_price_excluding_tax($product, array('qty' => $qty)), 'total' => wc_get_price_excluding_tax($product, array('qty' => $qty)), 'quantity' => $qty);
} else {
$default_args = array('quantity' => $qty);
}
$args = wp_parse_args($args, $default_args);
// BW compatibility with old args
if (isset($args['totals'])) {
foreach ($args['totals'] as $key => $value) {
if ('tax' === $key) {
$args['total_tax'] = $value;
} elseif ('tax_data' === $key) {
$args['taxes'] = $value;
} else {
$args[$key] = $value;
}
}
}
$item = new WC_Order_Item_Product();
$item->set_props($args);
$item->set_backorder_meta();
$item->set_order_id($this->get_id());
$item->save();
$this->add_item($item);
wc_do_deprecated_action('woocommerce_order_add_product', array($this->get_id(), $item->get_id(), $product, $qty, $args), '2.7', 'Use woocommerce_new_order_item action instead.');
return $item->get_id();
}