WooCommerce-productgegevens met behulp van PHP

WooCommerce-productgegevens

Krijg eenvoudig toegang tot alle WooCommerce-productgegevens met behulp van PHP.

Zodra u het $product-object beschikbaar heeft in WooCommerce, is het heel eenvoudig om alle gegevens op te halen waarnaar u mogelijk op zoek bent.

Dit omvat het vinden van de producttitel, prijs, verkoopprijs, afbeeldingen en andere instellingen die u in de backend van WooCommerce heeft ingesteld.

Om te beginnen zal ik de lijst met methoden dumpen die voor u beschikbaar zijn zodra u het $product-object hebt.

Zorg ervoor dat u een van de onderstaande methodeverzoeken toewijst aan een variabele naar keuze, zodat u ermee kunt werken.

<?php

/**
*

  • General Product Data
    *
    */
    $product->get_id(); // Returns the unique ID for this object.
    $product->get_description(); // Get product description.
    $product->get_formatted_name(); // Get product name with SKU or ID. Used within admin.
    $product->get_featured(); // If the product is featured.
    $product->get_name(); // Get product name.
    $product->get_title(); // Get the product’s title. For products this is the product name.
    $product->get_type(); // Get internal type. Should return string and *should be overridden* by child classes.
    $product->get_virtual(); // Get virtual.
    $product->get_total_sales(); // Get number total of sales.
    $product->get_short_description(); // Get product short description.
    $product->get_sku(); // Get SKU (Stock-keeping unit) – product unique ID.
    $product->get_slug(); // Get product slug.
    $product->get_status(); // Get product status.
    $product->get_permalink(); // Product permalink.
    $product->get_catalog_visibility(); // Get catalog visibility.

/**
*

  • Pricing Data
    *
    */
    $product->get_price(); // Returns the product’s active price.
    $product->get_date_on_sale_from(); // Get date on sale from.
    $product->get_date_on_sale_to(); // Get date on sale to.
    $product->get_display_price(); // Returns the price including or excluding tax, based on the ‘woocommerce_tax_display_shop’ setting.
    $product->get_price_excluding_tax(); // Returns the price (excluding tax) – ignores tax_class filters since the price may *include* tax and thus needs subtracting.
    $product->get_price_html(); // Returns the price in html format.
    $product->get_price_html_from_text(); // Functions for getting parts of a price, in html, used by $product->get_price_html.
    $product->get_price_html_from_to(); // Functions for getting parts of a price, in html, used by $product->get_price_html.
    $product->get_price_including_tax(); // Returns the price (including tax). Uses customer tax rates. Can work for a specific $qty for more accurate taxes.
    $product->get_price_suffix(); // Get the suffix to display after prices > 0.
    $product->get_sale_price(); // Returns the product’s sale price.
    $product->get_regular_price(); // Returns the product’s regular price.
    $product->get_tax_class(); // Returns the tax class.
    $product->get_tax_status(); // Returns the tax status.

/**
*

  • Image Related Data
    *
    */
    $product->get_image(); // Returns the main product image.
    $product->get_image_id(); // Get main image ID.
    $product->get_gallery_attachment_ids(); // Returns the gallery attachment ids.
    $product->get_gallery_image_ids(); // Returns the gallery attachment ids.

/**
*

  • Stock or Inventory Data
    *
    */
    $product->get_backorders(); // Get backorders.
    $product->get_availability(); // Returns the availability of the product.
    $product->get_max_purchase_quantity(); // Get max quantity which can be purchased at once.
    $product->get_min_purchase_quantity(); // Get min quantity which can be purchased at once.
    $product->get_stock_managed_by_id(); // If the stock level comes from another product ID, this should be modified.
    $product->get_stock_quantity(); // Returns number of items available for sale.
    $product->get_stock_status(); // Return the stock status.
    $product->get_total_stock(); // Get total stock – This is the stock of parent and children combined.
    $product->get_sold_individually(); // Return if should be sold individually.
    $product->get_low_stock_amount(); // Get low stock amount.

/**
*

  • Shipping Data
    *
    */
    $product->get_height(); // Returns the product height.
    $product->get_length(); // Returns the product length.
    $product->get_weight(); // Returns the product’s weight.
    $product->get_width(); // Returns the product width.
    $product->get_dimensions(); // Returns formatted dimensions.
    $product->get_manage_stock(); // Return if product manage stock.
    $product->get_shipping_class(); // Returns the product shipping class SLUG.
    $product->get_shipping_class_id(); // Get shipping class ID.

/**
*

  • Product Variations / Parent Data
    *
    */
    $product->get_child(); // Returns the child product.
    $product->get_children(); // Returns the children IDs if applicable. Overridden by child classes.
    $product->get_formatted_variation_attributes(); // Get formatted variation data with WC < 2.4 back compat and proper formatting of text-based attribute names. $product->get_matching_variation(); // Match a variation to a given set of attributes using a WP_Query.
    $product->get_parent(); // Get the parent of the post.
    $product->get_parent_id(); // Get parent ID.
    $product->get_variation_default_attributes(); // If set, get the default attributes for a variable product.
    $product->get_variation_description(); // Get product variation description.
    $product->get_variation_id(); // Get variation ID.

