Open gregcarv opened 4 years ago
@gregcarv
Hey Greg,
Yes at the moment only those four templates are customizable. I made a mistake an accidentally left those old partial files in the plugin which were used for the older 1.x version.
With that said, I am planning to release more templates in the near future for version 3.0. I'm hoping to release this in mid-March.
Is there anything in particular you'd to see?
Hi Andrew,
So, there is no way to customize the output of products in a loop? For instance, pulling the excerpt for the product instead of the full description, or adding a conditional for changing the button text depending on product tags etc...
@gregcarv
Technically you can do this by fetching products using method's the plugin provides, and then doing what you describe. For example:
$DB_Products = Factories\DB\Products_Factory::build();
// Grab product ids from post ids
$products = $DB_Products->get_product_ids_from_post_ids([123, 456, 789])
The $products
variable here would represent an array of product ids which you can then loop through and fetch the details (description, title, etc) using this method:
$product_details = $DB_Products->get_product_from_product_id($product_id);
However, in order to render product components (like the buy button) which work with the cart, you must use the plugin's Render API.
So building on the above example, you can then render "buy buttons" for each product like this:
foreach ($product_ids as $product_id) {
// Do something with the product details
$product_details = $DB_Products->get_product_from_product_id($product_id);
// Render the buy button component in React JS
$Render_Products->buy_button([
'product_id' => $product_id
]);
}
You can see a list of different options for rendering in the docs here: https://docs.wpshop.io/#/render-api/products.
Does this help?
Ok thanks, that clarifies some of it. I hadn't realized you were using react. It makes a bit more sense to me now.
To clarify what I am trying to do:
I was looking specifically at the "collections-single.php" and "products-all.php".
I had a few conditions* I wanted to set on the products list rendered on those templates, but that means modifying the "products" component (which I assume i don't have access to modifying).
Basically I need to create my own products component for the products list and single collection.
So I guess the main question is: 1) for a single collection, can I extract a list of product id's or post id's? or given a collection id, can I get the list of associated product id's?
e.g.;
$DB_Products = WP_Shopify\Factories\DB\Products_Factory::build(); $products = $DB_Products->get_products_by_collection_id('159247171648');
@gregcarv
In version 3.0
of the plugin (coming mid-March), I'll be exposing and documenting many additional methods like these. So the API will get better. But for now, here's what you can try doing:
global $post;
$DB_Products = Factories\DB\Products_Factory::build();
$DB_Collections = Factories\DB\Collections_Factory::build();
$collection_ids = $DB_Collections->get_collection_ids_from_post_ids($post->ID)
$products_from_collection = $DB_Products->get_products_by_collection_id($collection_ids);
The $products_from_collection
variable here will be an array of products associated with the given collection. Each product object should contain the product id.
Let me know if this helps!
I have tried this a couple of ways:
1) getting the collection id from the post meta:
$collection_ids_meta= get_post_meta($post_id, 'collection_id', true); $products_from_collection = $DB_Products->get_products_by_collection_id($collection_ids_meta);
2) the method you proposed:
$collection_ids = $DB_Collections->get_collection_ids_from_post_ids($post_id);
$products_from_collection = $DB_Products->get_products_by_collection_id($collection_ids);
Both approaches return an empty array. I double checked the collections had content by switching back to your templates.
I'm only having this problem on the collections-single.php
@gregcarv
Regarding method #2 above, is the $collection_ids
variable empty as well?
Can you also confirm whether the $post_id
variable matches the collection post?
for both method 1 & 2, I get the correct value for the $post_id and the $collection_ids
The only difference between method 1 & 2 is that method 2 returns an array with a single value for the collection Id Array ( [0] => 158055792704 )
.
Method #1 returns '158055792704'
Ah, I think I see the issue with 2. Try this:
$collection_ids = $DB_Collections->get_collection_ids_from_post_ids($post_id);
$products_from_collection = $DB_Products->get_products_by_collection_id($collection_ids[0]);
That doesn't work. I still get an empty array. It's the same as $collection_ids_meta = get_post_meta($post->ID, 'collection_id', true);
I have gotten most of what I need working well in the products-all.php. I'm gonna see if I can build a shortcode using my own template to generate the output I need.
Now I just need to get the collections working :)
I just wanted to thank you in advance for all the help.
After some digging and testing I found that the manual collections are syncing to the collects table, but the smart collections are not.
I recently acquired the pro version.
You documentation states that with the pro version the following templates can be modified when copied to your theme:
Does this mean that only those 4 specific files can be modified, or are the partials in the /wps-templates/components/ also modifiable?