Long story short a couple months ago i made a plugin for WordPress with the Bigcommerce API to fetch products in the Widget Area.
Now I have updated the Single File "Bigcommerce.php" and now the Function getProductImages () is none existent. And I cant seem to find the new function to get the Product Images. Maybe its just to late and Im tired or just plain Blind.
Please let me know how to fetch now the image for a specific product.
See below for the old code used i reverted back to the Old "Bigcommerce.php" and it works again but would ratehr use the new way.
Bigcommerce::configure(array(
'store_url' => $store_url,
'username' => $username,
'api_key' => $api_key
));
Bigcommerce::setCipher('RC4-SHA');
Bigcommerce::verifyPeer(false);
$countProducts = 0;
$products = Bigcommerce::getProducts();
shuffle($products);
echo '<div class="BCStoreWidget">';
if (!$products) {
echo '<div class="BCerror">';
$error = Bigcommerce::getLastError();
echo $error->code;
echo $error->message;
echo '</div>';
} else {
foreach ($products as $product) {
$productImages = Bigcommerce::getProductImages($product->id);
echo '<h4>' . $product->name . '</h4>';
if ($productImages->image_file){
echo '<div class="pimage">';
echo '<img src="' . $store_url . '/product_images/' . $productImages->image_file . '">';
echo '</div>';
}
// echo '<p>' . substr($product->description,0,100) . ' ...</p>';
echo '<p>' . number_format($product->price, 2, '.', '') . ' USD</p>';
echo '<p> Buy Now </p>';
$countProducts++;
if ($countProducts == $max_show)
break;
}
}
echo '</div>';
Thank you all in Advance
You can get product images just by getting the "images" attribute of the product object. So, all you have to do is:
$productImages = $product->images;
You can also access images directly using the getCollection function:
Bigcommerce::getCollection('/products/'.$product_id.'/images/');
Related
Forgive me if this is simple but I have a for each loop that searches JSON data for results from a search. I then have some preg_match statements that will look at some of the tags within the JSON and if their is a match display the thumbnail in a div. and currently this all works. But it currently displays every result in its own Div and i want just five divs with multiple images within if there is a match.
foreach ($hits as $hit)
{
$target = $hit->metadata->baseName;
$target1 = $hit->metadata->cf_imageType;
if(preg_match("/_cm/i",$target)) {
echo '<div id="div5">';
echo '<h2>Creative</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/_SS/i",$target)) {
echo '<div id="div6">';
echo '<h2>Styled</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/_SH/i",$target)) {
echo '<div id="div7">';
echo '<h2>Group</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/still life/i",$target1) && preg_match("/_sm_00|_sd_00/i",$target)) {
echo '<div id="div8">';
echo '<h2>Cutout</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/worn/i",$target1)) {
echo '<div id="div9">';
echo '<h2>Worn</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
}
I cant quite figure out how to accomplish this, would it be the case of putting the results into an array and then displaying the results within the div?
Any help would be greatly appreciated.
You are right and answered the question yourself :)
Collect the results in a first step and create the markup in a second step. Something like this will do it:
$creative = [];
$styled = [];
/* ... */
function getHitInfos($theHit)
{
return [
"url" => $theHit->thumbnailUrl,
"name" => $theHit->metadata->baseName
];
}
function printResults($results, $title) {
echo '<div>';
echo '<h2>'.$title.'</h2>';
foreach ($results as $key => $val) {
echo "<img src='" . $val["url"] . "' alt='error'>";
echo $val["name"];
}
echo '</div>';
}
foreach ($hits as $hit)
{
$target = $hit->metadata->baseName;
$target1 = $hit->metadata->cf_imageType;
echo '<div>';
if(preg_match("/_cm/i",$target)) {
$creative[] = getHitInfos($hit)
}
if(preg_match("/_SS/i",$target)) {
$styled[] = getHitInfos($hit)
}
/* ... */
}
printResults($creative, "Creative");
printResults($styled, "Styled");
/* ... */
Disclaimer: My last contact with php is some years ago, but I hope you will see the point here.
(I also created some helping functions to DRY the code)
I need some help with solving duplicating problem. To deal:
I have 2 filed custom meta
It displays this way :
Only 2 image placed, but it duplicate <li>.
First and third <li> is only video, second and fourth works fine, looks like this :
foreach($slider_xml->childNodes as $slider){
$test_test = get_post_meta(9,'test_1', false);
foreach($test_test as $test_test){
echo '<li class="img-vid-slide">';
echo '<iframe src="https://www.youtube.com/embed/'. $test_test . '" frameborder="0" allowfullscreen></iframe>';
}
echo '<div class="img-slide" >';
if($link_type == 'Lightbox'){
$image_full_url = wp_get_attachment_image_src(find_xml_value($slider, 'image'), 'full');
echo '<a href="' . $image_full_url[0] . '" data-rel="prettyPhoto[flexgal]" title="" >';
}else if($link_type != 'No Link'){
echo '<a href="' . $link . '" >';
}
echo '<img class="'. $i .'" src="' . $image_url[0] . '" alt="' . $alt_text . '" />';
echo '</div>';
if( !empty($title) ){
echo '<div class="flex-caption gdl-slider-caption">';
echo '<div class="gdl-slider-title">' . $title . '</div>';
echo '<div class="slider-title-bar"></div>';
echo $caption;
echo '</div>'; // flex-caption
}
if($link_type != 'No Link'){
echo '</a>';
}
echo '</li>';
}
I find solution.
Filed custom fields: prntscr.com/7oqqvt
I named them like "name_1","name_2"..."name_4".
$i is equal of last nubmer in custom field.
Result on screen: prntscr.com/7oqsaw
$i = 1;
while ($i <= 3):
foreach( $slider_xml->childNodes as $slider ){
// ======= SAME AS FIRST ====== //
$test_test = get_post_meta(9,'test_'. $i .'', true);
echo '<iframe src="https://www.youtube.com/embed/'. $test_test . '" frameborder="0" allowfullscreen></iframe>';
echo '<div class="img-slide" >';
if($link_type == 'Lightbox'){
// ======= SAME AS FIRST ====== //
}
echo '</li>';
$i++;
}
endwhile;
Works fine for me, do what i searching for!
Thanks, Greg, for a little hint, but i go for "while" instead of "for"! Good luck
Don't use the a foreach inside a foreach loop. It a function problem.
Use the alternative "for" loop instead of the double foreach.
What I have is a reply script, and I need the page to only use the username echo'd in for a variable, like if Meap sends me a PM and I want to reply to it, when I hit the reply link I need it to use his name, and not another name from the page. Currently it is using the first name on the page instead of the one that the reply link is for
Here's the code;
while ($row = mysqli_fetch_array($data)) {
session_start();
$reply = $row['from'];
echo '<div class="viewpost">';
echo '<div class="vpside">';
if(!empty($row['picture'])) {
echo '<img class="pictest" src="' . MM_UPLOADPATH . $row['picture'] . '" alt="' . MM_UPLOADPATH . 'nopic.png' . '" />';
}
if(!empty($row['from'])) {
echo '<p>From:<br />' . $row['from'] . '</p>';
$_SESSION['reply'] = $row['from'];
echo 'Reply';
}
if(!empty($row['rank'])) {
echo '<p>Rank:<br />' . $row['rank'] . '</p>';
}
if(!empty($row['gender'])){
echo '<p>Gender:<br /> ' . $row['gender'] . '</p>';
}
echo '</div>';
if(!empty($row['title'])) {
echo'<h4><u>' .$row['title']. '</u></h4>';
}
if(!empty($row['msg'])) {
echo '<p class="">' . $row['msg'] . '</p>';
}
echo '<div class="sig">';
if(!empty($row['bio'])) {
echo '<p>' . $row['bio'] . '</p>';
}
echo '</div>';
echo '</div><br />';
}
Here's a picture of what it looks like when you get a Private Message;
Evidently you have to be specific down to the last detail on here, the question - or rather the problem - is that it is not using the correct recipient for the reply. Say I click reply under Meaps name, it will use Lilpunish instead. Basically it will use the first one loaded, and I need it to use the correct username. How would I do this.?
Here's your problem:
$_SESSION['reply'] = $row['from'];
echo 'Reply';
Consider what is happening when this code is run multiple times. The first time, it sets the session variable reply to Meap. The next time, when the second message is printed, it sets it to another value.
What you actually want is to pass who to reply as a part of the link, e.g.
echo 'Reply';
And then not retrieve the reply-to from $_SESSION. Session variables are not really supposed to be used like this.
Basically I need to call a field (wrapped in a div) if the field has a value. I do not want the field or div to display if the field has no value. My PHP knowledge is dire but here is what I have to work with.
Here are instructions provided to me on how to call a custom field by ID:
$cust_1 = $this->fields->getFieldByCaption('Custom Text'); // getFieldByCaption() allow you to get the field by the Caption. This is not the best way to get a field since changing the caption in the back-end will break the reference.
echo '<br />Field ID: ' . $cust_1->getId();
$cust_2 = $this->fields->getFieldById(29); // getFieldById() is the ideal way of getting a field. The ID can be found at 'Custom Fields' section in Mosets Tree's back-end.
echo '<br />Name: ' . $cust_2->getName();
echo '<br />Has Caption? ' . (($cust_2->hasCaption()) ? 'Yes' : 'No');
echo '<br />Caption: ' . $cust_1->getCaption();
echo '<br />Value: ' . $cust_2->getValue();
echo '<br />Output: ' . $cust_2->getOutput(1);
echo '<hr />';
$this->fields->resetPointer();
while( $this->fields->hasNext() ) {
$field = $this->fields->getField();
echo '<br /><strong>' . $field->getCaption() . '</strong>';
echo ': ';
echo $field->getOutput(1); // getOutput() returns the formatted value of the field. ie: For a youtube video, the youtube player will be loaded
// echo $field->getValue(); // getValue() returns the raw value without additional formatting. ie: When getting value from a Online Video field type, it will return the URL.
$this->fields->next();
}
Here is the code I am working with and need help to try and do what I need it to as detailed above:
<?php
if( !is_null($this->fields->getFieldById(35)) ) {
$value = $field->getValue();
$hasValue = $field->hasValue();
$field = $this->fields->getField();
if( $hasValue )
{
echo '<div class="lin" id="lphn">'; {
echo $cust_2 = $this->fields->getFieldById(35);
echo $cust_2->getOutput(1);
echo '</div>';
}
else {
// Print nothing.
}
}
}
?>
I'm using PHP last.fm API to fetch artist image
I'd like to know how to cache the calls so that I don't need to make same call again and again. The API has facility to cache : DiskCache, SqliteCache but there is no documentation, could anyone please shed some light on it?
Below is my code :
<?php
require __DIR__ . "/src/lastfm.api.php";
// set api key
CallerFactory::getDefaultCaller()->setApiKey(LAST_FM_API_KEY);
// search for the Coldplay band
$artistName = "Coldplay";
$limit = 1;
$results = Artist::search($artistName, $limit);
echo "<ul>";
while ($artist = $results->current()) {
echo "<li><div>";
echo "Artist URL: " . $artist->getUrl() . "<br>";
echo '<img src="' . $artist->getImage(4) . '">';
echo "</div></li>";
$artist = $results->next();
}
echo "</ul>";
?>