/**
*

  • Product Downloads
    *
    */
    $product->get_download_expiry(); // Get download expiry.
    $product->get_download_limit(); // Get download limit.
    $product->get_downloadable(); // Get downloadable.
    $product->get_downloads(); // Get downloads.
    $product->get_file(); // Get a file by $download_id.
    $product->get_file_download_path(); // Get file download path identified by $download_id.
    $product->get_files(); // Same as $product->get_downloads in CRUD.

/**
*

  • Attributes, Tags, Categories & Associated Data Objects
    *
    */
    $product->get_attribute(); // Returns a single product attribute as a string.
    $product->get_attributes(); // Returns product attributes.
    $product->get_categories(); // Returns the product categories.
    $product->get_category_ids(); // Get category ids.
    $product->get_default_attributes(); // Get default attributes.
    $product->get_cross_sell_ids(); // Get cross sell IDs.
    $product->get_cross_sells(); // Returns the cross sell product ids.
    $product->get_related(); // Get and return related products.
    $product->get_tag_ids(); // Get tag ids.
    $product->get_tags(); // Returns the product tags.
    $product->get_upsell_ids(); // Get upsell IDs.
    $product->get_upsells(); // Returns the upsell product ids.
    $product->get_meta(); // Get Meta Data by Key.
    $product->get_meta_data(); // Get All Meta Data.

/**
*

  • Ratings and Reviews
    *
    */
    $product->get_rating_count(); // Get the total amount (COUNT) of ratings, or just the count for one rating e.g. number of 5 star ratings.
    $product->get_rating_counts(); // Get rating count.
    $product->get_rating_html(); // Returns the product rating in html format.
    $product->get_review_count(); // Get review count.
    $product->get_reviews_allowed(); // Return if reviews is allowed.
    $product->get_average_rating(); // Get average rating.

/**
*

  • Other Product Data
    *
    */
    $product->get_changes(); // Return data changes only.
    $product->get_data(); // Returns all data for this object.
    $product->get_data_keys(); // Returns array of expected data keys for this object.
    $product->get_data_store(); // Get the data store.
    $product->get_date_created(); // Get product created date.
    $product->get_date_modified(); // Get product modified date.
    $product->get_extra_data_keys(); // Returns all “extra” data keys for an object (for sub objects like product types).
    $product->get_menu_order(); // Get menu order.
    $product->get_meta_cache_key(); // Helper method to compute meta cache key. Different from WP Meta cache key in that meta data cached using this key also contains meta_id column.
    $product->get_object_read(); // Get object read property.
    $product->get_post_data(); // Get the product’s post data.
    $product->get_post_password(); // Get post password.
    $product->get_purchase_note(); // Get purchase note.

Productgegevens toewijzen aan een variabele

Om glashelder te zijn, als ik zeg dat je de methoden aan een variabele moet toewijzen, bedoel ik dit daarmee:

global $product;

// Assign product name
$product_name = $product->get_name(); // Get product name.

// Wrap product name in h2 and output it
echo ‘

‘ . $product_name . ‘

‘;

Hoe toegang te krijgen tot het $product-object

U moet ervoor zorgen dat u toegang hebt tot het productobject. Als u een van de methoden probeert uit te voeren op een variabele die geen productobject is, krijgt u een fatale fout.

Afhankelijk van waar u zich bevindt in uw WordPress-installatie en wat u doet, kunt u op verschillende manieren toegang krijgen tot het $product-object.
Toegang krijgen tot het $product-object als u de product-ID hebt

Dit is een eenvoudige, als je de product_id hebt, heb je toegang tot het productobject met behulp van de volgende WooCommerce-functie:

// Get $product object from product ID
$product = wc_get_product( $product_id );

// Do your thing
$product->get_type();
$product->get_name();

Toegang krijgen tot het $product-object als u zich in een querylus bevindt

global $post;

// Get post id
$post_id = $post->ID;

// Get product object
$product = wc_get_product( $post_id );
$product = wc_get_product( $post ); // Or just do it directly

// Go on then…
$product->get_price();

Toegang krijgen tot het $product-object als u iets op de productpagina doet

// Done
global $product;

$product->get_price();

Hoe toegang te krijgen tot het $product-object als u het winkelwagen-object opent

// Cart Object
$cart = WC()->cart->get_cart();

foreach( $cart as $cart_item_key => $cart_item ){

// Product Object
$product = $cart_item['data'];

// Start accessing data
$price = $product->get_price();

}

Een woord van voorzorg

Als je methoden gebruikt op klasse-objecten in php – wat wordt gedaan door $variable->method(), als je dit probeert te doen op een variabele die geen object is of als de methode niet beschikbaar is, zal het een fatale fout opleveren.

Dit betekent dat al het script niet meer wordt uitgevoerd en afhankelijk van waar u deze code heeft geplaatst, wordt uw website afgesloten.

Maak je geen zorgen – de schade is niet blijvend zolang je de wijziging ongedaan maakt.

Hoe dan ook, er is veel dat u kunt doen om ervoor te zorgen dat u vrijuit bent, maar hier is een heel eenvoudige kleine controle.

Het is niet de meest onfeilbare, maar ik gebruik dit vaak omdat het eenvoudig en snel te schrijven is en het me uit de problemen haalt voor bijna alle use-cases waarin ik me bevind.

// Check to make sure this is an object
if ( is_object($product) ) {

// Then do what we’re going to do $product_name = $product->get_name();

}

WooCommerce-productgegevens – WooCommerce-productgegevens – WooCommerce-productgegevens – WooCommerce-productgegevens – WooCommerce-productgegevens